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
相关推荐
hong1616882 分钟前
VSCode中配置C/C++环境
c语言·c++·vscode
Crossoads1 小时前
【数据结构】排序算法---快速排序
c语言·开发语言·数据结构·算法·排序算法
眰恦3741 小时前
数据结构--第五章树与二叉树
数据结构·算法
未 顾1 小时前
JavaSE--集合总览02:单列集合Collection的体系之一:List
数据结构·list
小立爱学习1 小时前
Linux 给 vmlinux 添加符号
linux·c语言
wx200411021 小时前
Codeforces Round 973 (Div. 2) - D题
数据结构·c++·算法
Crossoads2 小时前
【数据结构】排序算法---基数排序
c语言·开发语言·数据结构·算法·排序算法
爱敲代码的憨仔2 小时前
第二讲 数据结构
数据结构·算法
问道飞鱼2 小时前
每日学习一个数据结构-布隆过滤器Bloom Filter
数据结构·学习·哈希算法
龙ze2 小时前
数据结构—(java)反射,枚举,lambda表达式
java·开发语言·数据结构