學(xué)生信息管理系統(tǒng)之單鏈表實現(xiàn)
來源:程序員人生 發(fā)布時間:2016-10-04 11:40:34 閱讀次數(shù):3599次
用C++實現(xiàn)學(xué)生信息管理系統(tǒng),用到線性表的數(shù)據(jù)結(jié)構(gòu),用單鏈表實現(xiàn)。
首先看看甚么是單鏈表,有甚么特點:
1.單鏈表每一個節(jié)點存儲其數(shù)據(jù)和指向下1個節(jié)點的指針,即數(shù)據(jù)域和指針域,兩個邏輯上相鄰的元素存儲位置不1定相鄰。節(jié)點定義以下:
typedef struct LNode{
ElemType data;
LNode *next;
}LNode;
其中ElemType是學(xué)生基本信息的結(jié)構(gòu)體,其定義以下:
typedef struct StuInfo{
char stuID[10];
char name[15];
int age;
char city[15];
char tel[15];
}ElemType;
2.單鏈表可以由頭指針唯1肯定,遍歷單鏈表只能從頭開始。
3.每一個元素都有唯1先驅(qū)和唯1后繼,除頭節(jié)點和尾節(jié)點。
4.內(nèi)存動態(tài)分配,當(dāng)需要插入數(shù)據(jù)時申請1個相應(yīng)的空間便可,元素個數(shù)不受限制較為靈活。
5.基本操作:插入、刪除、輸出、修改、查找、排序等等。
由因而C++實現(xiàn),基本操作全部封裝在類的成員函數(shù)里面。
源碼以下:
/*---------------------------------/
/Date:2016-09⑴4-------------------/
/Function:Using single-list to achieve the management of students/
/From:<<Data Struct Examples>>-----/
/Author:---------------------------/
/---------------------------------*/
#include <iostream>
#include <iomanip>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef struct StuInfo{
char stuID[10];
char name[15];
int age;
char city[15];
char tel[15];
}ElemType;
typedef struct LNode{
ElemType data;
LNode *next;
}LNode;
class CLinkList{
private:
LNode *head;
public:
CLinkList();
virtual ~CLinkList();
bool IsListEmpty();
void ClearList();
void CreatList();
int GetListLength();
LNode* LocateElem(ElemType e);
LNode* LocateElem(int pos);
void InputStuInfo(ElemType &e);
bool ListInsert(ElemType &e,int i);
bool UpdateList(const ElemType &e,ElemType e1);
LNode *GetElem(int pos);
void OutputList();
bool ListDelete(ElemType e,int pos);
};
//構(gòu)造1個只有頭節(jié)點的空鏈表
CLinkList::CLinkList(){
head=new LNode;
head->next=NULL;
}
//析構(gòu)函數(shù)清空鏈表
CLinkList::~CLinkList(){
LNode *p=head->next,*q;
while(p){
q=p->next;
delete p;
p=q;
}
delete head;
}
int CLinkList::GetListLength(){
LNode *p;
p=head->next; //指針p指向頭節(jié)點的后繼節(jié)點
int i=0;
while(p){ //p不為空則計數(shù)器累加
i++;
p=p->next;
}
return i;
}
<span style="font-family: Arial, Helvetica, sans-serif;">void CLinkList::OutputList(){</span>
LNode *p=head->next;
if(p==NULL) cout<<"沒有信息!"<<endl;
else
cout<<setw(15)<<"學(xué)號"<<setw(15)<<"姓名"<<setw(15)<<"年齡"<<setw(15)<<"生源地"<<setw(15)<<"聯(lián)系電話"<<endl;
while(p){
cout<<setw(15)<<p->data.stuID<<setw(15)<<p->data.name<<setw(15)<<p->data.age<<setw(15)<<p->data.city<<setw(15)<<p->data.tel<<endl;
p=p->next;
}
}
bool CLinkList::IsListEmpty(){
if(GetListLength()==0) return true;
else
return false;
}
void CLinkList::ClearList(){
LNode *p=head->next,*q;
while(p){
q=p->next;
delete p;
p=q;
}
head->next=NULL; //得到帶頭節(jié)點的空鏈表
}
void CLinkList::CreatList(){
LNode *s;
ElemType e;
bool judge;
judge=IsListEmpty();
if(judge==false)
ClearList();
cout<<"輸入學(xué)號為!時結(jié)束!"<<endl;
cout<<"輸入學(xué)生學(xué)號:";
cin>>e.stuID;
while(strcmp(e.stuID,"!")){
cout<<"輸入學(xué)生姓名:";
cin>>e.name;
cout<<"輸入學(xué)生年齡:";
cin>>e.age;
cout<<"輸入學(xué)生源地:";
cin>>e.city;
cout<<"輸入聯(lián)系電話:";
cin>>e.tel;
cout<<endl;
s=new LNode;
s->data=e;
s->next=head->next;
head->next=s;
cout<<"輸入學(xué)生學(xué)號:";
cin>>e.stuID;
}
cout<<"鏈表建成"<<endl;
}
bool CLinkList::ListInsert(ElemType &e,int i){
int j=1;
LNode *s,*p;
s=new LNode; //建立1個待插入的節(jié)點s
s->data=e;
p=head;
while(j<i && p->next!=NULL){
p=p->next;
j++;
}
if(j==i){
s->next=p->next;
p->next=s;
return true;
}
else
return false;
}
bool CLinkList::ListDelete(ElemType e,int i){
int j=1;
LNode *p,*q;
q=head;
p=q->next;
if(p==NULL)
cout<<"\n此鏈表為空鏈表"<<endl;
while(j<i && p->next!=NULL){
q=p;
p=p->next;
j++;
}
if(p!=NULL){
e=p->data;
q->next=p->next;
delete p;
return true;
}
return false;
}
//按內(nèi)容定位
LNode *CLinkList::LocateElem(ElemType e){
LNode *p;
p=head->next;
while(p!=NULL && strcmp(p->data.stuID,e.stuID)!=0){
p=p->next;
}
if(p==NULL){
cout<<"\n該鏈表中不存在該元素"<<endl;
return NULL;
}
return p;
}
//按序號定位
LNode *CLinkList::LocateElem(int i){
int j=1;
LNode *p;
p=head;
//判斷輸入是不是合法
if(i<1||i>GetListLength()){
cout<<"單鏈表中不存在該元素"<<endl;
return NULL;
}
while(j<i && p->next!=NULL){
p=p->next;
j++;
}
if(j==i)
return p->next;
}
void CLinkList::InputStuInfo(ElemType &e){
cout<<"輸入學(xué)生學(xué)號:";
cin>>e.stuID;
cout<<"輸入學(xué)生姓名:";
cin>>e.name;
cout<<"輸入學(xué)生年齡:";
cin>>e.age;
cout<<"輸入學(xué)生源地:";
cin>>e.city;
cout<<"輸入聯(lián)系電話:";
cin>>e.tel;
cout<<endl;
}
bool CLinkList::UpdateList(const ElemType &e,ElemType e1){
LNode *p;
p=head->next;
while(p){
if(strcmp(p->data.stuID,e.stuID)==0){
p->data=e1;
return true;
}
p=p->next;
}
return false;
}
LNode *CLinkList::GetElem(int pos){
LNode *p;
p=LocateElem(pos);
if(p==NULL){
return NULL;
}
else
return p;
}
int Menu_Select();
void Menu_show();
void Menu_show(){
cout<<"---------------------------------"<<endl;
cout<<"---------學(xué)生信息管理系統(tǒng)---------"<<endl<<endl;
cout<<"-------⑴.生成學(xué)生信息表格--------"<<endl;
cout<<"-------⑵.插入學(xué)生基本信息--------"<<endl;
cout<<"-------⑶.修改學(xué)生基本信息--------"<<endl;
cout<<"-------⑷.刪除學(xué)生基本信息--------"<<endl;
cout<<"-------⑸.查詢學(xué)生基本信息--------"<<endl;
cout<<"-------⑹.輸出學(xué)生基本信息--------"<<endl;
cout<<"-------⑺.退出學(xué)生信息系統(tǒng)--------"<<endl;
cout<<"---------------------------------"<<endl;
cout<<endl;
cout<<"*提示:輸入編號并回車進(jìn)行相應(yīng)操作*"<<endl;
}
int Menu_Select(){
int selectNum;
for(;;){
cout<<"輸入操作編號:";
cin>>selectNum;
if(selectNum<1||selectNum>7)
cout<<"輸入有誤,請重新輸入!"<<endl;
else break;
}
return selectNum;
}
int main(void){
CLinkList list;
ElemType e,e1;
LNode *p;
Menu_show();
while(1){
switch(Menu_Select()){
case 1:
list.CreatList();
break;
case 2:
int i;
bool judge;
cout<<"插入的位置:";
cin>>i;
cout<<endl;
if(i<1||i>list.GetListLength()+1)
cout<<"插入的位置不合法!"<<endl;
else{
cout<<"請輸入學(xué)生信息和插入位置!"<<endl;
list.InputStuInfo(e);
judge=list.ListInsert(e,i);
if(false==judge)
cout<<"插入位置出錯!"<<endl;
else
cout<<"插入信息成功!"<<endl;
}
break;
case 3:
cout<<"請輸入要修改的學(xué)生學(xué)號:";
cin>>e.stuID;
cout<<endl;
p=list.LocateElem(e);
if(p){
cout<<"輸入該學(xué)生的新信息:"<<endl;
list.InputStuInfo(e1);
list.UpdateList(e,e1);
cout<<"修改信息成功!"<<endl;
}
break;
case 4:
int pos;
cout<<"刪除的位置:";
cin>>pos;
cout<<endl;
if(pos<1||pos>list.GetListLength())
cout<<"刪除位置不合法!"<<endl;
else{
list.ListDelete(e,pos);
cout<<"刪除學(xué)生信息成功!"<<endl;
}
break;
case 5:
cout<<"輸入要查詢學(xué)生的學(xué)號:"<<endl;
cin>>e.stuID;
p=list.LocateElem(e);
if(p!=NULL)
cout<<p->data.stuID<<setw(15)<<p->data.name<<setw(15)<<p->data.age<<setw(15)<<p->data.city<<setw(15)<<p->data.tel<<endl;
case 6:
cout<<"輸出結(jié)果為:"<<endl;
list.OutputList();
break;
case 7:
cout<<"再見!"<<endl;
exit(0);
}
}
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