C-数据结构-单向链表(无头结点)

无头结点的单向不循环链表

带头结点最简单的形式:一个头节点 数据域不管,指针域为空。

/*

实现了单向无头链表的几个函数 插入,删除,展示,寻找,释放

对于直接需要对链表本身修改的 函数传参问题,二级指针 或者是 函数返回值

我们一般只对 nohead.c进行实现,main.c则是由整体的框架决定,所以对于返回的什么值,最好不要在nohead.c中直接输出值

*/

nohead.h

c 复制代码
#ifndef NOHEAD_H__
#define NOHEAD_H__

#define NAMESIZE 32

struct score_st
{
	int id;
	char name[NAMESIZE];
	int math;
	int chinese;
	
};

struct node_st
{
	struct score_st data;
	struct node_st *next;
	
};

//struct node_st * list_insert(struct node_st *,struct score_st *data);
int list_insert(struct node_st **,struct score_st *data);

void list_show(struct node_st *);

int list_delete(struct node_st **);
struct score_st * list_find(struct node_st *,int id);
void list_destory(struct node_st *);

#endif

nohead.c

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include"nohead.h"

/*
struct node_st * list_insert(struct node_st *list,struct score_st *data)
{
	struct node_st *new;
	
	new = malloc(sizeof(*new));
	if(new = NULL)
		return NULL;
	new->data = *data;
	//new->next = NULL;
	new->next = list;
	list = new;
	
	return list;
}
*/
int list_insert(struct node_st **list,struct score_st *data)
{
	struct node_st *new;
	
	new = malloc(sizeof(*new));
	if(new = NULL)
		return -1;
	new->data = *data;
	//new->next = NULL;
	new->next = *list;
	*list = new;
	
	return 0;
}
void list_show(struct node_st *list)
{
	struct node_st *cur;
	for(cur = list;cur != NULL;cur = cur->next)
	{
		printf("%d %s %d %d\n",cur->data.id,cur->data.name,cur->data.math,cur->data.chinese);
	}
}

int list_delete(struct node_st **)
{
	struct node_st *cur;
	if(*list == NULL)
		return -1;
	cur = *list;
	*list = (*list)->next;
	free(cur);
	return 0;
}

struct score_st *list_find(struct node_st *list,int id)
{
	struct node_st *cur;
	for(cur = list;cur !=NULL ;cur = cur->next)
	{
		if(cur->data.id == id)
		{
			//printf("%d %s %d %d\n",cur->data.id,cur->data.name,cur->data.math,cur->data.chinese)
			return &cur->data;
		}
	}
	return NULL;
}
//cur 是指向当前节点的指针。cur->data 是当前节点中的 score_st 类型的数据。&cur->data 是获取 cur->data 的地址,也就是指向 cur->data 的指针。
/*
int list_find(struct node_st *list,int id)
{
	struct node_st *cur;
	for(cur = list;cur !=NULL ;cur = cur->next)
	{
		if(cur->data.id == id)
		{
			printf("%d %s %d %d\n",cur->data.id,cur->data.name,cur->data.math,cur->data.chinese)
			return 0;
		}
	}
	return -1;
}
*/
void list_destory(struct node_st *list)
{
	struct node_st *cur;
	if(list == NULL)
		return ;
	for(cur = list;cur !=NULL;cur = list)
	{
		list = cur->next;
		free(cur);
	}

}

main.c

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include"nohead.h"
#define NAMESIZE 32


int main()
{
	int i,ret;
	struct node_st *list = NULL;
	struct score_st tmp;
	for(i=0;i<7;i++)
	{
		tmp.id = i;
		snprintf(tmp.name,NAMESIZE,"stu%d",i);
		tmp.math = rand()%100;
		tmp.chinese = rand()%100;
		
		//list = list_insert(list,&tmp);
		ret = list_insert(&list,&tmp);
		if(ret != 0)
			exit(1);
	}
	list_show(list);
	//list_delete(&list);
	//list_show(list);
	int id = 3;
	struct score_st *ptr;
	ptr = list_find(list,id);
	if(ptr ==NULL)
		printf("Can not find!\n");
	else
		printf("%d %s %d %d\n",ptr->id,ptr->name,ptr->math,ptr->chinese);

	list_destory(list);
	exit(0);
}

MAKEFILE

c 复制代码
all:main
main:main.o nohead.o
	$(CC) $^ -O $@
clean:
	rm *.o main -rf
相关推荐
Savior`L5 小时前
二分算法及常见用法
数据结构·c++·算法
吃西瓜的年年5 小时前
1. 初识C语言
c语言·开发语言
永远都不秃头的程序员(互关)6 小时前
C语言 基本语法
c语言·开发语言
甄心爱学习7 小时前
CSP认证 备考(python)
数据结构·python·算法·动态规划
kyle~7 小时前
排序---常用排序算法汇总
数据结构·算法·排序算法
赖small强7 小时前
【Linux C/C++开发】Linux 平台 Stack Protector 机制深度解析
linux·c语言·c++·stack protector·stack-protector·金丝雀机制
有时间要学习8 小时前
面试150——第二周
数据结构·算法·leetcode
freedom_1024_8 小时前
红黑树底层原理拆解
开发语言·数据结构·b树
liu****8 小时前
3.链表讲解
c语言·开发语言·数据结构·算法·链表
minji...8 小时前
Linux 基础IO(一) (C语言文件接口、系统调用文件调用接口open,write,close、文件fd)
linux·运维·服务器·网络·数据结构·c++