#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10000 //順序表可能達到的最大長度
#define ERROR 0
#define OVERFLOW -1
#define Status int
using namespace std;
typedef struct{
char name[10]; //姓名
int no; //學號
int score; //成績
}Student;
typedef struct{
Student *elem; //儲存空間的基地址
int length; //當前長度
}SqList; //順序表的結構類型爲 SqList
void input(Student &e){ //輸入學生信息
cout<<"請輸入學生姓名:"<<endl;
cin>>e.name ;
cout<<"請輸入學生學號:"<<endl;
cin>>e.no ;
cout<<"請輸入學生成績:"<<endl;
cin>>e.score ;
}
void output(Student &e){ //輸出學生信息
cout<<"姓名: " <<e.name<<" 學號: "<<e.no<<" 成績: "<<e.score<<endl;
}
Status InitList(SqList &L){ //初始化順序表
L.elem = new Student[MAXSIZE]; //爲表分配大小爲MAXSIZE的空間
if(!L.elem) exit(OVERFLOW); //分配失敗
L.length=0;
return 1;
}
Status GetStud(SqList &L,int i,Student &e){ //順序表取值
if(i<1||i>L.length)
return ERROR;
e = L.elem[i-1];
return 1;
}
Status ListInsert(SqList &L,int i,Student e){ //插入學生信息
if(i<1||(i>L.length+1) ) //判斷i值是否合法
return ERROR;
if(L.length==MAXSIZE) //判斷當前儲存空間是否已滿
return ERROR;
for(int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++;
return 1;
}
Status ListDelete(SqList &L,int i){ //刪除學生信息
if((i<=0)||(i>L.length))
return ERROR;
else
for(int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return 1;
}
Status LocatedStud(SqList &L, Student &e){ //查找學生信息
int i;
for(i=0;i<L.length;i++){
if(!strcmp(e.name, L.elem[i].name))
return i+1;
}
return 0;
}
int main(){
int n,x,y,num,len,chose;
char p;
Student e;
Student k;
SqList L;
cout<<"*********學生管理界面*********"<<endl;
cout<<" 1.創建鏈表"<<endl;
cout<<" 2.輸入學生信息"<<endl;
cout<<" 3.顯示所有學生信息"<<endl;
cout<<" 4.查找指定學生信息"<<endl;
cout<<" 5.插入學生信息"<<endl;
cout<<" 6.刪除指定學生信息"<<endl;
cout<<" 7.統計所有學生人數"<<endl;
cout<<" 8.退出界面"<<endl<<endl;
cout<<"*********請選擇功能*********"<<endl;
while(1){
cin>>n;
while(n<1||n>8)
{
cout<<"不存在該選擇,請重新輸入!"<<endl;
break;
}
switch(n){
case 1: //創建鏈表
if(InitList(L))
cout<<"鏈表創建成功!"<<endl;
else
cout<<"鏈表創建失敗!"<<endl;
break;
case 2: //輸入學生信息
cout<<"請輸入學生信息:"<<endl;
cout<<"請輸入學生人數:"<<endl;
cin>>num;
for(int i=0;i<num;i++){
L.length++;
input(L.elem[i]);
}
break;
case 3: //顯示所有學生信息
for(int i=0;i<L.length;i++){
output(L.elem[i]);
}
break;
case 4: //查找指定學生信息
cout<<"請輸入所要查找同學的姓名:"<<endl;
cin>>e.name ;
chose=LocatedStud(L,e);
if(chose!=0)
output(L.elem[chose-1]);
else
printf("未查詢到此學生信息\n");
break;
case 5: //插入學生信息
cout<<"請輸入要插入的位置:"<<endl;
cin>>x;
cout<<"請輸入插入學生的信息: "<<endl;
cout<<"姓名:"<<endl;
cin>>k.name ;
cout<<"學號:"<<endl;
cin>>k.no ;
cout<<"成績:"<<endl;
cin>>k.score ;
cout<<L.length<<endl;
if(ListInsert(L,x,k))
cout<<"學生信息插入成功"<<endl;
else
cout<<"學生信息插入失敗"<<endl;
break;
case 6: //刪除指定學生信息
cout<<"請輸入要刪除信息的學生位置:"<<endl;
cin>>y;
cout<<"如果確定刪除,請點Y!"<<endl;
cin>>p;
if(p=='Y'){
if(ListDelete(L,y)==1)
cout<<"刪除成功!"<<endl;
else
cout<<"刪除失敗!"<<endl;
}
else if(p!='Y')
cout<<"刪除失敗!"<<endl;
else
break;
case 7: //統計所有學生人數
cout<<"學生總人數爲:"<<L.length<<endl;
break;
case 8: //退出系統界面
exit(0);
}
}
return 0;
}
線性表的基本操作及其應用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.