巩固了一下单链表的知识点,并运用单链表写了个简单的学生管理系统
实现功能:增、删、改、查
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[20];
int age;
};
//定义单链表
typedef struct SingleList
{
struct student mystudent;
struct SingleList *next;
}LIST,*LPLIST;
//创建带有表头的单链表
LPLIST CreatList()
{
//申请内存
LPLIST List = (LPLIST)malloc(sizeof(LIST));
//初始化基本数据成员
List->next = NULL;
return List;
}
//创建节点
LPLIST CreatNode(struct student mystudent)
{
//申请内存
LPLIST Node = (LPLIST)malloc(sizeof(LIST));
//初始化基本数据成员
strcpy((Node->mystudent).name,mystudent.name);
(Node->mystudent).age = mystudent.age;
Node->next = NULL;
return Node;
}
//表头插入
void InsertHead(LPLIST List, struct student mystudent)
{
LPLIST pNode = CreatNode(mystudent);
pNode->next = List->next;
List->next = pNode;
}
//表尾插入
void InsertTail(LPLIST List, struct student mystudent)
{
LPLIST pNode = CreatNode(mystudent);
LPLIST pNext = List;
while (pNext->next != NULL)
{
pNext = pNext ->next;
}
pNext ->next = pNode;
}
//指定位置插入
void InsertApption(LPLIST List, struct student mystudent,char *name)
{
LPLIST pNode = CreatNode(mystudent);
LPLIST pNext1 = List;
LPLIST pNext2 = List->next;
while (strcmp((pNext2->mystudent).name , name))
{
pNext1 = pNext2;
pNext2 = pNext2->next;
if (pNext2 == NULL)
{
printf("未找到指定位置\n");
return;
}
}
pNext1->next = pNode;
pNode->next = pNext2;
}
//判断是否为空
int IsEmpty(LPLIST List)
{
if (List->next == NULL)
return 0;
return 1;
}
//删除表头
void DeletHead(LPLIST List)
{
if (IsEmpty(List))
{
LPLIST pNext = List->next;
List->next = pNext->next;
return ;
}
printf("信息为空,无法删除");
}
//删除表尾
void DeletTail(LPLIST List)
{
if (IsEmpty(List))
{
LPLIST pNext1 = List;
LPLIST pNext2 = List->next;
while (pNext2->next != NULL)
{
pNext1 = pNext2;
pNext2 = pNext2->next;
}
pNext1->next = NULL;
return ;
}
printf("信息为空,无法删除\n");
}
//指定位置删除
void DeletApption(LPLIST List, char *name)
{
if (IsEmpty(List))
{
LPLIST pNext1 = List;
LPLIST pNext2 = List->next;
while (strcmp((pNext2->mystudent).name, name))
{
pNext1 = pNext2;
pNext2 = pNext2->next;
if (pNext2 == NULL)
{
printf("未找到指定删除位置\n");
return;
}
}
pNext1->next = pNext2->next;
return ;
}
printf("信息为空,无法删除\n");
}
//打印函数
void PrintList(LPLIST List)
{
LPLIST p = List->next;
while (p != NULL)
{
printf("name:%s\tage:%d\n",(p->mystudent).name,(p->mystudent).age);
p = p->next;
}
printf("\n");
}
//菜单
void Menu()
{
printf(" ===========================\n");
printf(" 1.表头插入\n");
printf(" 2.表尾插入\n");
printf(" 3.指定位置插入\n");
printf(" 4.表头删除\n");
printf(" 5.表尾删除\n");
printf(" 6.指定位置删除\n");
printf(" 7.打印信息\n");
printf(" ===========================\n");
}
int main()
{
LPLIST List = CreatList();//创建单链表
printf("\n+++++++++++++++STUDENTSYSTEM+++++++++++++++\n\n");
while (1)
{
Menu();
struct student mystudent;
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("请输入学生姓名和年龄:\n");
scanf("%s%d",mystudent.name,&mystudent.age);
InsertHead(List,mystudent);
break;
case 2:
printf("请输入学生姓名和年龄:\n");
scanf("%s%d", mystudent.name, &mystudent.age);
InsertTail(List, mystudent);
break;
case 3:
printf("请输入学生姓名和年龄,及要插入的位置的名字:\n");
char name2[20];
scanf("%s%d%s", mystudent.name, &mystudent.age,name2);
InsertApption(List, mystudent,name2);
break;
case 4:
DeletHead(List);
break;
case 5:
DeletTail(List);
break;
case 6:
printf("请输入要删除学生的姓名:\n");
char name1[20];
scanf("%s", name1);
DeletApption(List, name1);
break;
case 7:
PrintList(List);
break;
default:
printf("选择错误\n");
break;
}
}
system("pause");
return 0;
}