单链表---实现基本的增删改查功能

作为掌握C语言基本语法知识之后的拓展延伸,数据结构再合适不过,首先我们将接触到链表的使用。

在此将以 如下程序样例 分段解析 链表的具体使用方法

首先是程序头部分,其包含如下

1-所需头文件

2-宏定义初始链表成员数量

3-定义链表的数据类型

4-声明各类函数

#include<stdio.h>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
#define Num 5


typedef struct stu
{
	int num;
	double score;
	struct stu* next;
}Link;

void menu(Link * head);								/*菜单*/
Link* insert(Link *head);							/*增*/
Link* dele(Link* head);								/*删*/
Link* creat(Link* head, int n);						/*创*/
void find(Link* head);								/*查*/
void deplay(Link * head);							/*展示*/

主函数内容简短精炼,只负责初始化链表头节点 , 菜单调用内存释放

/*主函数√*/
int main(void)
{
	Link* head = NULL;
	menu(head); 
		free(head);
	return 0;
}

主要框架交由菜单函数负责  一个简易的while 循环 与 switch导向 实现各类函数调用与程序的有序执行.

/*菜单√*/
void menu(Link* head)					
{
	int Get;
	int Flag = 2;
	int F = 0;
	while (Flag != EOF)
	{
		if(Flag != 2)
		system("pause");
		system("cls");
		printf("---------------Welcome---------------\n"
				"->1.创建_数据表(成员数为%d)\n"
				"->2.查询_单个成员信息\n"
				"->3.展示_所有成员信息\n"
				"->4.插入_新成员信息\n"
				"->5.删除_某成员信息\n"
				"->0.退出\n"
				"-------------------------------------\n"
				"请输入功能号->>"
				, Num);
		Flag = scanf("%d",&Get);
		switch(Get)
		{
		case 1:
			if (F == 1)
			{
				printf("链表已创建\n");
				break;
			}
			head = creat(head , Num);
			F = 1;
			break;
		case 2:
			if (F == 0)
			{
				printf("链表未创建\n");
				break;
			}
			find(head);
			break;
		case 3:
			if (F == 0)
			{
				printf("链表未创建\n");
				break;
			}
			deplay(head);
			break;
		case 4:
			if (F == 0)
			{
				printf("链表未创建\n");
				break;
			}
			head = insert(head);
			break;
		case 5:
			if (F == 0)
			{
				printf("链表未创建\n");
				break;
			}
			head = dele(head);
			break;
		case 0:
			printf("\nQuit!\n");
			exit(0);
		default:
			printf("error! intput again!\n");
			break;
		}
	}
}

链表创建函数 此处实参(头节点宏定义的成员数量) 初始成员数量 由宏定义来完成  也可更改为输入流文件流输入 皆可 


/*创√*/
Link* creat(Link* head, int n)
{
	Link* L, * R;
	printf("学员信息录入操作\n");
	for (int i = 1; i <= n; i++)
	{
		printf("请输入第 %d 同学的成绩信息(学号 分数) :",i);
		R = (Link*)malloc(sizeof(Link));
		while ((scanf("%d %lf", &R->num, &R->score)) == EOF)
			printf("输入格式错误 请重新输入->");
		R->next = NULL;
		if (head == NULL)
			head = R;
		else
			L->next = R;
		L = R;
	}
	return head;
}

链表新成员插入函数  输入流输入新成员数据 并 有序插入到链表中 同时判断重复成员

/*增√*/
Link* insert(Link* head)				
{
	Link* q , *p1 ,*p = head;
	int flag = 1;
	q = (Link*)malloc(sizeof(Link));
	printf("学员信息插入操作");
	printf("请输入添加的学员学号及成绩->");
	while ((scanf("%d %lf", &q->num, &q->score)) == EOF)
	{
		while (p != NULL)
		{
			if (p->num == q->num)
			{
				flag = 0;
				printf("学员学号重复-请重新输入->");
				break;
			}
			p = p->next;
		}
		if (flag == 1)
			break;
		p = head;
	}
	if (head == NULL) //空表填入
	{
		q->next = NULL;
		head = q;
		return head;
	}
	if (head->num > q->num)//插入表头之前
	{
		q->next = head;
		head = q;
		return head;
	}
	p = head;
	p1 = p->next;
	while (p1 != NULL && p1->num < q->num)
	{
		p = p1;
		p1 = p1->next;
	}
	p->next = q;
	q->next = p1;
	return head;
}

链表成员删除函数  

/*删*/
Link* dele(Link* head) 		
{
	Link* p, * p1;
	int flag = 0;
	int num;
	double score;
	printf("学员信息删除操作");
	printf("请输入添加的学员学号及成绩->");
	while ((scanf("%d %lf", &num, &score)) == EOF)
		printf("输入格式错误 请重新输入->");
	p = head;
	p1 = p->next;
	if (num == head->num && score == head->score) // 删除头节点
	{
		head = head->next;
		flag = 1;
		free(p);
		return head;
	}
	while (p1->next!= NULL && num != p1->num)
	{
		p = p1;
		p1 = p1->next;
	}
	if (p1->num != num && flag == 0)
	{
		printf("未找到该学员信息\n");
	}
	else
	{
		p1 = p1->next;
		p->next = p1;	
	}
	return head;
}

链表单个成员信息查询函数  多种查询方式(学号与成绩)  并依据输入 进行相应适宜的反馈

