?? 5_44.cpp
字號:
# include<iostream.h>
#include <iomanip.h>
# include<string.h>
struct date
{
int year; int month; int day;
};
struct friends
{
char name[10]; char sex;char tel[12]; date birthday;friends* next;
};
int n; //動態分配結構friends的個數
struct friends* create() //新建鏈表,此函數返回一個指向鏈表頭的指針
{
struct friends* head,*p1,*p2;
n=0;
p1=p2=new friends;
cout<<"Input my friends'name ,with'#' to end:"<<endl;
cin>>p1->name;
cout<<"Continue to input my friends'sex(F/M),tel and birthday:"<<endl;
cin>>p1->sex;
cin>>p1->tel;
cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
head=NULL;
while (strcmp(p1->name,"#")!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new friends;
cout<<"Input my friends'name ,with'#' to end:"<<endl;
cin>>p1->name;
if(strcmp(p1->name,"#")!=0)
{
cout<<"Continue to input my friends'sex(F/M),tel and birthday:"<<endl;
cin>>p1->sex;
cin>>p1->tel;
cin>>p1->birthday.year>>p1->birthday.month>>p1->birthday.day;
}
}
p2->next=NULL;
return head;
}
void print(struct friends* head) //輸出鏈表
{
struct friends* p;
p=head;
cout<<setw(11)<<"name"<<setw(5)<<"sex"<<setw(12)<<"telNO."<<setw(16)<<"birthday"<<endl;
while(p!=NULL)
{
cout<<setw(11)<<p->name<<setw(4)<<p->sex<<setw(16)<<p->tel<<" ";
cout<<setw(5)<<p->birthday.year;
cout<<setw(3)<<p->birthday.month<<" ";
cout<<setw(2)<<p->birthday.day<<endl;
p=p->next;
}
}
struct friends*DELETE(struct friends*head,char name[])
{
struct friends*p1,*p2;
if(head==NULL)
{
cout<<"list null!DO NOT DELETE!";
return(head);
}
p1=head;
while(strcmp(p1->name,name)!=0 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
delete p1;
cout<<"delete:"<<name;
}
else
cout<<name<<"has not been found!"<<endl;
return head;
}
struct friends*insert(struct friends*head,struct friends*frd)
{
struct friends*p0,*p1,*p2;
p1=head;
p0=frd;
if(head==NULL)
{
head=p0; p0->next=NULL;
}
else
while(strcmp(p0->name,p1->name)>0 && p1->next!=NULL)
{
p2=p1;p1=p1->next;
}
if (strcmp(p0->name,p1->name)<=0 )
{
if(head==p1)
{
head=p0;
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
n++;
return head;
}
void main()
{
char del[10];
friends*s,*t;
s=create();
print(s);
cout<<"Input the name you want deleted:";
cin>>del;
s=DELETE(s,del);
cout<<endl;
print(s);
cout<<"Input the name you want inserted:";
t=new friends;
cin>>t->name>>t->sex>>t->tel>>t->birthday.year>>t->birthday.month>>
t->birthday.day;
s=insert(s,t);
print(s);
delete t;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -