🎈此程序與我的《線性表順序存儲——學生成績管理系統》實現功能一致這裏不再放置程序運行截圖
#include <iostream.h>
#include <stdlib.h>
#define NULL 0
/*
學生管理系統2.0 版
鏈式存儲
by 文麗
*/
typedef struct LNode
{
int data;
struct LNode * next;
}LinkNode;
// 菜單
void Menu(void);
//
void GetMessage(int a[],int n);
//初始化線性表
void InitList(LinkNode *&L);
//將信息存放到表中
void CreateListR(LinkNode *&L,int a[],int n);
//判空
int ListEmpty(LinkNode * L);
//輸出
void DispList(LinkNode *L);
//長度
int ListLength(LinkNode * L);
//取
int GetNum(LinkNode * L);
void GetElem(LinkNode *L,int i, int &e);
//查找
void locateElam(LinkNode *L,int e);
// 插入
void ListInsert(LinkNode * &L,int i,int e);
//刪除
void ListDelete(LinkNode * &L,int i);
//銷燬表
void DestroyList(LinkNode *&L);
int main()
{
LinkNode *L; //定義實參 L
int a[25];//定義實參數組 a,個數自己定
int x = 25;
int ch;
int num;
InitList(L);
do
{
Menu();
while(cin >> ch )
{
if(ch >= 1 && ch <= 9)
break;
else
cout << "您輸入不合法請重新輸入:";
}
switch(ch)
{
case 1: //錄入學生成績
system("cls");
GetMessage(a,x);
CreateListR(L,a,x);
cout << "錄入成功!\n";
break;
case 2: //輸出每位學生的成績
system("cls");
cout << "學生成績如下:\n";
DispList(L);
break;
case 3: { //統計人數
system("cls");
if (ListEmpty(L) != 0)
cout << "沒有學生!";
else
cout<<" 現統計人數: "<<ListLength(L)<<endl;
}
break;
case 4:{ //查詢成績
system("cls");
cout << "請輸入您要查詢學生成績的編號:";
num = GetNum(L);
GetElem(L,num,x);
cout << "該學生成績爲:" << x <<endl;
}
break;
case 5: //查詢不及格同學
system("cls");
cout << "不及格人數如下:\n";
locateElam(L,60);
break;
case 6: {//添加學生成績
system("cls");
cout << "請輸入要添加學生的編號以及成績:";
num=GetNum(L);
cin >> x;
ListInsert(L,num,x);
cout << "添加學生成功!\n";
cout<<" 現統計人數: "<<ListLength(L)<<endl;
}
break;
case 7: {//刪除學生成績
system("cls");
cout << "請輸入要刪除學生的編號:";
num=GetNum(L);
ListDelete(L,num);
cout << "刪除學生成功!\n";
cout<<" 現統計人數: "<<ListLength(L)<<endl;
}
break;
case 8: //銷燬成績表
system("cls");
DestroyList(L);
cout << "該表銷燬成功!";
break;
case 9: //退出
system("cls");
cout << "謝謝你的使用,再見!\n";
break;
}
}while(ch >= 1 && ch <= 8);
return 0;
}
void Menu(void)
{
cout << "\n 歡迎登陸學生成績管理系統\n\n";
cout << "(1)錄入學生成績 \n" ;
cout << "(2)輸出每位學生的成績 \n" ;
cout << "(3)統計人數 \n" ;
cout << "(4)查詢成績 \n" ;
cout << "(5)查詢不及格同學\n" ;
cout << "(6)添加學生成績 \n" ;
cout << "(7)刪除學生成績 \n" ;
cout << "(8)銷燬成績表\n" ;
cout << "(9)退出\n\n" ;
cout << "請輸入您的選項:" ;
}
//初始化線性表
void InitList(LinkNode * &L)
{
L = (LinkNode *) malloc (sizeof(LinkNode));
L -> next = NULL;
}
void GetMessage(int a[],int n)
{
int i = 0;
cout << "請分別輸入"<< n << "位學生的學生成績:\n" ;
for(i = 0; i < 25 ;i ++ )
cin >> a[i];
}
//建立 頭插法
void CreateListR(LinkNode * &L,int a[],int n)
{
int i;
LinkNode *s,*r;
InitList(L);
r = L;
for(i = 0; i < n ; i++)
{
s = (LinkNode *) malloc (sizeof(LinkNode));
s -> data = a[i];
r -> next = s;
r = s;
}
r -> next = NULL;
}
//判空
int ListEmpty(LinkNode * L)
{
return (L -> next == NULL);
}
//輸出
void DispList(LinkNode *L)
{
int i = 0;
LinkNode *p = L -> next;
if(ListEmpty(L))
{
cout << "該表爲空!";
return;
}
while(p != NULL)
{
if(i % 5 == 0)
cout << endl; //每輸出5個換一行
cout << " " << p ->data ;
p = p ->next;
i++;
}
}
//長度
int ListLength(LinkNode * L)
{
int n = 0;
LinkNode *p = L;
while(p -> next != NULL)
{
n++;
p = p ->next;
}
return (n);
}
int GetNum(LinkNode * L)
{
int num;
while(cin >> num)
{
if(num > 1 && num <= ListLength(L))
break;
cout << "請輸入一個小於" << ListLength(L) << "的同學編號 :";
}
return num;
}
//取
void GetElem(LinkNode * L,int i, int &e)
{
int j = 1;
LinkNode *p = L;
if(i <= 0)
return;
while(j <= i && p != NULL)
{
j ++;
p = p ->next;
}
if(p == NULL)
return;
e = p ->data;
}
//查找
void locateElam(LinkNode * L,int e)
{
int i;
i = 0;
LinkNode *p = L->next;
cout << "編號\t成績\n";
while(p != NULL)
{
if(p ->data < e)
{
cout << i+1 <<"\t" << p ->data << "\n";
}
p = p ->next;
i++;
}
}
// 插入 在L中第i個位置插入e
void ListInsert(LinkNode * &L,int i,int e)
{
int j = 1;
LinkNode *p = L, *s;
if(i <= 0)
return ;
while(j <= i - 1 && p != NULL )
{
j ++;
p = p ->next;
}
if(p == NULL)
return ;
else
{
s = (LinkNode *) malloc (sizeof(LinkNode));
s -> data= e;
s -> next = p -> next;
p-> next = s;
return ;
}
}
//刪除L中第i個元素
void ListDelete(LinkNode *&L,int i)
{
int j = 1;
LinkNode *p = L, *q;
if(i <= 0)
return ;
while(j <= i - 1 && p != NULL )
{
j ++;
p = p ->next;
}
if(p == NULL)
return ;
else
{
q = p ->next;
if(q == NULL)
return;
//e = q -> data;
p -> next = q -> next;
free(q);
return ;
}
}
//刪除表
void DestroyList(LinkNode * &L)
{
LinkNode *pre = L, *p = L ->next;
while(p != NULL)
{
free(pre);
pre = p;
p = pre ->next;
}
free(pre);
}