单向链表(增,删,改,查,插入)源码

我就是个新手敲的代码有点乱,随便敲的,没有太多注释,各位理解不太好的可以咨询我

开始继续学习啦!

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _student
{
	short ID;//学号
	char name[9];//名字
	char sex[4];//性别
	short age;//年龄
}student;

typedef struct _std
{
	student date;
	struct _std* next;
}std;

std* xuanze(std* head, std* temp); //上下各一个函数     方便调用

void jiemian()
{
	printf("\t\t\t+--------------欢迎来到学员记录表-------------+\n");
	printf("\t\t\t|------+++++++++++++++++++++++++++++++++------|\n");
	printf("\t\t\t|------1------------增加数据-----------1------|\n");
	printf("\t\t\t|------2------------插入数据-----------2------|\n");
	printf("\t\t\t主-----3------------删除数据-----------3-----主\n");
	printf("\t\t\t菜-----4------------修改数据-----------4-----菜\n");
	printf("\t\t\t单-----5------------查找数据-----------5-----单\n");
	printf("\t\t\t|------6------------查看数据-----------6------|\n");
	printf("\t\t\t|------7------------结束程序-----------7------|\n");
	printf("\t\t\t|------+++++++++++++++++++++++++++++++++------|\n");
	printf("\t\t\t+--------------欢迎来到学员记录表-------------+\n");
}

int findwode()
{
	int i;
	printf("\t\t\t+---------------查找数据记录表----------------+\n");
	printf("\t\t\t|------+++++++++++++++++++++++++++++++++------|\n");
	printf("\t\t\t查-----1---------通过性别查找----------1-----查\n");
	printf("\t\t\t找-----2---------通过年龄查找----------2-----找\n");
	printf("\t\t\t|------3---------通过贵姓查找----------3------|\n");
	printf("\t\t\t|------+++++++++++++++++++++++++++++++++------|\n");
	printf("\t\t\t+---------------查找数据记录表----------------+\n");
	printf("\n\t\t\t请输入即将查找参数的序号:");
	scanf("%d", &i);
	return i;
}


std* inithead()//初始化头部(仅仅调用一次)已在main里完成
{
	std* head;
	head = (std*)malloc(sizeof(std));
	head->next = NULL;
	return head;
}

std* _1scanfnode(std* head, std* temp) //1.增加数据------------------------------------------------------------------------------------------------------------------------------
{
	int i=100;
	std* node;
	node = (std*)malloc(sizeof(std));

	std* ptemp = temp;

	node->next = NULL;

	printf("\t\t\t             名字:");
	scanf("%s", node->date.name);

	printf("\t\t\t             性别:");
	scanf("%s", node->date.sex);


	printf("\t\t\t             年龄:");
	scanf("%hd", &node->date.age);

	printf("\t\t\t             学号:");
	scanf("%hd", &node->date.ID);

	while (ptemp->date.ID == node->date.ID)//只要等于就重新输入
	{
		printf("不能重复学号,请重新输入:");
		scanf("%hd", &node->date.ID);
	}

	temp->next = node;
	temp = node;
	
	while (i != 1)
	{
		printf("\n\n\n\n\t\t\t继续输入请按“1”,返回主菜单请按“0”:\n");
		scanf("%d",&i);
		if (i == 1)
		{
			_1scanfnode(head, temp);

		}
		if (i == 0)
		{
			system("cls"); 
			temp = xuanze(head, temp);
		}
		else
		{
			printf("输入的数据有误请重新输入!\n");
			scanf("%d", &i);
		}
	
	}
	return temp;
}
std* charu_cg(std* head, std* temp);

