通訊錄問題
頭檔案linklistc:#include <string>
#include <iostream>
using namespace std;
struct DataType
{
int ID;
char name[10];
char ch;
char phone[13];
char addr[31]; };
struct Node
{
DataType data; //資料域
struct Node * next; //指標域
};
class LinkList
{
public:
LinkList(){first = new Node ; first->next = NULL;}//無參建構式
LinkList( DataType a [], int n);//有參建構式,使用含有n個元素的陣列a初始化單鏈表
~LinkList(); //解構式
DataType xingming (char t[10]); //按姓名查找通訊錄,回傳結構體
DataType dianhua (char s[13]); //按電話查找通訊錄,回傳結構體
void Insert (DataType x); //在通訊錄中加入一條資訊
void Delete(char t[10]); //洗掉某個人的資訊
void PrintList();//遍歷
private:
Node * first; //頭指標
};
LinkList::LinkList( DataType a [], int n) //尾插法建立單鏈表
{
first = new Node ;
Node * r = first;
for (int i=0;i<n;i++)
{
Node * s = new Node ;//①建立新結點
s->data=https://bbs.csdn.net/topics/a[i];//②將a[i]寫入到新結點的資料域
r->next = s; //③將新結點加入到鏈表中
r = s; //④修改尾指標
}
r->next = NULL; //終端結點的指標域設為空
}
LinkList::~LinkList() //解構式
{
Node * p = first; //初始化作業指標p
while (p) //要釋放的結點存在
{
first = p; //暫存要釋放的結點
p = p -> next; //移動作業指標
delete first; //釋放結點
}
}
DataType LinkList::xingming (char t[10]) //按姓名查找
{
Node * p = first ->next; //初始化作業指標
while (p)
{
if (strcmp(p->data.name,t)==0)
{return p->data;
break;}//找到被查元素,回傳結構體
p = p->next;
}
if (strcmp(p->data.name,t)!=0)
{cout<<"無此人"<<endl;} //若找不到,回傳錯誤標識-1
}
DataType LinkList::dianhua (char s[13]) //按電話查找
{
Node * p = first ->next; //初始化作業指標
while (p)
{
if (strcmp(p->data.phone,s)==0)
{return p->data;
break;}//找到被查元素,回傳結構體
p = p->next;
}
if (strcmp(p->data.phone,s)!=0)
{cout<<"無此電話號碼"<<endl;}; //若找不到,回傳錯誤標識-1
}
void LinkList::Insert (DataType x)//加入某人的資訊
{ Node * p = first; //初始化作業指標
while (p->next!=NULL)
{p=p->next;}
Node * s = p->next;
s->data=https://bbs.csdn.net/topics/x;
s->next=NULL;
}
void LinkList::Delete(char t[10]) //洗掉某人的資訊,并回傳
{
Node * p = first ->next; //初始化作業指標
while (p)
{
if (strcmp(p->data.name,t)==0)
{p->data=https://bbs.csdn.net/topics/p->next->data;
Node *q=p->next;
p->next=q->next;
delete q;}
if (strcmp(p->data.name,t)!=0)
{cout<<"無此人資訊";}//回傳錯誤值
}
void LinkList::PrintList() //按次序遍歷線性表中的各個資料元素
{
Node * p = first->next ; //初始化作業指標
while (p){
cout << p->data.ID<<" "<<p->data.name<<" "<<p->data.phone<<" "<<p->data.ch<<" "<<p->data.addr << endl;
p = p-> next;
}
}
主程式:
#include <iostream>
#include "linklistc.h"
using namespace std;
void main()
{ Datatype shuju[6]={{1,"tom",'m',"13433232785","New York"},{2,"mary",'f',"18600557372","Beijing"},
{3,"Ashley",'m',"13812474136","Nanjing"},{4,"xiaoming",'m',"13651184722","tianjin"},
{5,"House",'m',"18832800098","Boston"},{6,"Lihua",'f',"1861238876","London"}};
Linklist a(shuju,6);
int b;
cout<<"請輸入想要執行的操作"<<endl
cout<<"1.查看"<<" "<<"2.按姓名查詢"<<" "<<"3.按電話查詢"<<" "<<"4.洗掉"<<" "<<"5.添加"<<" "<<"6.修改"<<endl;
cin>>b;
while (cin>>b)
{if (b==1)
{a.PrintList();}
if(b==2)
{cout<<"請輸入姓名"<<endl
char t[10];
cin>>t;
DataType c=a.xingming (t);
cout<< c.ID<<" "<<c.name<<" "<<c.phone<<" "<<c.ch<<" "<<c.addr << endl;}
if(b==3)
{cout<<"請輸入電話號碼"<<endl;
char s[13];
cin>>s;
c=a.dianhua(s);
cout<< c.ID<<" "<<c.name<<" "<<c.phone<<" "<<c.ch<<" "<<c.addr << endl;}
if(b==4)
{cout<<"請輸入姓名"<<endl;
cin>>t;
a.Delete(t);}
if(b==5)
{cout<<"請依次輸入ID,姓名,電話號碼,性別,地址"<<endl;
DataType e;
cin>>e.ID>>e.name>>e.phone>>e.ch>>e.addr;
a.Insert (e);}
if(b==6)
{cout<<"請問要修改誰的資訊"<<endl
cin>>t;
c=a.xingming(t);
cout<<"請重新輸入資訊,依次為ID,電話號碼,性別,地址"
cin>>c.ID>>c.phone>>c.ch>>c.addr;}}
}
?真心調了一下午了,求幫忙,要不趕不及交了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130812.html
標籤:基礎類
上一篇:關于c語言檔案讀取與寫入的問題
