C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)_第1頁
C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)_第2頁
C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)_第3頁
C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)_第4頁
C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)本文實(shí)例為大家分享了C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)的具體代碼,供大家參考,具體內(nèi)容如下

功能如下:

1添加學(xué)生信息

2刪除學(xué)生信息

3顯示學(xué)生信息

4查詢學(xué)生信息

5學(xué)生信息排序

6清空屏幕信息

7清空文檔信息

8退出管理系統(tǒng)

上代碼!

#includeiostream

#includealgorithm

#includestring

#includefstream//讀寫文件的頭文件

usingnamespacestd;

structElementType;

structNode;

structQueue;

typedefstructQueue*MyQueue;

structElementType{

intid;

stringname;

intnum;

structNode{

ElementTypedata;

Node*next;

structQueue{

Node*front;

Node*rear;

MyQueueInit(MyQueueq);//Initializequeue

boolIsEmpty(MyQueueq);//Determineifthequeueisempty

boolInsert(ElementTypex,MyQueueq);//Insertthedatatotheendofthequeue

boolDelete(constintmessage,MyQueueq);//Findsomedatainthequeue,andthendeletethecorrespondingnode

voidPrint(constNode*q);//Printsalltheinformationinanode

voidPrintAll(constMyQueueq);//Printsinformationfromallnodes

boolFindByName(conststringmassage,constMyQueueq);//Printsinformationfromallnodes

voidInput(MyQueueq);//Whentheaddressbookisempty,re-entertheinformationintotheaddressbook

voidWrite(MyQueueq);//Writetheinformationfromthequeuetothedocument

MyQueueRead();//Writetheinformationfromthequeuetothedocument

MyQueueReadOrClear(MyQueueq);//Whethertoemptyalltheinformation

voidSwap(ElementTypex,ElementTypey);//Swapfunctionsinsort

MyQueueBubbleSort(MyQueueq);//SortbystudentIDusingbubblesort

voidMenu(MyQueueq);//mainmenu

//初始化隊(duì)列

MyQueueInit(MyQueueq){

q=newQueue();

if(q==NULL)returnNULL;

q-front=NULL;

q-rear=NULL;

returnq;

//查看隊(duì)列是否為空

boolIsEmpty(MyQueueq){

returnq-front==NULL;

//添加信息

boolInsert(ElementTypex,MyQueueq){

Node*temp=newNode();

if(temp==NULL)returnfalse;

temp-data=x;//這里需要改成需要的內(nèi)容,最好(必須)改成一個函數(shù)的形式,賦值的時候調(diào)用函數(shù),打印的時候也調(diào)用函數(shù)

temp-next=NULL;

if(IsEmpty(q)){

q-front=temp;

q-rear=temp;

returntrue;

}

else{

q-rear-next=temp;

q-rear=temp;

returntrue;

}

//刪除功能

boolDelete(constintmessage,MyQueueq){

Node*temp=newNode();

if(temp==NULL)returnfalse;//申請儲存空間失敗

boolpd=0;

//先是找到這個id再進(jìn)行刪除

//先判斷是不是頭節(jié)點(diǎn),若不是再把頭節(jié)點(diǎn)當(dāng)首節(jié)點(diǎn)進(jìn)行使用

if(q-front-data.id==message){//如果刪除頭節(jié)點(diǎn)

temp=q-front;

q-front=q-front-next;

deletetemp;

temp=NULL;

pd=1;

}

elseif(q-rear-data.id==message){//如果刪除尾節(jié)點(diǎn)

//先找到尾節(jié)點(diǎn)的前一個結(jié)點(diǎn)

temp=q-front;

while(temp-next-data.id!=message)temp=temp-next;

q-rear=temp;

q-rear-next=NULL;

pd=1;

}

else{//如果刪除中間節(jié)點(diǎn)

temp=q-front;

while(temp-next!=NULLtemp-next-data.id!=message)temp=temp-next;

if(temp-next==NULL)returnfalse;//判斷是不是沒有找到,沒有找到返回false

Node*mp=newNode();

mp=temp-next;

temp-next=temp-next-next;

deletemp;

mp=NULL;

pd=1;

}

if(pd==1){

Write(q);

cout"已成功刪除該學(xué)生信息!"endl;

returntrue;

}

//通過姓名進(jìn)行查找

boolFindByName(conststringmassage,constMyQueueq){//此函數(shù)只有查找功能,沒有打印功能,打印功能在另一個函數(shù)

Node*temp=newNode();

boolpd=0;

if(q-front-==massage){

temp=q-front;

Print(temp);

returntrue;

}

else{

temp=q-front;

while(temp-next!=NULLtemp-next-!=massage)temp=temp-next;

if(temp-next==NULL)returnfalse;//沒有找到這個人的姓名,返回false

Print(temp-next);

returntrue;

}

//單個進(jìn)行打印

voidPrint(constNode*q){

cout"該學(xué)生的信息為:"endl;

cout"學(xué)號:"q-data.id"姓名:"q-"電話號碼:"q-data.numendl;

//打印全部的學(xué)生信息

voidPrintAll(constMyQueueq){

cout"學(xué)號";

for(inti=0;ii++){

cout"-";

}

cout"姓名";

for(inti=0;ii++){

cout"-";

}

cout"電話號碼"endl;

Node*temp;

temp=q-front;

while(temp!=NULL){

cout""temp-data.id"

"temp-"

"temp-data.numendl;

temp=temp-next;

}

//coutendl;

//實(shí)現(xiàn)排序的功能函數(shù)

voidSwap(ElementTypex,ElementTypey){

ElementTypetemp;

temp=x;

x=y;

y=temp;

MyQueueBubbleSort(MyQueueq){

if(q-front==NULL||q-front-next==NULL)returnNULL;

for(Node*i=q-front;i-next!=NULL;i=i-next){

for(Node*j=q-front;j-next!=NULL;j=j-next){

if(j-data.idj-next-data.id){

Swap(j-data,j-next-data);

}

}

}

returnq;

//把全部信息存入到文檔中

voidWrite(MyQueueq){

//先根據(jù)學(xué)號進(jìn)行排序,再進(jìn)行存儲

q=BubbleSort(q);

ofstreamwriteIt;

writeIt.open("data.txt");

if(writeIt.fail()){

cout"該文件沒有找到!"endl;

cout"程序已退出!"endl;

exit(1);

}

Node*temp=newNode();

if(q!=NULL){

temp=q-front;

while(temp!=NULL){

writeIttemp-data.id""temp-""temp-data.numendl;;

temp=temp-next;

}

}

writeIt.close();

//從文檔中讀出所有的信息

MyQueueRead(){

ifstreamreadIt("data.txt");

if(readIt.fail()){

cout"該文件沒有找到!"endl;

cout"程序已退出!"endl;

exit(1);

}

intid1;

stringname1;

intnum1;

MyQueueq=newQueue();

ElementTypex;

while(!readIt.eof()){

readItid1name1num1;

if(readIt.eof())break;

x.id=id1;

=name1;

x.num=num1;

Insert(x,q);

}

readIt.close();

returnq;

//讀入文檔中的信息

MyQueueReadOrClear(MyQueueq){

q=Read();

returnq;

//使整個隊(duì)列置空

voidMakeEmpty(MyQueueq){

while(q-front!=NULL){

Node*temp=newNode();

temp=q-front;

q-front=q-front-next;

deletetemp;

}

//主菜單

voidMenu(MyQueueq){

q=ReadOrClear(q);

while(1){

coutendl;

cout"|--------------------學(xué)生通訊錄系統(tǒng)---------------------|"endl;

cout"|--------------------1添加學(xué)生信息---------------------|"endl;

cout"|--------------------2刪除學(xué)生信息---------------------|"endl;

cout"|--------------------3顯示學(xué)生信息---------------------|"endl;

cout"|--------------------4查詢學(xué)生信息---------------------|"endl;

cout"|--------------------5學(xué)生信息排序---------------------|"endl;

cout"|--------------------6清空屏幕信息---------------------|"endl;

cout"|--------------------7清空文檔信息---------------------|"endl;

cout"|--------------------8退出管理系統(tǒng)---------------------|"endl;

cout"|-------------------------------------------------------|"endl;

intn;

cout"輸入您的選擇:"endl;

cinn;

switch(n){

case1:{

ElementTypex;

cout"請輸入該學(xué)生的信息:學(xué)號姓名電話號碼"endl;

cinx.idx.num;

Insert(x,q);

Write(q);

cout"已成功添加該學(xué)生信息!"endl;

break;

}

case2:{

cout"請輸入該學(xué)生的學(xué)號:"endl;

intnum1;

cinnum1;

if(!Delete(num1,q)){

cout"該系統(tǒng)中不存在該學(xué)生!"endl;

};

break;

}

case3:{

cout"正在打印全部學(xué)生信息中.......請稍等!"endl;

cout"全部學(xué)生的信息為:"endl;

PrintAll(q);

break;

}

case4:{

cout"請輸入該學(xué)生的姓名:"endl;

stringname1;

cinname1;

if(!FindByName(name1,q)){

cout"該系統(tǒng)中不存在該學(xué)生!"endl;

}

break;

}

case5:{

cout"正在根據(jù)學(xué)生的學(xué)號對學(xué)生進(jìn)行排序....."endl;

cout"排完序后,結(jié)果為:"endl;

BubbleSort(q);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論