std* charu(std* head,std* temp)//1.插入数据-----------------------------------------------------------------------------------------------------------------
{
	int j;
	std* node = (std*)malloc(sizeof(std));
	node->next = NULL;//一定为空为以后做准备
	std* phead = head, * bhead = head;
	std* qianhead = NULL, * zhonghead = NULL, * houhead = NULL;
	int ID = 0;

	if (head->next == NULL)
	{
		printf("\t\t\t对不起没有数据可插入!");
		printf("\t\t\t返回主菜单请按“0”");
		scanf("%d", &j);
		if (j == 0)
		{
			xuanze(head, temp);
		}
	}
	else
	{
		system("cls");
		printf("\t\t\t        名字        性别        年龄        学号\n");
		while (bhead->next != NULL)
		{
			bhead = bhead->next;
			printf("\t\t\t%10s   %10s   %10hd   %10hd\n", bhead->date.name, bhead->date.sex, bhead->date.age, bhead->date.ID);
		}

		printf("\t\t\t想插入第几号成员的后面:\n");
		scanf("%d", &ID);

		system("cls");
		printf("\t\t\t请输入即将插入的数据:\n");
		printf("\t\t\t             名字:");
		scanf("%s", node->date.name);

		printf("\t\t\t             性别:");
		scanf("%s", node->date.sex);


		printf("\t\t\t             年龄:");
		scanf("%hd", &node->date.age);

		printf("\t\t\t             学号:");
		scanf("%hd", &node->date.ID);

		while (head->next)
		{
			qianhead = head;//前

			head = head->next;//中
			zhonghead = head;

			if (zhonghead->next == NULL && zhonghead->date.ID == ID)//当有一个值时
			{
				zhonghead->next = node;
				charu_cg(phead, temp);
			}
			if (zhonghead->next != NULL)//不为空时
			{
				head = head->next;//后
				houhead = head;
			}
			if (zhonghead->date.ID == ID)//当有第三个,或者多个
			{
				zhonghead->next = node;
				node->next = houhead;
				charu_cg(phead, temp);
			}

			if (houhead->next == NULL && houhead->date.ID == ID)//当有两个值时
			{
				zhonghead->next = node;
				node->next = houhead;
				charu_cg(phead, temp);
			}
		}
	}	
	return phead;
}

std* charu_cg(std* head,std* temp)
{
	int i;
	printf("插入成功!");
	printf("\t\t\t返回主菜单请按“0”,继续插入请按“1”:");
	scanf("%d", &i);
	if (i == 0)
	{
		xuanze(head, temp);
	}
	else
	{
		charu(head, temp);
	}
	return 0;
}

