结构(链表)

0 头文件及全局变量

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

1 菜单栏

void menu()
{
	printf("\t\t****Welcome to student Administration System****\n");
	printf("\t\t****************************************\n");
	printf("\t\t*1)Display all student information********\t\n");
	printf("\t\t*2)Delete student information*************\t\n");
	printf("\t\t*3)Add Student information****************\t\n");
	printf("\t\t*4)Modify student information*************\t\n");
	printf("\t\t*5)Query student information**************\t\n");
	printf("\t\t*6)exit                     **************\t\n");
	printf("\t\t*7)clear                    **************\t\n");
	printf("\t\t*8)sort                     **************\t\n");
	printf("\t\t****************************************\n");
}

2 存储数据:结构体封装

typedef struct Stu_
{
	int id;
	char name[20];
}Stu;
typedef struct Node_
{
	Stu stu;
	int size;
	struct Node_* next;
}Node;

3 基本功能 :

3.0初始化头结点
Node* initHead()
{
	Node* head = (Node*)malloc(sizeof(Node));
	head->next = NULL;

	head->size = 0;

	return head;
}
3.1显示所有信息
void show(Node* head)
{
	Node* pMove = head->next;

	while (pMove)
	{
		printf("ID:%d\tName:%s\n", pMove->stu.id, pMove->stu.name);
		pMove = pMove->next;
	}
}
3.2添加一条信息
void addNew(Node* head)
{
	Node* newStu = (Node*)malloc(sizeof(Node));
	printf("请输入该学生的名字:");
	scanf("%s", newStu->stu.name);
	//putchar('\n');
	printf("请输入该学生的学号:");
	scanf("%d", &newStu->stu.id);

	newStu->next = head->next;
	head->next = newStu;

	head->size++;
}
3.3修改某一个数据
void modify(Node* head)
{
	Node* pMove = head->next;
	int id;
	int flag=0;
	printf("请输入你想修改的学生的学号:");
	scanf("%d", &id);
	while (pMove)
	{
		if (pMove->stu.id == id)
		{
			flag = 1;
			printf("找到了!!!\n");
			printf("请输入你修改后的学号:");
			scanf("%d", &pMove->stu.id);
			printf("修改成功!!!\n");
		}
		pMove = pMove->next;
	}

	if (flag == 0)
	{
		printf("抱歉,没找到!\n");
	}

}
3.4删除一条信息
void deleteStu(Node* head)
{
	Node* pre = head;
	int id;
	int flag = 0;
	printf("请输入你想删除的学生的学号:");
	scanf("%d", &id);
	while (pre->next)
	{
		if (pre->next->stu.id == id)
		{
			flag = 1;
			printf("查到此人!!!]\n");
			Node* deleteD=pre->next;
			pre->next = deleteD->next;
			free(deleteD);
			deleteD = NULL;
			printf("删除成功!!!\n");

			head->size--;

			return;
		}
		pre = pre->next;
	}
	if (flag == 0)
	{
		printf("抱歉,没找到啊!!!\n");
	}
}
3.5查找学生信息
void find(Node* head)
{
	int id;
	int flag = 0;
	Node* pMove = head->next;
	printf("请输入你想要查找的学生的学号:");
	scanf("%d", &id);
	while (pMove)
	{
		if (pMove->stu.id == id)
		{
			flag = 1;
			printf("找到了!!!\n");
			printf("该学生的信息如下:\n");
			printf("ID:%d\tName:%s\n", pMove->stu.id, pMove->stu.name);
			return;
		}
		pMove = pMove->next;
	}
	if (flag == 0)
	{
		printf("对不起,查无此人!!!\n");
	}
}

4 扩展

4.1 排序(链表中结点排序)
void sort(Node* head)
{
	Node* pre;
	Node* pMove;
	Node* r;
	Node* temp;
	int flag = 1;
	while (flag)
	{
		pre = head;
		pMove = head->next;
		flag = 0;
		while (pMove)
		{
			r = pMove->next;
			if (!r) 
			{
				break;
			}
			else if (pMove->stu.id < r->stu.id)
			{
				temp = r->next;
				pMove->next = temp;
				r->next = pMove;
				pre->next = r;
				pre = r;
				pMove = pre->next;
				r = pMove->next;
				flag = 1;
			}
			else 
			{
				pre = pMove;
				pMove = pre->next;
			}
		}
	}
	printf("排序成功!!!\n");
}

5 主函数

int main()
{
	Node* head = initHead();
	menu();

	int select;

	while (1)
	{
		printf("请输入数字来操作:");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			show(head);
			break;
		case 2:
			deleteStu(head);
			break;
		case 3:
			addNew(head);
			printf("当前学生人数为%d;\n", head->size);
			break;
		case 4:
			modify(head);
			break;
		case 5:
			find(head);
			break;
		case 6:
			exit(0);
		case 7:
			system("cls");
			menu();
			break;
		case 8:
			sort(head);
			break;
		default:
			break;
		}
	}
	return 0;
}