實驗要求
- 對學生的基本信息用鏈表進行處理,基本信息包括:學號、姓名、性別、 年齡、C 語言的成績。
- 要求:
(1)建立鏈表,輸入一個學生的基本信息,插入鏈表,插入要求爲鏈表必 須按照學號從小到大的順序進行排列。
(2)可以在鏈表中刪除某一學生的信息即輸入一個學生的學號,查找到該 節點信息作刪除。
(3)用函數封裝每一個操作。
首先,定義一個結構體:
struct xxx
{
long long xh;//學號,不考慮存在非數字,還有我知道最大位數,別槓
char xm[10],xb;//定義姓名,性別,這裏只考慮最高4箇中文字符作爲姓名,性別用M來代表男,W來代表女,O來代表其他QAQ;
int age,cyy;//年齡和c語言成績
struct xxx *next;//指向下一個
struct xxx *last;//指向上一個
};
然後考慮到要求(3)
我們定義了
一個菜單
輸入函數
刪除函數
輸出函數
退出函數
嗯,還有主函數
void menu();
void sr();
void sc();
void bs();
void exit();
先來看一下主函數
int main()
{
int op;
t=NULL;
w=NULL;
while(f){
system("cls");//清空
menu();
bs();//輸出函數
cin>>op;
switch(op)
{
case 1:
cout<<"依次輸入學號、姓名、性別、 年齡、C 語言的成績"<<endl;
sr();
break;
case 2:
sc();
break;
case 3:
exit();
break;
}
system("pause");//暫停
}
return 0;
}
菜單在這裏
void menu()
{//嗯,就是一串文字,增加交互能力
cout<<"1:輸入一個學生的基本信息"<<endl<<"2:輸入一個學生的學號刪除該學生的信息"<<endl<<"3:退出"<<endl;
cout<<"如果要退出,請按3和回車鍵,不要點X!!!!!!!!!!!!!"<<endl;
//鏈表採用動態分配,直接退出可能會存在安全隱患
}
輸入函數
void sr()
{//確實有點長,但是主要是爲了增加交互性,而且這裏用的是雙向鏈表,雖然有點多餘
struct xxx *p,*q;
p=(struct xxx *)malloc(sizeof(struct xxx));
cout<<"輸入學號"<<endl;
cin>>(p->xh);
cout<<"輸入姓名"<<endl;
cin>>(p->xm);
cout<<"輸入性別"<<endl;
cin>>(p->xb);
cout<<"輸入年齡"<<endl;
cin>>(p->age);
cout<<"輸入c語言成績"<<endl;
cin>>(p->cyy);
if(t==NULL){
t=p;
w=p;
p->next=NULL;
p->last=NULL;
}
else
{
q=t;
if(p->cyy>q->cyy){
p->next=q;
t=p;
q->last=p;
}
else{
while(q->next!=NULL&&p->cyy<q->next->cyy)
q=q->next;
if(q->next!=NULL){
q->next=p;
q->last=p->last;
p->last=q;
}
else{
p->next=NULL;
p->last=q;
q->next=p;
w=p;
}
}
}
}
輸出函數
void bs()
{
int i=0;
cout<<"--------------------------------------------------------------------------"<<endl;
if(t==NULL)cout<<"暫無學生信息"<<endl;
else{
struct xxx *p;
p=t;
cout<<"名次:"<<++i<<" "<<"學號:"<<p->xh<<" "<<"姓名:"<<p->xm<<" "<<"性別:"<<p->xb<<" "<<"年齡:"<<p->age<<" "<<"c語言成績:"<<p->cyy<<endl;
while(p->next!=NULL){
cout<<"名次:"<<++i<<" "<<"學號:"<<p->next->xh<<" "<<"姓名:"<<p->next->xm<<" "<<"性別:"<<p->next->xb<<" "<<"年齡:"<<p->next->age<<" "<<"c語言成績:"<<p->next->cyy<<endl;
p=p->next;
}
}
cout<<"--------------------------------------------------------------------------"<<endl;
}
刪除函數
void sc()
{
if(t==NULL)cout<<"沒有數據,你看不到嗎???"<<endl;
else
{
long long v;
cout<<"輸入要刪除的學生的學號:"<<endl;
cin>>v;
struct xxx *p;
p=t;
if(p->xh==v){
t=p->next;
free(p);
}
else
{
while(p->next!=NULL&&(p->next->xh)!=v)
p=p->next;
if(p->next==NULL)cout<<"根本沒有這個人"<<endl;
else{
if(p->next->next!=NULL)
{
p->next->next->last=p;
p=p->next;
p->last->next=p->next;
free(p);
}
else{
p=p->next;
p->last->next=NULL;
free(p);//記得每次去節點的時候要釋放空間
}
}
}
}
}
退出函數
void exit()//主要是爲了清空所有動態申請的地址,釋放空間
{
if(t==NULL)f=0;
else{
struct xxx *p;
p=t;
while(p->next!=NULL)
{
t=t->next;
free(p);
p=t;
}
free(p);
f=0;
}
}
完整代碼
#include<iostream>
#include<stdlib.h>
using namespace std;
struct xxx
{
long long xh;
char xm[10],xb;
int age,cyy;
struct xxx *next;
struct xxx *last;
};
struct xxx *t;
struct xxx *w;
int f=1;
void menu();
void sr();
void sc();
void bs();
void exit();
int main()
{
int op;
t=NULL;
w=NULL;
while(f){
system("cls");
menu();
bs();
cin>>op;
switch(op)
{
case 1:
cout<<"依次輸入學號、姓名、性別、 年齡、C 語言的成績"<<endl;
sr();
break;
case 2:
sc();
break;
case 3:
exit();
break;
}
system("pause");
}
return 0;
}
void menu()
{
cout<<"1:輸入一個學生的基本信息"<<endl<<"2:輸入一個學生的學號刪除該學生的信息"<<endl<<"3:退出"<<endl;
cout<<"如果要退出,請按3和回車鍵,不要點X!!!!!!!!!!!!!"<<endl;
}
void bs()
{
int i=0;
cout<<"--------------------------------------------------------------------------"<<endl;
if(t==NULL)cout<<"暫無學生信息"<<endl;
else{
struct xxx *p;
p=t;
cout<<"名次:"<<++i<<" "<<"學號:"<<p->xh<<" "<<"姓名:"<<p->xm<<" "<<"性別:"<<p->xb<<" "<<"年齡:"<<p->age<<" "<<"c語言成績:"<<p->cyy<<endl;
while(p->next!=NULL){
cout<<"名次:"<<++i<<" "<<"學號:"<<p->next->xh<<" "<<"姓名:"<<p->next->xm<<" "<<"性別:"<<p->next->xb<<" "<<"年齡:"<<p->next->age<<" "<<"c語言成績:"<<p->next->cyy<<endl;
p=p->next;
}
}
cout<<"--------------------------------------------------------------------------"<<endl;
}
void sr()
{
struct xxx *p,*q;
p=(struct xxx *)malloc(sizeof(struct xxx));
cout<<"輸入學號"<<endl;
cin>>(p->xh);
cout<<"輸入姓名"<<endl;
cin>>(p->xm);
cout<<"輸入性別"<<endl;
cin>>(p->xb);
cout<<"輸入年齡"<<endl;
cin>>(p->age);
cout<<"輸入c語言成績"<<endl;
cin>>(p->cyy);
if(t==NULL){
t=p;
w=p;
p->next=NULL;
p->last=NULL;
}
else
{
q=t;
if(p->cyy>q->cyy){
p->next=q;
t=p;
q->last=p;
}
else{
while(q->next!=NULL&&p->cyy<q->next->cyy)
q=q->next;
if(q->next!=NULL){
q->next=p;
q->last=p->last;
p->last=q;
}
else{
p->next=NULL;
p->last=q;
q->next=p;
w=p;
}
}
}
}
void sc()
{
if(t==NULL)cout<<"沒有數據,你看不到嗎???"<<endl;
else
{
long long v;
cout<<"輸入要刪除的學生的學號:"<<endl;
cin>>v;
struct xxx *p;
p=t;
if(p->xh==v){
t=p->next;
free(p);
}
else
{
while(p->next!=NULL&&(p->next->xh)!=v)
p=p->next;
if(p->next==NULL)cout<<"根本沒有這個人"<<endl;
else{
if(p->next->next!=NULL)
{
p->next->next->last=p;
p=p->next;
p->last->next=p->next;
free(p);
}
else{
p=p->next;
p->last->next=NULL;
free(p);
}
}
}
}
}
void exit()
{
if(t==NULL)f=0;
else{
struct xxx *p;
p=t;
while(p->next!=NULL)
{
t=t->next;
free(p);
p=t;
}
free(p);
f=0;
}
}