std* del_node(std* head, std* temp)//3.删除数据---------------------------------------------------------------------------------------------------------------------------------------
{
	int ID, i = 1, j = 1;
	std* phead = head, * qianhead = NULL, * zhonghead = NULL, * houhead = NULL;//保存使用
	std* pphead = head;
	std* bhead = head;//遍历使用

	if (head->next == NULL)
	{
		system("cls");
		printf("\t\t\t        名字        性别        年龄        学号\n");
		printf("\n\n\n\n\n\t\t\t\t\t此列表暂时没有数据!\n\n\n\n\n\n\n\n\n\t\t\t返回主菜单按“0”");
		scanf("%d", &j);
		if (j == 0)
		{
			xuanze(phead, temp);
		}
	}

	system("cls");
	printf("\t\t\t        名字        性别        年龄        学号\n");
	while (bhead->next != NULL)
	{
		bhead = bhead->next;
		printf("\t\t\t%10s   %10s   %10hd   %10hd\n", bhead->date.name, bhead->date.sex, bhead->date.age, bhead->date.ID);
	}

	printf("\t\t\t请输入删除该成员的学号:  ");//删除
	scanf("%d", &ID);



	while (head->next != NULL)
	{
		
		qianhead = head;

		head = head->next;
		zhonghead = head;

		if (zhonghead->next == NULL && zhonghead->date.ID == ID)//当有一个值时
		{
			qianhead->next = NULL;
			free(zhonghead);
		}
			if (qianhead->next == NULL)
			{
				temp = qianhead;//当列表空了,就需要对其参数了
				del_node(phead, temp);
			}

		if (zhonghead->next != NULL)
		{
			head = head->next;
			houhead = head;
		}
			if (zhonghead->date.ID == ID  )
			{
				qianhead->next = houhead;
				free(zhonghead);
			}
			if (houhead->date.ID == ID && houhead->next == NULL)//当列表里面只有两个值时
			{
				zhonghead->next = NULL;
				free(houhead);
			}

		if (zhonghead->date.ID != ID)
		{
			printf("\t\t\t返回主菜单请按“0”,继续删除请按“1”:");
			scanf("%d", &i);

			if (i == 0)
			{
				xuanze(phead, temp);
			}
			else
			{
				del_node(phead, temp);
			}
		}
	}
	return phead;
}
std* xiugaishuju(std* head, std* temp)//4.修改数据----------------------------------------------------------------------------------------------
{           
	int ID, i;                  
	std* node = (std*)malloc(sizeof(std));
	
	std* whead = head;
	std* nhead = head;
	system("cls");
	printf("\t\t\t        名字        性别        年龄        学号\n");
	while (head->next != NULL)
	{
		head = head->next;
		printf("\t\t\t%10s   %10s   %10hd   %10hd\n", head->date.name, head->date.sex, head->date.age, head->date.ID);
	}

	printf("输入想修改的学号:\n");
	scanf("%d",&ID);

	printf("\t\t\t请输入即将修改的成员参数:\n");

	printf("\t\t\t             名字:");
	scanf("%s", node->date.name);

	printf("\t\t\t             性别:");
	scanf("%s", node->date.sex);

	printf("\t\t\t             年龄:");
	scanf("%hd", &node->date.age);

	while (whead->next)
	{
		whead = whead->next;
		if (whead->date.ID == ID)
		{
			strcpy(whead->date.name, node->date.name);
			strcpy(whead->date.sex, node->date.sex);
			whead->date.age, node->date.age;
			printf("\n\n\n\n\n\n\t\t\t\t\t\t\t\t修改成功!\n");
			printf("\t\t\t继续修改请按“1”,返回主菜单请按“0\n");
			scanf("%d", &i);
			if (i == 0)
			{
				xuanze(nhead, temp);
			}
			else
			{
				xiugaishuju(nhead, temp);
			}
		}
	}
	return head;
}
void findfanhui(std* head, std* temp)//5.查找数据----------------------------------------------------------------------------------------------------------------                                
{
	printf("返回主菜单. . .\n");
	system("pause");
	xuanze(head, temp);
 }
	 std* finddate(std* head, std* temp)
{
	int i;
	char sex[4];//性别
	int  age;    //年龄
	char name[9];//贵姓
	char fname[9];
	std* xnsb_head = head;
	i = findwode();

	if (head->next == NULL)
	{
		findfanhui(head,temp);
	}

	if (i == 1)
	{
		system("cls");
		printf("\t\t\t        名字        性别        年龄        学号\n");
		while (head->next != NULL)
		{
			head = head->next;
			printf("\t\t\t%10s   %10s   %10hd   %10hd\n", head->date.name, head->date.sex, head->date.age, head->date.ID);
		}

		printf("请输入即将要查找的(性别):");
		scanf("%s",sex);
		printf("\t\t\t|-+性别--------名字--------年龄---------学号--|\n");
		while (xnsb_head->next)
		{
			xnsb_head = xnsb_head->next;
			if (strcmp(xnsb_head->date.sex,sex) == 0)
			{
				printf("\t\t\t    %s           %s           %d            %d  \n", xnsb_head->date.sex, xnsb_head->date.name, xnsb_head->date.age, xnsb_head->date.ID);
				findfanhui(head,temp);
			}
		}
	}
	if (i == 2)
	{
		system("cls");
		printf("\t\t\t        名字        性别        年龄        学号\n");
		while (head->next != NULL)
		{
			head = head->next;
			printf("\t\t\t%10s   %10s   %10hd   %10hd\n", head->date.name, head->date.sex, head->date.age, head->date.ID);
		}

		printf("请输入即将要查找的(年龄):");
		scanf("%d", &age);
		printf("\t\t\t|-+年龄--------名字--------性别---------学号--|\n");
		while (xnsb_head->next)
		{
			xnsb_head = xnsb_head->next;
			if (xnsb_head->date.age == age)
			{
				printf("\t\t\t    %d           %s           %s            %d  \n", xnsb_head->date.age, xnsb_head->date.name, xnsb_head->date.sex, xnsb_head->date.ID);
				findfanhui(head, temp);
			}
		}
	}
	if (i == 3)
	{
		system("cls");
		printf("\t\t\t        名字        性别        年龄        学号\n");
		while (head->next != NULL)
		{
			head = head->next;
			printf("\t\t\t%10s   %10s   %10hd   %10hd\n", head->date.name, head->date.sex, head->date.age, head->date.ID);
		}

		printf("请输入即将要查找的(贵姓):");
		scanf("%s", name);
		printf("\t\t\t|-+名字--------性别--------年龄---------学号--|\n");
		while (xnsb_head->next)
		{
			xnsb_head = xnsb_head->next;//一直指

			strncpy(fname, xnsb_head->date.name,sizeof(fname));
			fname[2] = '\0';
			printf("%s", fname);
			if (strcmp(fname,name) == 0)
			{
				printf("\t\t\t    %s           %s           %d            %d  \n", xnsb_head->date.name, xnsb_head->date.sex, xnsb_head->date.age, xnsb_head->date.ID);
				findfanhui(head, temp);
			}
		}
	}
	return 0;
}



