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
相关推荐
Fanxt_Ja17 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后12318 小时前
【数据结构】二叉树的概念
数据结构·二叉树
小莞尔19 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔19 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing1023292919 小时前
Day03_刷题niuke20250915
c语言
第七序章1 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
l1t1 天前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
l1t1 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
散1121 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19431 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack