C語言——單鏈表——學生管理系統
阿新 • • 發佈:2018-11-01
鞏固了一下單鏈表的知識點,並運用單鏈表寫了個簡單的學生管理系統
實現功能:增、刪、改、查
#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; }