#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
const int namelength=20;
const int phonelength=20;
struct employee//结构体
{
char name[namelength];
char num[phonelength];
long ID;
struct* next;
};
struct employee* creat(void);//创建链表
struct employee* insert(struct employee*);//插入链表
void show(struct employee*);//显示链表
struct employee* modify(struct employee*);//修改链表
struct employee* DELETE(struct employee*);//删除链表
//函数声明
void main()//主函数
{long id;
char answer;
struct employee* head=NULL;
cout<<"A.creat an initial linked list of names and phone numbers"<<endl;//界面窗口
cout<<"B.insert a new structure into the linked list"<<endl;
cout<<"C.modify an existing structure in the linked list"<<endl;
cout<<"D.delete an existing structure from the list"<<endl;
cout<<"E.exit from the program"<<endl;
do
{
cout<<"ENTER YOUR CHOICE"<<endl;
cin>>answer;
switch(answer)
{
case 'A':
head=creat();
break;
case 'B':
head=insert(head);
break;
case 'C':
head=modify(head);
break;
case 'D':
cout<<"please enter the id of which you want to delete"<<endl;
cin>>id;
head=DELETE(head);
break;
case 'E':
system("cls");
break;
default:
break;
}
show(head);
cout<<"continue?Y/N"<<endl;
}while(answer=='y'||answer=='Y');
}
struct employee* creat(void)
{
int n=0;
struct employee *head,*p1,*p2;
p1=p2=new employee;
head=NULL;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;
while(p1->ID)
{n++;
if(n==1)
{
head=p1;
p1->next=NULL;
}
else
p2->next=p1;
p2=p1;
p1=new employee;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;
}
p2->next=NULL;
cout<<"the new linked list created successfully!"<<endl;
return(head);
}
struct employee* insert(struct employee* head)
{
struct employee newlist;
cout<<"please initial the new list(name number ID)"<<endl;
cin>>newlist.name>>newlist.num>>newlist.ID;
newlist.next=NULL;
struct employee *p0=&newlist,*p1,*p2;
p1=head;
if(head==NULL)
{head=p0;
p0->next=NULL;
}
else
while(p1->next!=NULL&&p1->ID<p0->ID)
{p2=p1;
p1=p1->next;
}
if(p1->ID<p0->ID)
{p1->next=p0;
p0->next=NULL;
}
else
{p2->next=p0;
p0->next=p1;
}
cout<<"insert successfully!"<<endl;
return(head);
}
void show(struct employee* head)
{
cout<<"date below"<<endl;
cout<<"******************************************************"<<endl;
while(head->next)
{cout<<head->name<<" "<<head->num<<" "<<head->ID<<endl;
head=head->next;
}
}
struct employee* modify(struct employee*head)
{
struct employee* p;
int temp;
long id;
cout<<"employee in which ID you want to modify?"<<endl;
cin>>id;
while(p->next!=NULL&&p->ID!=id)
p=p->next;
if(p->ID=id)
{
cout<<"which one do you want to madify 1.name 2.num?"<<endl;
cin>>temp;
if(temp=1)
cin.getline(p->name,namelength);
else if(temp=2)
cin.getline(p->num,phonelength);
}
else
cout<<"fail to modify!"<<endl;
return(head);
}
struct employee* DELETE(struct employee*head)
{
struct employee *p1,*p2;
p1=p2=head;
long id;
cout<<"which one do you want to delete?"<<endl;
cin>>id;
while(p1->next!=NULL&&p1->ID!=id)
{
p2=p1;
p1=p1->next;
}
if(p1->ID=id)
p2->next=p1->next;
else
cout<<"can't find the list!"<<endl;
return (head);
}
运行一下一个同样错误出现8次。。。可是我找不到。。。指针对指针赋值应该没关系的啊