双向链表实现电话簿C++代码实现
生活随笔
收集整理的這篇文章主要介紹了
双向链表实现电话簿C++代码实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
雙向鏈表實(shí)現(xiàn)電話簿主要涉及的知識(shí)點(diǎn)就是雙向鏈表的創(chuàng)建,插入,刪除操作,涉及到一小點(diǎn)的文件操作。雙向鏈表的插入和刪除操作根據(jù)刪除頭結(jié)點(diǎn)尾結(jié)點(diǎn)中間結(jié)點(diǎn)代碼不同,其中頭結(jié)點(diǎn)和尾結(jié)點(diǎn)稍許的麻煩因?yàn)椴迦雱h除都涉及到頭指針和尾指針的重新賦值和連接。刪除中間結(jié)點(diǎn)較為簡(jiǎn)單,設(shè)置一個(gè)臨時(shí)結(jié)點(diǎn)指向要?jiǎng)h除結(jié)點(diǎn)的前向指針前后連接即可。文件操作主要涉及文件的讀取操作相對(duì)來(lái)說(shuō)比較簡(jiǎn)單。最難理解的可能就是雙向鏈表的操作。這一部分我會(huì)單掕出來(lái)記錄下自己的見(jiàn)解。代碼雖然有500多行,但很多是雙向鏈表插入刪除等重復(fù)的操作,最主要的還是心中要有整體的框架。知道類內(nèi)包含哪些函數(shù)。代碼如下:
參考資料:譚浩強(qiáng)C++程序設(shè)計(jì)實(shí)踐
#include<fstream> #include<iostream> #include<iomanip> #include<string> #include<cstring> #include<cstdlib> using namespace std; namespace NameRecord {struct friend_node{char last_name[20];char first_name[15];char phone_num[12];friend_node *next;friend_node *prior;};friend_node *head_ptr;friend_node *tail_ptr;friend_node *current_ptr;char pause;class record{public:void userchoice(int choice);void insertrecord();void insertnode(friend_node *new_tr);void insertnodehead(friend_node *new_tr);void insertnodeend(friend_node *new_tr);void showlist();void deleterecord();void deletenodehead();void deletenodeend();void deletenodemiddle();int verifydelete();void deletenode();void deletelist();void searchbylastname();void savefile();void loadfile();//void help();void modifyrecord();void userinput(); }; } using namespace NameRecord; int main() {record myrecord;cout<<"歡迎使用電話本\n";cout<<"按enter鍵繼續(xù)\n";cin.get(pause);system("cls");int choice;head_ptr=NULL;tail_ptr=NULL;myrecord.loadfile();do{cout<<"1_新增記錄\n";cout<<"2_顯示所有記錄\n";cout<<"3_按姓氏搜索記錄\n";cout<<"4_刪除記錄\n";cout<<"5_修改記錄\n";cout<<"6_幫助\n";cout<<"7_退出程序\n";cout<<"輸入你的選擇:";cin>>choice;myrecord.userchoice(choice);}while(choice!=7);return 0; } void record::userchoice(int choice) {switch(choice){case 1:insertrecord();break;case 2:showlist();break;case 3:searchbylastname();break;case 4:deleterecord();break;case 5:modifyrecord();break;// case 6:// help();// break;case 7:savefile();if(head_ptr!=NULL)deletelist();break;default:cout<<"選擇無(wú)效\n";break; } } void record::insertrecord() {friend_node *new_ptr;new_ptr=new friend_node;if(new_ptr!=NULL){system("cls");cin.ignore(20,'\n');cout<<"名字";cin.getline(new_ptr->first_name,15);cout<<"姓氏";cin.getline(new_ptr->last_name,20);cout<<"電話號(hào)碼";cin.getline(new_ptr->phone_num,15);insertnode(new_ptr);}elsecout<<"警告:申請(qǐng)存儲(chǔ)空間失敗,不能創(chuàng)建新結(jié)點(diǎn)。\n";system("cls"); } void record::insertnode(friend_node *new_ptr) {system("cls");friend_node *temp_ptr;//情況1:雙向鏈表為空 if(head_ptr==NULL){new_ptr->next=new_ptr;new_ptr->prior=new_ptr;head_ptr=new_ptr;tail_ptr=new_ptr;}//雙向鏈表只有一個(gè)結(jié)點(diǎn) if(head_ptr->next==head_ptr){if(strcmp(new_ptr->last_name,head_ptr->last_name)<0)insertnodehead(new_ptr);elseinsertnodeend(new_ptr);return;} //情況3:如果鏈表中不是只有一個(gè)結(jié)點(diǎn)if(head_ptr->next!=head_ptr){current_ptr=head_ptr->next;while((strcmp(new_ptr->last_name,current_ptr->last_name)>0)&&(current_ptr!=head_ptr))current_ptr=current_ptr->next;if(current_ptr==head_ptr)insertnodeend(new_ptr);else{temp_ptr=current_ptr->prior;temp_ptr->next=new_ptr;new_ptr->prior=temp_ptr;current_ptr->prior=new_ptr;new_ptr->next=current_ptr;}return;} } void record::insertnodehead(friend_node *new_ptr) {new_ptr->next=head_ptr;new_ptr->prior=tail_ptr;head_ptr->prior=new_ptr;tail_ptr->next=new_ptr;head_ptr=new_ptr; } void record::insertnodeend(friend_node *new_ptr) {new_ptr->next=head_ptr;tail_ptr->prior=new_ptr;new_ptr->prior=tail_ptr;head_ptr->next=new_ptr;tail_ptr=new_ptr; } void record::showlist() {system("cls");int n;cout<<"請(qǐng)輸入每屏顯示的數(shù)目(不得大于20):\n";cin>>n;system("cls");int i;char fullname[36];current_ptr=head_ptr;do{i=1;cout<<setw(20)<<"Name"<<setw(20)<<"Phone Number\n";do{strcpy(fullname,current_ptr->last_name);strcat(fullname,",");strcat(fullname,current_ptr->first_name);cout<<setw(20)<<fullname<<setw(20)<<current_ptr->phone_num<<endl;current_ptr=current_ptr->next;i++;}while(current_ptr!=head_ptr&&i<=n);cin.get(pause);if(current_ptr!=head_ptr){cout<<"請(qǐng)按enter鍵繼續(xù)";cin.get(pause);system("cls");}elsecout<<"文件結(jié)束!";}while(current_ptr!=head_ptr);cin.get(pause);cin.ignore(1,pause);system("cls"); } void record::searchbylastname() {system("cls");int nflag=0;char search_string[20];current_ptr=head_ptr;if(current_ptr==NULL)cout<<"電話記錄為空";else{cin.ignore(20,'\n');cout<<"輸入你要搜索記錄的姓氏";cin.getline(search_string,20);if(strcmp(current_ptr->last_name,search_string)==0){if(nflag==0)cout<<"找到結(jié)點(diǎn)\n";nflag=1;cout<<current_ptr->first_name<<' '<<current_ptr->last_name<<"\t\t";cout<<current_ptr->phone_num<<endl; }current_ptr=current_ptr->next;while(current_ptr!=head_ptr){if(strcmp(current_ptr->last_name,search_string)==0){if(nflag==0)cout<<"找到記錄\n";nflag=1;cout<<current_ptr->first_name<<' '<<current_ptr->last_name<<"\t\t";cout<<current_ptr->phone_num<<endl;}current_ptr=current_ptr->next;}if(nflag==0){cout<<"無(wú)記錄!";cin.get(pause);system("cls");}} } void record::deleterecord() {system("cls");char search_string[20];friend_node *previous_ptr;previous_ptr=NULL;current_ptr=head_ptr;if(current_ptr==NULL){cout<<"沒(méi)有要?jiǎng)h除的記錄";return;}cin.ignore(20,'\n');int nflag=0;cout<<"\n輸入你要?jiǎng)h除結(jié)點(diǎn)的姓氏";cin.getline(search_string,20);while((strcmp(current_ptr->last_name,search_string)==0)&&head_ptr!=NULL){nflag=1;cout<<"\n找到記錄\n";cout<<current_ptr->first_name<<' '<<current_ptr->last_name<<"\t\t";cout<<current_ptr->phone_num<<endl;if(verifydelete()){deletenode();cout<<"\n記錄已刪除\n";}else{cout<<current_ptr->first_name<<","<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<" 的記錄沒(méi)有刪除\n";current_ptr=current_ptr->next;}}do{if(strcmp(current_ptr->last_name,search_string)==0){nflag=1;cout<<"\n找到記錄\n";cout<<current_ptr->first_name<<' '<<current_ptr->last_name<<"\t\t";cout<<current_ptr->phone_num<<endl;if(verifydelete()){deletenode();cout<<"\n記錄已刪除\n";}else{cout<<current_ptr->first_name<<','<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<" 的記錄沒(méi)有刪除\n";}}current_ptr=current_ptr->next;} while((current_ptr!=head_ptr||head_ptr==NULL));if(nflag==0)cout<<"\n沒(méi)有找到相符的記錄也沒(méi)有刪除記錄\n";cin.get();system("cls"); } int record::verifydelete() {char yesno;cout<<"\n確認(rèn)?(y/n)";cin>>yesno;cin.ignore(20,'\n');if((yesno=='Y')||(yesno=='y'))return(1);elsereturn(0); } void record::deletenode() {if(current_ptr=head_ptr)deletenodehead();elseif(current_ptr->next==head_ptr)deletenodeend();elsedeletenodemiddle(); } void record::deletenodehead() {if(head_ptr->next!=head_ptr){head_ptr=current_ptr->next;tail_ptr->next=head_ptr;head_ptr->prior=tail_ptr;delete current_ptr;current_ptr=head_ptr;}else{head_ptr=NULL;tail_ptr=NULL;delete current_ptr;} } void record::deletenodeend() {friend_node *previous_ptr=current_ptr->prior;delete current_ptr;previous_ptr->next=head_ptr;head_ptr->prior=previous_ptr;tail_ptr=previous_ptr;current_ptr=tail_ptr; } void record::deletenodemiddle() {friend_node *previous_ptr=current_ptr->prior;previous_ptr->next=current_ptr->next;current_ptr->next->prior=previous_ptr;delete current_ptr;current_ptr=previous_ptr; } void record::deletelist() {friend_node *temp_ptr;current_ptr=head_ptr;do{temp_ptr=current_ptr->next;tail_ptr->prior=temp_ptr;temp_ptr->prior=tail_ptr;delete current_ptr;current_ptr=temp_ptr;}while(temp_ptr!=NULL&&temp_ptr!=tail_ptr);delete current_ptr; } void record::savefile() {ofstream outfile;outfile.open("FRIENDS.dat",ios::out);if(outfile){current_ptr=head_ptr;if(head_ptr!=NULL){do{outfile<<current_ptr->last_name<<endl;outfile<<current_ptr->first_name<<endl;outfile<<current_ptr->phone_num<<endl;current_ptr=current_ptr->next;}while(current_ptr!=head_ptr);}outfile<<"文件結(jié)束"<<endl;outfile.close();}elsecout<<"打開(kāi)文件出錯(cuò)\n"; } void record::loadfile() {friend_node *new_ptr;ifstream infile;int end_lop=0;if(infile){do{new_ptr=new friend_node;if(new_ptr!=NULL){infile.get(new_ptr->last_name,20);infile.ignore(20,'\n');if((strcmp(new_ptr->last_name,"")!=0)&&(strcmp(new_ptr->last_name,"文件結(jié)束")!=0)){infile.get(new_ptr->first_name,15);infile.ignore(20,'\n');infile.get(new_ptr->phone_num,15);infile.ignore(20,'\n');insertnode(new_ptr);}else{delete new_ptr;end_lop=1;}}else{cout<<"警告:沒(méi)有成功從磁盤導(dǎo)入文件.\n";end_lop=1;}}while(end_lop==0);infile.close();}else cout<<"沒(méi)有可用的數(shù)據(jù)文件,記錄表為空\(chéng)n"; } void record::modifyrecord() {system("cls");char search_string[20];if(head_ptr==NULL){cout<<"無(wú)記錄可修改\n";return;}int nflag=0;current_ptr=head_ptr;cin.ignore(20,'\n');cout<<"\n輸入你想修改記錄的姓氏";cin.getline(search_string,20);friend_node *new_ptr;while((strcmp(current_ptr->last_name,search_string)==0)&&head_ptr!=NULL){new_ptr=new friend_node;nflag=1;cout<<"\n找到記錄\n";cout<<current_ptr->first_name<<' '<<current_ptr->last_name<<"\t\t";cout<<current_ptr->phone_num<<endl;if(verifydelete()){cout<<"請(qǐng)輸入新記錄的姓氏";cin.getline(new_ptr->last_name,20);cout<<"請(qǐng)輸入新記錄的名字";cin.getline(new_ptr->first_name,15);cout<<"請(qǐng)輸入新記錄的電話";cin.getline(new_ptr->phone_num,12);if(verifydelete()){deletenode();insertnode(new_ptr);cout<<"\n記錄已修改\n";cout<<new_ptr->first_name<<' ';cout<<new_ptr->last_name<<endl;cout<<new_ptr->phone_num<<endl;current_ptr=head_ptr;}}else{cout<<current_ptr->first_name<<","<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<" 的記錄沒(méi)有被修改\n";current_ptr=current_ptr->next;delete new_ptr;} }do{if(strcmp(current_ptr->last_name,search_string)==0){nflag=1;new_ptr=new friend_node;cout<<"\n找到記錄\n";cout<<current_ptr->first_name<<' '<<current_ptr->last_name<<"\t\t";cout<<current_ptr->phone_num<<endl;if(verifydelete()){cout<<"請(qǐng)輸入新記錄的姓氏";cin.getline(new_ptr->last_name,20);cout<<"請(qǐng)輸入新記錄的名字";cin.getline(new_ptr->first_name,15);cout<<"請(qǐng)輸入新記錄的電話";cin.getline(new_ptr->phone_num,12);if(verifydelete()){friend_node *temp_ptr=current_ptr->prior;deletenode();insertnode(new_ptr);current_ptr=temp_ptr;cout<<"\n記錄已修改\n";cout<<new_ptr->first_name<<' ';cout<<new_ptr->last_name<<endl;cout<<new_ptr->phone_num<<endl;}}else{cout<<current_ptr->first_name<<","<<current_ptr->last_name<<"\t"<<current_ptr->phone_num<<" 的記錄沒(méi)有被修改\n";delete new_ptr;}}current_ptr=current_ptr->next;}while(current_ptr!=head_ptr||head_ptr==NULL);if(nflag==0){cout<<"找不到相符合的記錄";userinput();}system("cls");} void record::userinput(){char answer_y_n;cout<<"再試一次?y/n";cin.get(answer_y_n);if(answer_y_n=='y'||answer_y_n=='y')if(answer_y_n=='y')modifyrecord();else{cout<<"無(wú)效輸入";userinput();}}?
總結(jié)
以上是生活随笔為你收集整理的双向链表实现电话簿C++代码实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安卓证书 查看
- 下一篇: s3c2440移植MQTT