C++ 雙向連結串列簡單實現通訊錄
阿新 • • 發佈:2018-12-23
#include<iostream>
#include<fstream>
#include <stdlib.h>
#include<string>
using namespace std;
typedef struct Directory
{
string Name;
string Mobile;
string Wechatnumber;
string STREET;
string CITY;
string EIP;
string STATE;
struct Directory* next;
struct Directory* prev;
}Directory;
//頭節點初始化
Directory p0 = {"0","0","0","0","0","0","0",NULL,NULL};
Directory pn = {"0","0","0","0","0","0","0",NULL,NULL};
//設定頭指標,指向頭節點
Directory *head = &p0;
//函式宣告
void enter(Directory*);
void display_list();
void printf_a();
void display_menu(Directory*);
int key_ramove(string);
void display_listfiile();
Directory* find_load(string);
Directory* load();
int main()
{
cout<<"========================================"<<endl;
cout<<"= 通 訊 錄 =" <<endl;
cout<<"= ="<<endl;
cout<<"= ="<<endl;
cout<<"= 1.新增 2.刪除 3.查詢 4.檢視通訊錄 ="<<endl;
cout<<"========================================"<<endl;
int i = 0; //定義按鍵變數,存放鍵值
string key; //定義字串變數,存放輸入的字串
p0.next = &pn; //這倆句是初始化頭尾節點,頭尾節點連起來
pn.prev = &p0;
while(1)
{
cin>>i; //輸入i,用於選擇第幾個功能
switch(i){ //選擇,i等於幾,就執行case幾
case 1:
load(); //新建節點並插入連結串列
cout<<endl;
cout<<"新增完成!!!"<<endl;
printf_a();
break;
case 2:
cout<<"請輸入名字:";
cin>>key;
key_ramove(key); //刪除節點,就是刪除一個人的資訊
printf_a();
break;
case 3:
cout<<"請輸入名字:";
cin>>key;
display_menu(find_load(key)); //列印找到節點內的資訊,find_load(key)為找節點用的遍歷函式
printf_a(); /
break;
case 4:
display_list(); //列印所有節點的名字
printf_a();
break;
case 5:
display_listfiile(); //把所有節點的資訊輸入到"address.txt"檔案
break;
default:
break;
}
}
return 0;
}
/**************************************************
* 返回型別:void
* 函式作用:列印選單
***************************************************/
void printf_a()
{
cout<<"----------------------------------------"<<endl;
cout<<"- 1.新增 2.刪除 3.查詢 4.檢視通訊錄 -"<<endl;
cout<<"- 5.匯出txt文件 -"<<endl;
cout<<"----------------------------------------"<<endl;
}
/**************************************************
* 返回型別:Directory*
* 函式作用:新建節點插入,連結串列
***************************************************/
Directory* load()
{
Directory *p = new Directory; //給這個新節點分配空間
enter(p);
p->next = head->next; //p的下一個指向頭指標指向節點的下一個
head->next = p; //頭指標指向節點的下一個指向p
p->prev = head; //p的上一個指向指標指向節點的下一個
p->next->prev = p;
head = p; //頭指標指向p
return p;
}
/**************************************************
* 返回型別:void
* 函式作用:單個節點查詢
* 傳入引數:名字 10分
***************************************************/
Directory* find_load(string key_name)
{
Directory *p;
p = &pn;
for(p; p->prev != NULL ;p = p->prev)
{
if(p->Name == key_name )
{
return p;
}
}
return NULL;
}
/**************************************************
* 返回型別:void
* 函式作用:單個節點刪除
* 傳入引數:名字 15分
***************************************************/
int key_ramove(string key_name)
{
Directory *p; //定義結構體型別的指標
p = &pn;
for(p; p->prev != NULL;p = p->prev)
{
if(p->Name == key_name )
{
head = pn.prev;
p->prev->next = p->next; //p的上一個的下一個指向p的下一個
p->next->prev = p->prev; //p的下一個的上一個指向p的上一個
free(p); //釋放p的空間
return 0; //刪除後,退出函式
}
}
cout<<"沒有此人!!!!"<<endl;
return 0;
}
/**************************************************
* 返回型別:void
* 函式作用:單個節點輸入
* 傳入引數:Directtory(自己定義的結構體)型指標, 5分
***************************************************/
void enter(Directory *P )
{
char jubge; //用來判斷的變數
string name, mobile;
cout<<"輸入姓名:";
cin>>name;
P->Name = name; //把輸入的字串放到,當前指標指向的節點
cout<<"輸入電話:";
cin>>mobile;
P->Mobile = mobile; //把輸入的字串放到,當前指標指向的節點
cout<<"是否完善資訊?(Y/N)"<<endl;
cin>>jubge;
if(jubge == 'y' || jubge == 'Y')
{
string wechatnumber; //微信
string street; //街道
string city; //城市
string eip; //郵編
string state; //國家
cout<<"微信:";
cin>>wechatnumber;
P->Wechatnumber = wechatnumber;
cout<<"街道:";
cin>>street;
P->STREET = street;
cout<<"城市:";
cin>>city;
P->CITY = city;
cout<<"國家:";
cin>>state;
P->STATE = state;
}else{ //除了輸入y以外都會執行這個
P->Wechatnumber = "NULL";
P->STREET = "NULL";
P->CITY = "NULL";
P->STATE = "china";
}
}
/**************************************************
* 返回型別:void
* 函式作用:列印通訊錄(所有人名字,逆序)
***************************************************/
void display_list()
{
Directory *p; //定義結構體型別的指標
p = head; // 讓p 等於當前head指標所指的節點
int i = 1; //顯示序號
cout<<endl;
cout<<"*******************************************"<<endl;
cout<<" 通 訊 錄 "<<endl;
cout<<"------------------------------------------"<<endl;
while(p->prev != NULL) //迴圈,知道p指向節點的prev指標指向NULL
{
cout<<" "<<i<<": "<<p->Name<<endl; //列印p指標對應節點的名字
cout<<"------------------------------------------"<<endl;
p = p->prev; //指標指向上一個節點
i++;
}
cout<<"*******************************************"<<endl;
cout<<endl;
}
void display_listfiile()
{
Directory *p; //定義結構體型別的指標
p = &pn; // 讓p 等於當前head指標所指的節點
int i = 1; //顯示序號
ofstream fout("address.txt"); //開啟address.txt檔案,沒有自動建立
while(p->prev != NULL) //迴圈,知道p指向節點的prev指標指向NULL
{
fout << i <<":" << p->Name <<endl; //把當前節點的名字輸出到address.txt檔案
fout <<" " << p->Mobile <<endl;
fout <<" " << p->Wechatnumber <<endl;
fout <<" " << p->STREET <<endl;
fout <<" " << p->CITY <<endl;
fout <<" " << p->STATE <<endl;
fout <<endl;
p = p->prev; //指標指向上一個節點
i++;
}
fout.close(); //關閉檔案流
}
/**************************************************
* 返回型別 Directory
* 函式作用:列印詳細資訊
***************************************************/
void display_menu(Directory *P)
{
if(P == NULL) // 判斷 如果P為空,則列印沒有這個人
{
cout<<"沒有此人!! "<<endl;
}else{
cout<<"* * * * * * * * * * * * * * * * * * * * "<<endl;
cout<<"* 姓名: "<<P->Name<<endl;
cout<<"* 電話: "<<P->Mobile<<endl;
cout<<"* 微信: "<<P->Wechatnumber<<endl;
cout<<"* 街道: "<<P->STREET<<endl;
cout<<"* 城市: "<<P->CITY<<endl;
cout<<"* 國家:"<<P->STATE<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * "<<endl;
}
}