資料結構課程設計原始碼
阿新 • • 發佈:2019-02-03
主程式main()
#include "ds.h" #include <iostream> #include <string.h> #include <stdlib.h> #include <conio.h> using namespace std; LinkList::LinkList() { head=(LNode *)malloc(sizeof(LNode)); head->next=NULL; length=0; } bool LinkList::IsCreate() { if(length==0) return 0; return 1; } void LinkList::ListSize() { if(!IsCreate()) { cout<<"您還沒有建表,請先建表!"<<endl; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } else { cout<<"共儲存了"<<length<<"個學生的資料資訊."<<endl; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } } void LinkList::CreatList() { if(IsCreate()) { cout<<"已經建立連結串列!"<<endl<<"請按任意鍵繼續. . . "<<endl; getch(); } else { int n; cout<<" ******************利用頭插法建立連結串列*****************"<<endl; cout<<"請輸入要建立的單鏈表的節點數: "; cin>>n; LNode *p=head;length=n; for(int i=n;i>0;i--) { LNode *p=(LNode *)malloc(sizeof(LNode)); cout<<"請輸入學號: ";cin>>p->num; cout<<"請輸入姓名: ";cin>>p->name; cout<<"請輸入年齡: ";cin>>p->age; cout<<"請輸入成績: ";cin>>p->Score; cout<<"********************************************************************************"<<endl; p->next=head->next; head->next=p; } cout<<"請按任意鍵繼續. . ."<<endl; getch(); } } void LinkList::Find() { if(!IsCreate()) { cout<<"您還沒有建表,請先建表!"<<endl; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } else { char num[N]; cout<<"請輸入學號: "; cin>>num; LNode *p=head->next; while(p&&strcmp(p->num,num)!=0) { p=p->next; } if(!p)cout<<"無法查詢到所要查詢的學生資訊!"<<endl; else{ cout<<"姓名: "<<p->name<<endl; cout<<"年齡: "<<p->age<<endl; cout<<"成績: "<<p->Score<<endl; } cout<<"請按任意鍵繼續. . ."<<endl; getch(); } } void LinkList::DeleteList() { if(!IsCreate()) { cout<<"您還沒有建表,請先建表!"<<endl; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } else { char num[N]; cout<<"請輸入所要刪除的學生的學號: "; cin>>num; LNode *p=head; while(p->next&&strcmp(p->next->num,num)!=0) { p=p->next; } if(!(p->next))cout<<"找不到所要刪除的內容,操作失敗!"; else { length--; LNode *q=p->next; p->next=p->next->next; free(q); } cout<<"請按任意鍵繼續. . ."<<endl; getch(); } } void LinkList::Display() { if(!IsCreate()) { cout<<"您還沒有建表,請先建表!"<<endl; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } else { cout<<"所有學生的資訊如下:"<<endl<<"********************************************************************************"<<endl; cout<<"共有"<<length<<"個學生的資訊"<<endl; LNode *p=head->next; while(p) { cout<<"學號: "<<p->num<<endl; cout<<"姓名: "<<p->name<<endl; cout<<"年齡: "<<p->age<<endl; cout<<"成績: "<<p->Score<<endl<<endl; p=p->next; } cout<<"請按任意鍵繼續. . ."<<endl; getch(); } } void LinkList::InsertList() { if(!IsCreate()) { cout<<"您還沒有建表,請先建表!"<<endl; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } else { int n,i=1; cout<<"請輸入要插入的位置: ";cin>>n; if(n<1||n>length+1)cout<<"插入的位置不正確,操作失敗!"<<endl; else { LNode *q,*p=head; q=(LNode *)malloc(sizeof(LNode)); while(i<n) { p=p->next; i++; } cout<<"請輸入學號: ";cin>>q->num; cout<<"請輸入姓名: ";cin>>q->name; cout<<"請輸入年齡: ";cin>>q->age; cout<<"請輸入成績: ";cin>>q->Score; q->next=p->next; p->next=q; length++; cout<<"請按任意鍵繼續. . ."<<endl; getch(); } } } int main() { LinkList L;int order,flag=1,confirm=1;char a[20]; cout<<" ********************歡迎進入學生資訊管理系統*********************"<<endl; while(confirm) { while(flag) { cout<<"請輸入命令:"<<endl; cout<<"1.建立連結串列 2.輸出連結串列 3.插入資料 4.刪除資料 5.查詢資料 6.顯示資料總數 7.退出系統"<<endl; cout<<"輸入命令(數字標號):"; cin>>order; switch(order) { case 1: L.CreatList();break; case 2: L.Display();;break; case 3: L.InsertList();break; case 4: L.DeleteList();;break; case 5: L.Find();break; case 6: L.ListSize();break; case 7: flag=0;break; } cout<<endl; } question: cout<<"您確定要退出嗎?(Y/N): "; cin>>a; if(!strcmp(a,"Y")||!strcmp(a,"y")) { cout<<"歡迎下次再使用本系統,謝謝!"<<endl; confirm=0; } else if(!strcmp(a,"N")||!strcmp(a,"n"))flag=1; else { cout<<"您輸入的命令不正確,請重新輸入!"<<endl; goto question; } } system("pause"); return 0; }
標頭檔案
typedef struct LNode{ //資料節點定義 char num[N]; //學號 char name[N]; //姓名 int age; //年齡 float Score; //成績 LNode *next; }LNode; class LinkList //連結串列類 { private: LNode *head; //將節點封裝為私有變數 int length; public: LinkList(); //建構函式 bool IsCreate(); //判斷是否建立連結串列 void ListSize(); //求連結串列長度 void CreatList(); //建立連結串列 void InsertList(); //插入 void DeleteList(); //刪除 void Find(); //查詢 void Display(); //顯示 void BubbleSortList(); //連結串列的氣泡排序法 void Count(); //統計成績不及格的人數 }