请教各位大神,我的程序执行的时候无法终止,case1和3有这个问题,用断点看了半天没看懂,用loop ,goto loop 的方法也试过,没有解决问题。请教。
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
using namespace std;
typedef int ElemType;
struct Addressbook
{
ElemType data;
Addressbook *next;
};
class LinkList
{
private :
Addressbook *Head;
public:
LinkList ();
~LinkList(){}
void Creat (int n);
void Display();
void Insert (int i,ElemType e);
void Delete(int i);
};
LinkList::LinkList()
{
Head=new Addressbook;
Head->next=NULL;
}
void LinkList::Creat(int n)
{
int i,length;
Addressbook *p,*q;
length=n;
if(length>0)
{
cout<<"Input data elem:";
for(i=1;i<=length;i++)
{
p=new Addressbook;
cin>>p->data;
p->next=NULL;
if(i==1)
Head->next=p;
else
q->next=p;
q=p;
}
}
else
cout<<"Length Error!"<<endl;
}
void LinkList::Display()
{
Addressbook *p;
p=Head->next;
if(p==NULL)
cout<<"LinkList Is Empty!"<<endl;
else
{
cout<<"Data Elem:";
while(p!=NULL)
{
cout<<"通讯录人数"<<endl;
cout<<"姓名 性别 住址 电话号码"<<endl;
cout<<setw(20)<<p->data;
p=p->next;
}
}
cout<<endl;
}
void LinkList::Insert (int i,ElemType e)
{
Addressbook *p,*q,*s;
int k=1;
q=Head;
p=Head->next;
while(k<i&&p!=NULL)
{
q=p;
p=p->next;
k++;
}
if (k==i)
{
s=new Addressbook;
s->data=e;
s->next=p;
q->next=s;
cout<<"Insert Success!"<<endl;
}
else
cout<<"Insert Location i Error!"<<endl;
}
void LinkList::Delete(int i)
{
Addressbook *p,*q;
int k=1;
q=Head;
p=Head->next;
while(k<i&&p!=NULL)
{
q=p;
p=p->next;
k++;
}
if(k==i)
{
q->next=p->next;
delete p;
cout<<"已删除通讯录信息"<<endl;
}
else
{
cout <<"Delete Location Error!"<<endl;
}
}
void main()
{
int i,n,choice=0;
ElemType e;
LinkList ll;
do
{
cout<<"************************************"<<endl;
cout<<"***********通讯录信息管理系统*******"<<endl;
cout<<"***************1建立****************"<<endl;
cout<<"***************2查询****************"<<endl;
cout<<"***************3插入****************"<<endl;
cout<<"***************4删除****************"<<endl;
cout<<"***************0退出****************"<<endl;
cout<<"************************************"<<endl;
cout<<"请选择0-4";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"请输入人数";
cin>>n;
ll.Creat(n);
break;
/*goto loop;*/
}
loop:case 2:
{
ll.Display();
break;
}
case 3:
{
cout<<"请输入要插入位置";
cin>>i;
cout<<"请输入要插入的通讯录信息逗号隔开"<<endl;
cout<<"姓名,性别,住址,电话号码"<<endl;
cin>>e;
ll.Insert(i,e);
break;
/*goto loop;*/
}
case 4:
{
cout<<"请输入要删除的位置";
cin>>i;
ll.Delete(i);
break;
}
case 0:break;
default:cout<<"请输入正确数字"<<endl;
}
}while(choice);
}