std* display(std* head, std* temp)//6.查看数据----------------------------------------------------------------------------------------------------------------------------
{
	std* phead = head;
	if (head->next == NULL)
	{
		printf("\t\t\t        名字        性别        年龄        学号\n");
		printf("\t\t\t                    暂无数据                    \n");
	}
	else
	{
		printf("\t\t\t        名字        性别        年龄        学号\n");
		while (head->next != NULL)
		{
			head = head->next;
			printf("\t\t\t%10s   %10s   %10hd   %10hd\n", head->date.name, head->date.sex, head->date.age, head->date.ID);
		}
	}
	if (head->next == NULL)
	{
		system("pause");
		xuanze(phead, temp);
	}
	
	return phead;
}

void GG(std* head)//7.结束程序-----------------------------------------------------------------------------------------------------------------
{
	std* phead = head;
	while (head->next)
	{
		head = head->next;
		free(head);
	}
	free(phead);
	printf("\n\n\n\n\n\n\n\n\n\t\t\t\t\t完结\n");
}

std* xuanze(std* head, std* temp)
{
	system("cls");
	int a;
	jiemian();
	printf("\t\t\t请输入:");
	scanf("%d", &a);
	if (a == 1)
	{
		system("cls");
		printf("\t\t\t根据对应的提示,输入准备录入的数据\n");
		temp = _1scanfnode(head, temp);
	}
	if (a == 2)
	{
		system("cls");
		head = charu(head, temp);
	}
	if (a == 3)
	{
		system("cls");
		head = del_node(head, temp);
	}
	if (a == 4)
	{
		system("cls");
		head = xiugaishuju(head, temp);
	}
	if (a == 5)
	{
		system("cls");
		finddate(head,temp);
	}
	if (a == 6)
	{
		system("cls");
		head = display(head, temp);
	}
	if (a == 7)
	{
		system("cls");
		GG(head);
	}
	else
		while (a != 1 && a != 2 && a != 3 && a != 4 && a != 5 && a != 6 && a != 7)
		{
			printf("\t\t\t输入有误请重新输入!\n");
			scanf("%d", &a);
		}
	return temp;
}

int main(void)
{
	std* temp, * head = inithead();

	temp = head;

	temp = xuanze(head, temp);

	system("pause");
	return 0;
}

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值