|
发表于 2021-10-1 10:25:28
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
#include<cstdlib>
#include<string.h>
#include<iomanip>
using namespace std;
typedef struct
{
char num[5];
char name[9];
char phone[13];
}ElemType;
typedef struct node
{
ElemType data;
struct node *next;
}ListNode,*LinKlist;
int menu_select()
{
int sn;
cout<<"Welcome to Friends Book Management"<<endl;
cout<<"1.LinkList of Friends Book--Create"<<endl;
cout<<"2.Node of Friends Book--Insert"<<endl;
cout<<"3.Node of Friends Book--Search"<<endl;
cout<<"4.Node of Friends Book--Delete"<<endl;
cout<<"5.LinkList of Friends Book--Show"<<endl;
cout<<"6,Exit"<<endl;
cout<<"CeEnter 1-6 to select the menu:";
for(; ;){
cin>>sn;
if(sn<1 || sn>6)
cout<<"\n\tInput Error! please enter 1-6 to select!\n\nCeEnter 1-6 to select the menu:";
else
break;
}
cout<<endl<<endl;
return sn;
}
void PrintList(LinkList head)
{
LinkList p;
p=head->next;
if(p){
cout<<"number(4) name(8) Phone(11)"<<endl;
cout<<"---------------------------------<<"endl;
}
else cout<<"No record!"<<endl;
while(p){
cout<<setw(6)<<p->data.num<<setw(10)<<p->data.name<<setw(13)<<p->data.phone<<endl;
p=p->next;
}
cout<<"----------------------------------"<<endl<<endl;
}
void CreateList(LinkList &head)
{
ListNode *p,*rear;
int flag=0;
rear=head;
while(flag==0)
{
cout<<"-----------------------"<<endl;
cout<<"- number(4) name(8) Phone(11) -"<<endl;
cout<<"-----------------------"<<endl;
cout<<"Please input the data of record including : number(4) name(8) Phone(11)"<<endl;
p=new ListNode;
cin>>p->data.num>>p->data.name>>p->data.phone;
rear->next=p;
rear=p;
cout<<"Stop Creating List?(1:Stop or 0:Continue)\n";
cin>>flag;
}
rear->next=NULL;
PrintList(head);
return;
}
void InsetNode(LinkList &head, ListNode *p)
{
ListNode *p1,*p2;
p1=head;
p2=p1->next;
while(p2 &&(strcmp(p2->data.num,p->data.num)<0){
p1=p2;
p2=p2->next;
}
p1->next=p;
p->next=p2;
PrintList(head);
}
ListNode *ListFind(LinkList head)
{
LinkList p;
char num(5);
char name(9);
int selnum;
cout<<"===================\n";
cout<<"1.search by number\n";
cout<<"2.search by name\n";
cout<<"===================\n";
cout<<"Enter your selection: ";
p=head->next;
cin>>selnum;
if(selnum==1)
{
cout<<"Please enter the number for searching:";
cin>>num;
while(p&&strcmp(p->data.num,num)!=0)
p=p->next;
if(p==NuLL||strcmp(p->data.num,num)!=0)
p=NULL;
}
else
if(selnum==2)
{
cout<<"Please enter the name number for searching: ";
cin>>name;
while(P&&strcmp(p->data.name,name)!=0)
p=p->next;
}
return p;
}
void DelNode(LinkList head)
{
char ch;
LinkList p,q;
p=ListFind(head);
if(!p)
{
cout<<"not found!\n\n";
return;
}
cout<<"Delete it,sure(y/n)?";
cin>>ch;
if(ch=='y'||ch=='Y')
{
q=head;
while(q&&q->next!=p)
q= q->next;
q->next=p->next;
delete p;
cout<<"it has been deleted!\n\n";
PrintList(head);
}
}
void DestroyList(LinkList &L)
{
LinkList p;
while(L)
{
p=L;
L=L->next;
delete p;
}
return;
}
int main()
{
LinkList head,p;
head=new ListNode;
head->next=NULL;
while(1)
{
switch(menu_select())
{
case 1:
cout<<"*********************************"<<endl;
cout<<"* LinkList of Friends Book --Create *"<<endl;
cout<<"*********************************"<<endl;
CreateList(head);
break;
case 2:
cout<<"*********************************"<<endl;
cout<<"* Node of Friends Book --Insert *"<<endl;
cout<<"*********************************"<<endl;
cout<<"* number(4) name(8) Phone(11) *"<<endl;
cout<<"*********************************"<<endl;
cout<<"Please input the data of record including : number(4) name(8) Phone(11)"<<endl;
p=new ListNode;
cin>>p->data.num>>p->data.name>>p->data.Phone;
InsertNode(head,p);
break;
case 3:
cout<<"*********************************"<<endl;
cout<<"* Node of Friends Book --Search *"<<endl;
cout<<"*********************************"<<endl;
p=ListFind(head);
if(p)
{
cout<<" number(4) name(8) Phone(11) "<<endl;
cout<<"*********************************"<<endl;
cout<<setw(6)<<p->data.num <<setw(10)<<p->data.name<<setw(13)<<p->data.Phone<<endl;
cout<<"*********************************"<<endl;
}
else
cout<<"Not Found\n\n";
break;
case 4:
cout<<"*********************************"<<endl;
cout<<"* Node of Friends Book --Delete *"<<endl;
cout<<"*********************************"<<endl;
DelNode(head);
break;
case 5:
cout<<"*********************************"<<endl;
cout<<"* LinkList of Friends Book --Show *"<<endl;
cout<<"*********************************"<<endl;
PrintList(head);
break;
case 6:
cout<<"\t Quit! \n";
DestroyList(head);
return 0;
}
}
} |
|