學生信息管理系統之單鏈表實現
來源:程序員人生 發布時間:2016-10-04 11:40:34 閱讀次數:3589次
用C++實現學生信息管理系統,用到線性表的數據結構,用單鏈表實現。
首先看看甚么是單鏈表,有甚么特點:
1.單鏈表每一個節點存儲其數據和指向下1個節點的指針,即數據域和指針域,兩個邏輯上相鄰的元素存儲位置不1定相鄰。節點定義以下:
typedef struct LNode{
ElemType data;
LNode *next;
}LNode;
其中ElemType是學生基本信息的結構體,其定義以下:
typedef struct StuInfo{
char stuID[10];
char name[15];
int age;
char city[15];
char tel[15];
}ElemType;
2.單鏈表可以由頭指針唯1肯定,遍歷單鏈表只能從頭開始。
3.每一個元素都有唯1先驅和唯1后繼,除頭節點和尾節點。
4.內存動態分配,當需要插入數據時申請1個相應的空間便可,元素個數不受限制較為靈活。
5.基本操作:插入、刪除、輸出、修改、查找、排序等等。
由因而C++實現,基本操作全部封裝在類的成員函數里面。
源碼以下:
/*---------------------------------/
/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);
};
//構造1個只有頭節點的空鏈表
CLinkList::CLinkList(){
head=new LNode;
head->next=NULL;
}
//析構函數清空鏈表
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指向頭節點的后繼節點
int i=0;
while(p){ //p不為空則計數器累加
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)<<"學號"<<setw(15)<<"姓名"<<setw(15)<<"年齡"<<setw(15)<<"生源地"<<setw(15)<<"聯系電話"<<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; //得到帶頭節點的空鏈表
}
void CLinkList::CreatList(){
LNode *s;
ElemType e;
bool judge;
judge=IsListEmpty();
if(judge==false)
ClearList();
cout<<"輸入學號為!時結束!"<<endl;
cout<<"輸入學生學號:";
cin>>e.stuID;
while(strcmp(e.stuID,"!")){
cout<<"輸入學生姓名:";
cin>>e.name;
cout<<"輸入學生年齡:";
cin>>e.age;
cout<<"輸入學生源地:";
cin>>e.city;
cout<<"輸入聯系電話:";
cin>>e.tel;
cout<<endl;
s=new LNode;
s->data=e;
s->next=head->next;
head->next=s;
cout<<"輸入學生學號:";
cin>>e.stuID;
}
cout<<"鏈表建成"<<endl;
}
bool CLinkList::ListInsert(ElemType &e,int i){
int j=1;
LNode *s,*p;
s=new LNode; //建立1個待插入的節點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;
}
//按內容定位
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<<"輸入學生學號:";
cin>>e.stuID;
cout<<"輸入學生姓名:";
cin>>e.name;
cout<<"輸入學生年齡:";
cin>>e.age;
cout<<"輸入學生源地:";
cin>>e.city;
cout<<"輸入聯系電話:";
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<<"---------學生信息管理系統---------"<<endl<<endl;
cout<<"-------⑴.生成學生信息表格--------"<<endl;
cout<<"-------⑵.插入學生基本信息--------"<<endl;
cout<<"-------⑶.修改學生基本信息--------"<<endl;
cout<<"-------⑷.刪除學生基本信息--------"<<endl;
cout<<"-------⑸.查詢學生基本信息--------"<<endl;
cout<<"-------⑹.輸出學生基本信息--------"<<endl;
cout<<"-------⑺.退出學生信息系統--------"<<endl;
cout<<"---------------------------------"<<endl;
cout<<endl;
cout<<"*提示:輸入編號并回車進行相應操作*"<<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<<"請輸入學生信息和插入位置!"<<endl;
list.InputStuInfo(e);
judge=list.ListInsert(e,i);
if(false==judge)
cout<<"插入位置出錯!"<<endl;
else
cout<<"插入信息成功!"<<endl;
}
break;
case 3:
cout<<"請輸入要修改的學生學號:";
cin>>e.stuID;
cout<<endl;
p=list.LocateElem(e);
if(p){
cout<<"輸入該學生的新信息:"<<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<<"刪除學生信息成功!"<<endl;
}
break;
case 5:
cout<<"輸入要查詢學生的學號:"<<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<<"輸出結果為:"<<endl;
list.OutputList();
break;
case 7:
cout<<"再見!"<<endl;
exit(0);
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