数据结构无头结点单向不循环链表(C语言版)

main.c(负责测试)

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "nohead.h"
int main()
{
	LNode *list = NULL;
	struct score_st data[10],mydata,data1;
	int i = 0;
	srand((unsigned)time(NULL));
	//初始化数组
	for (i = 0; i < 10; i++)
	{
		data[i].chinese = rand() % 100;
		data[i].english = rand() % 100;
		data[i].math = rand() % 100;
		data[i].student_id = i;
		snprintf(data[i].name, 32, "学生的名字是%d", i);
	}
	data1.chinese = rand() % 100;
	data1.english = rand() % 100;
	data1.math = rand() % 100;
	data1.student_id = 100;
	snprintf(data1.name, 32, "学生的名字是%d", 100);
	//打印链表中的数据
	list_display(list);
	//链表中插入数据,头部插入,无头节点单向不循环链表
	for (i = 0; i < 10; i++)
	{
		list_insert(&list, &data[i]);
	}
	list_display(list);
	//查找id = 3的学生信息
	list_find(list,3,&mydata);
	printf("%4s %20s %4s %4s %4s\n", "学号", "姓名", "数学", "语文", "外语");
	printf("%4d %20s %4d %4d %4d\n", mydata.student_id,
		mydata.name, mydata.math, mydata.chinese,
		mydata.english);
	//删除链表中的第一个有效节点(单向不循环无头节点链表)
	//list_delete(&list);
	//list_display(list);
	list_insert_at(&list,0,&data1);
	list_display(list);
	list_destroy(list);
}

nohead.c(负责函数实现)

#include<stdio.h>
#include<stdlib.h>
#include "nohead.h"

int list_insert_at(struct LNode **list,int i, struct score_st *data)
{
	struct LNode *node = *list,*newnode = NULL;
	int j = 0;
	if (i < 0)
	{
		return -1;
	}
	if (i == 0)
	{
		newnode = (LNode*)malloc(sizeof(LNode));
		if (newnode == NULL)
		{
			return -3;
		}
		newnode->score = *data;
		newnode->next = *list;
		*list = newnode;
		return 0;
	}
	//找到第i-1个有效节点
	while ((j < i-1)&&(node!=NULL))
	{
		node = node->next;
		j++;
	}
	if (node == NULL)
	{
		return -2;
	}
	newnode = (LNode*)malloc(sizeof(LNode));
	if (newnode == NULL)
	{
		return -3;
	}
	newnode->score = *data;
	newnode->next = node->next;
	node->next = newnode;
	return 0;
}

int list_insert(struct LNode **list, struct score_st *data)
{
	LNode *newnode = NULL;
	newnode = (LNode*)malloc(sizeof(LNode));
	if (newnode == NULL)
		return -1;
	newnode->score = *data;
	newnode->next = *list;
	*list = newnode;
}

void list_display(LNode *list)
{
	LNode *ps = list;
	if (ps == NULL)
	{
		printf("链表为空\n");
		return -1;
	}
	printf("学生的信息如下:\n");
	printf("%4s %20s %4s %4s %4s\n", "学号", "姓名", "数学", "语文", "外语");
	while (ps)
	{
		printf("%4d %20s %4d %4d %4d\n", ps->score.student_id,
			ps->score.name, ps->score.math, ps->score.chinese,
			ps->score.english);
		ps = ps->next;
	}
}


int list_find(LNode *ps,int id,struct score_st *data)
{
	while (ps)
	{
		if (ps->score.student_id == id)
		{
			*data = ps->score;
			return 0;
		}
		ps=ps->next;
	}
	return -1;
}

//删除无头节点链表中第一个有效节点
int list_delete(LNode **ps)
{
	LNode* curnode = *ps;
	if (*ps == NULL)
	{
		printf("链表为空删除失败\n");
		return -1;
	}
	(*ps) = curnode->next;
	free(curnode);
	return 0;
}

void list_destroy(LNode *p)
{
	LNode *nextnode = NULL;
	while (p)
	{
		nextnode = p->next;
		free(p);
	
		p = nextnode;
	}

}

nohead.h(负责函数声明)

#ifndef NOHEAD_H__
#define NOHEAD_H__
#define MAX_SIZE 32
//定义结构体类型,保存学生成绩
struct score_st
{
	char name[MAX_SIZE];
	int student_id;
	int math;
	int chinese;
	int english;
};
typedef struct LNode
{
	struct score_st score;
	struct LNode *next;
}LNode;
//无头节点单向不循环链表,在链表头部插入数据,第一个有效节点的下标为0
int list_insert(struct LNode *list, struct score_st *score);
//按位置插入节点
int list_insert_at(struct LNode **list, int i, struct score_st *data);
void list_display(LNode *list);
int list_delete(LNode **ps);
void list_destroy(LNode *list);
//查找id==**的结点
int list_find(LNode *ps, int id,struct score_st *data);
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值