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
相关推荐
long31622 分钟前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
Once_day1 小时前
C++之《程序员自我修养》读书总结(1)
c语言·开发语言·c++·程序员自我修养
坚果派·白晓明2 小时前
在鸿蒙设备上快速验证由lycium工具快速交叉编译的C/C++三方库
c语言·c++·harmonyos·鸿蒙·编程语言·openharmony·三方库
风指引着方向3 小时前
图编译优化全链路:CANN graph-engine 仓库技术拆解
c语言
张张努力变强3 小时前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
wWYy.3 小时前
数组快排 链表归并
数据结构·链表
C++ 老炮儿的技术栈3 小时前
VS2015 + Qt 实现图形化Hello World(详细步骤)
c语言·开发语言·c++·windows·qt
Once_day3 小时前
C++之《Effective C++》读书总结(4)
c语言·c++·effective c++
李斯啦果3 小时前
【PTA】L1-019 谁先倒
数据结构·算法