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

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

/*
实现了单向无头链表的几个函数 插入,删除,展示,寻找,释放
对于直接需要对链表本身修改的 函数传参问题,二级指针 或者是 函数返回值
我们一般只对 nohead.c进行实现,main.c则是由整体的框架决定,所以对于返回的什么值,最好不要在nohead.c中直接输出值
*/

nohead.h

#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

#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

#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

all:main
main:main.o nohead.o
	$(CC) $^ -O $@
clean:
	rm *.o main -rf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值