/*查√*/
void find(Link * head) 		
{
	int flag;
	int num;
	double score;
	int i = 0;
	Link* p = head;
	printf("---------------学员成绩查询功能---------------\n");
	printf("请选择查询模式:\n"
			"->1.按学号查询\n"
			"->2.按分数查询\n"
			"->");
	while (scanf("%d", &flag) == EOF && flag != 1 && flag != 2)
	{
		printf("输入错误 请重新输入\n->");
	}

	if (flag == 1)
	{
		printf("请输入所需查询的学员学号->");
		while ((scanf("%d",&num)) == EOF)
			printf("输入格式错误 请重新输入->");
		while (p != NULL)
		{
			if (p->num == num)
			{
				printf("num\t   score\n");
				printf("%.3d\t%8.2lf\n", p->num, p->score);
				break;
			}
			else if (p->next == NULL)
				printf("未找到\n");
			p = p->next;
		}
	}
	else if (flag == 2)
	{
		printf("请输入所需查询的学员分数->");
		while ((scanf("%lf", &score)) == EOF)
			printf("输入格式错误 请重新输入->");

		while (p != NULL)
		{
			if (p->score == score)
			{
				i++;
				if (i == 1)
				printf("num\t   score\n");
				printf("%.3d\t%8.2lf\n", p->num, p->score);
			}
			else if (p->next == NULL && i == 0)
				printf("未找到\n");
			p = p->next;
		}
		printf("找到该分数学员共%d人\n",i);
	}
}

链表 遍历展示函数  遍历链表所有成员 有序输出所有成员信息并打印到屏幕上

/*展示√*/
void deplay(Link* head) 				
{
	Link* p = head;
	printf("num\t   score\n");
	while ( p != NULL)
	{
		printf("%.3d\t%8.2lf\n",p->num,p->score);
		p = p->next;
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个基于单链表的火车票信息管理系统的实现,包括增加、删除、修改和查询火车票信息的功能: ```python class TrainTicket: def __init__(self, train_num, start_station, end_station, date, price, next_ticket=None): self.train_num = train_num self.start_station = start_station self.end_station = end_station self.date = date self.price = price self.next_ticket = next_ticket class TrainTicketLinkedList: def __init__(self): self.head = None # 添加火车票信息 def add_ticket(self, train_num, start_station, end_station, date, price): new_ticket = TrainTicket(train_num, start_station, end_station, date, price) if self.head is None: self.head = new_ticket else: current_ticket = self.head while current_ticket.next_ticket is not None: current_ticket = current_ticket.next_ticket current_ticket.next_ticket = new_ticket # 删除火车票信息 def remove_ticket(self, train_num): if self.head is None: return if self.head.train_num == train_num: self.head = self.head.next_ticket return current_ticket = self.head while current_ticket.next_ticket is not None: if current_ticket.next_ticket.train_num == train_num: current_ticket.next_ticket = current_ticket.next_ticket.next_ticket return current_ticket = current_ticket.next_ticket # 修改火车票信息 def update_ticket(self, train_num, start_station, end_station, date, price): current_ticket = self.get_ticket(train_num) if current_ticket is not None: current_ticket.start_station = start_station current_ticket.end_station = end_station current_ticket.date = date current_ticket.price = price # 查询火车票信息 def get_ticket(self, train_num): current_ticket = self.head while current_ticket is not None: if current_ticket.train_num == train_num: return current_ticket current_ticket = current_ticket.next_ticket return None # 打印所有火车票信息 def display_tickets(self): if self.head is None: print("No train tickets available.") return current_ticket = self.head while current_ticket is not None: print("Train number:", current_ticket.train_num) print("Start station:", current_ticket.start_station) print("End station:", current_ticket.end_station) print("Date:", current_ticket.date) print("Price:", current_ticket.price) print("-----------------------") current_ticket = current_ticket.next_ticket ``` 你可以通过创建一个 `TrainTicketLinkedList` 对象,来使用这些功能,例如: ```python # 创建一个火车票信息管理系统 ticket_system = TrainTicketLinkedList() # 增加火车票信息 ticket_system.add_ticket("G1234", "Beijing", "Shanghai", "2022-01-01", 500) ticket_system.add_ticket("G2345", "Shanghai", "Guangzhou", "2022-01-02", 600) ticket_system.add_ticket("G3456", "Beijing", "Guangzhou", "2022-01-03", 800) # 打印所有火车票信息 ticket_system.display_tickets() # 查询火车票信息 ticket = ticket_system.get_ticket("G2345") if ticket is not None: print("Train number:", ticket.train_num) print("Start station:", ticket.start_station) print("End station:", ticket.end_station) print("Date:", ticket.date) print("Price:", ticket.price) else: print("Train ticket not found.") # 修改火车票信息 ticket_system.update_ticket("G3456", "Beijing", "Shenzhen", "2022-01-03", 900) # 删除火车票信息 ticket_system.remove_ticket("G1234") # 打印所有火车票信息 ticket_system.display_tickets() ``` 输出结果如下: ``` Train number: G1234 Start station: Beijing End station: Shanghai Date: 2022-01-01 Price: 500 ----------------------- Train number: G2345 Start station: Shanghai End station: Guangzhou Date: 2022-01-02 Price: 600 ----------------------- Train number: G3456 Start station: Beijing End station: Guangzhou Date: 2022-01-03 Price: 800 ----------------------- Train number: G2345 Start station: Shanghai End station: Guangzhou Date: 2022-01-02 Price: 600 Train number: G3456 Start station: Beijing End station: Shenzhen Date: 2022-01-03 Price: 900 ----------------------- ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值