單鏈表做學生信息管理系統(tǒng)
修改前的代碼(c語言):
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
//首先把'學生'的數(shù)據(jù)抽象出來
struct student {
char name[20];
char num[20];
int math;//數(shù)學成績
int english; //英語成績
int py;//python成績
int sum;//總分把都加起來。sum=math+english+python;
//所有關(guān)于數(shù)據(jù)的地方都要修改
};
//有頭鏈表先做表頭再插入
struct Node {
int data;
struct Node* next;
};//創(chuàng)建結(jié)構(gòu)體
struct Node* list; //需要一個指針存儲整個鏈表
struct Node* createHead() {
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//用指針表示結(jié)點,首先要把指針變?yōu)樽兞浚醋鰟討B(tài)內(nèi)存申請
assert(headNode); //斷言函數(shù),看他的內(nèi)存是否申請成功;判斷當前指針是否為空,如果為空就會引發(fā)斷點
headNode->next = NULL;
return headNode;
//創(chuàng)建鏈表,表頭的數(shù)據(jù)不做初始化(headNode->data=1,表頭初始化的個人理解),只初始化指針
}
//創(chuàng)建結(jié)點。插入數(shù)據(jù)之前首先要創(chuàng)建結(jié)點
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
assert(newNode); //判斷newNode內(nèi)存是否分配成功
newNode->data = 1;
newNode->next = NULL;
return newNode;
}
//插入數(shù)據(jù)
void insertData(struct Node* headNode,int data)
{
struct Node* newNode = createNode(data);?
newNode->next = headNode->next;
headNode->next = newNode;
}
//打印鏈表
void printList(struct Node* headNode)? //括號里用頭結(jié)點代表整個鏈表
{
//有頭鏈表第一個結(jié)構(gòu)體沒有數(shù)據(jù)。第一個指針指向第二個結(jié)點
struct Node* pmove = headNode->next;
while (pmove != NULL)
{
printf("%d\t", pmove->data);
pmove = pmove->next; //打印完,pmove走向下一個
}
}
//刪除數(shù)據(jù)
void deleteData(struct Node* headNode, int posData) {
//刪除結(jié)點,直到其前后結(jié)點的名字,將前一個結(jié)點連到后一個結(jié)點,自己當燈泡
struct Node* preNode = headNode;
struct Node* curNode = headNode->next;
while (curNode != NULL && curNode->data != posData)
{
preNode = curNode;
curNode = preNode->next;//前驅(qū)結(jié)點走向當前結(jié)點,被踢的當前結(jié)點走向當前結(jié)點的下一個
}
if (curNode == NULL) {
printf("刪除失敗!沒有找到指定數(shù)據(jù)!\n");
}
else
{
preNode->next = curNode->next;
free(curNode);
}
}
//查找數(shù)據(jù)
struct Node* searchData(struct Node* headNode,int posData)
{
struct Node* pmove = headNode->next;
while (pmove != NULL? && pmoce->data!=posData)
{
pmove = pmove->next; //pmove走向下一個位置
}
return pmove;//返回pmove,繼續(xù)
}
void makeMenu() {
printf("----------[鏈式學生信息管理系統(tǒng)]---------");
printf("\n\t\t0.退出功能\n");?
printf("\t\t1.錄入功能\n");
printf("\t\t2.瀏覽功能\n");
printf("\t\t3.查找功能\n");
printf("\t\t4.修改功能\n");
printf("\t\t5.刪除功能\n");
printf("------------------------");
printf("請輸入(0~5):");
}
void keyDown(){
int userkey = 0;
printf("請輸入你需要的功能模塊:");
scanf_s("%d", &userkey); //char d 20;等同于canf_s("%s",d,20);
switch (userkey)
{
case 0:
printf("-------[退出功能]-------");
system("pause");
exit(0);//用一個地址把整個程序關(guān)掉;
break;
case 1:
printf("-------[錄入功能]-------");
break;
? ??
case 2:
printf("-------[瀏覽功能]-------");
break;
case 3:
printf("-------[查找功能]-------");
break;
case 4:
printf("-------[修改功能]-------");
break;
case 5:
printf("-------[刪除功能]-------");
break;
default:
printf("輸入錯誤!沒有該功能、n");
break;
}
}
int main() {
list = createHead(); //初始化
insertData(list,1); //pmove在第一個結(jié)點
insertData(list,666); //pmove走向第二個結(jié)點
printList(list);
printf("\n");
while(1)
{
makeMenu();
keyDown();
system("pause");
system("cls");
return 0;
}
}