这是个链表的基本操作代码实现,怎么运行一半就停了啊?求指教
#include <iostream>using namespace std;
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
void InitList_L(LinkList &);
void DestroyList_L(LinkList &);
void CreatList_L(LinkList &head,int n);
bool ListEmpty_L(LinkList );
int ListLength_L(LinkList);
bool GetElem_L(LinkList,int,ElemType &);
int LocateElem_L(LinkList,ElemType,int(*compare)(ElemType,ElemType));
bool PriorElem_L(LinkList head,ElemType cur_e,ElemType &pre_e);
bool NextElem_L(LinkList head,ElemType cur_e,ElemType &next_e);
bool ListInsert_L(LinkList &head,int i,ElemType e);
bool ListDelete_L(LinkList &head,int i,ElemType &e);
void ListTraverse_L(LinkList head);
int equal(ElemType a,ElemType b);
void InitList_L(LinkList &head)
{
head=new LNode;
head->next=NULL;
}
void DestroyList_L(LinkList &head)
{
LinkList p,q;
p=q=head;
while(p->next)
{
q=p->next;
p->next=q->next;
free(q);
}
}
void CreatList_L(LinkList &head,int n)
{
head=new LNode;
head->next=NULL;
LinkList p;
for(int i=n;i>0;i--)
{
p=new LNode;
ElemType a;
cin>>p->data;
p->next=head->next;
head->next=p;
}
}
bool ListEmpty_L(LinkList head)
{
if(head->next!=NULL)return false;
else return true;
}
int ListLength_L(LinkList head)
{
LinkList p=head;
int i=0;
while(p->next)
i++;
return i;
}
bool GetElem_L(LinkList head,int i,ElemType &e)
{
LinkList p=head;
int j;
for(j=1;p->next!=NULL&&j<=i;j++)
p=p->next;
if(j>i){e=p->data;return true;}
else
return false;
}
int LocateElem_L(LinkList head,ElemType e,int(*compare)(ElemType,ElemType))
{
LinkList p=head;
for(int i=0;p->next!=NULL;p=p->next,i++)
if((*compare)(e,p->next->data))return i+1;
else
return 0;
}
bool PriorElem_L(LinkList head,ElemType cur_e,ElemType &prior)
{
LinkList p=head;
while(p->next->next!=NULL)
{
if(equal(p->next->next->data,cur_e))
{prior=p->next->data;return true;}
p=p->next;
}
return false;
}
bool NextElem_L(LinkList head,ElemType cur_e,ElemType &next)
{
LinkList p=head;
while(p->next->next!=NULL)
{
if(equal(p->next->data,cur_e))
{next=p->next->next->data;return true;}
p=p->next;
}
return false;
}
bool ListInsert_L(LinkList &head,int i,ElemType e)
{
LinkList p=head,s;
int j=0;
while(p&&j<i-1)
{
p=p->next;j++;
}
if(!p||j>i-1)return false;
s=new LNode;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
bool ListDelete_L(LinkList &head,int i,ElemType &e)
{
LinkList p=head,q;
int j=0;
while(p->next&&j<i-1)
{
p=p->next;j++;
}
q=p->next;
p->next=q->next;
e=q->data;
free(q);
return true;
}
void ListTraverse_L(LinkList head)
{
LinkList p=head;
while(p->next!=NULL)
{
cout<<p->next->data<<" ";
p=p->next;
}
cout<<endl;
}
int equal(ElemType a,ElemType b)
{
if(a==b)return 1;
else
return 0;
}
int main()
{
LinkList head;
ElemType e,e1;
int i;
cout<<"1.创建列表head,从键盘倒序输入5个整型元素到线性表head"<<endl;
CreatList_L(head,5);
cout<<"2.输出线性表L"<<endl;
ListTraverse_L(head);
cout<<"3.顺序表长度:"<<ListLength_L(head)<<endl;
cout<<"4.顺序表L"<<(ListEmpty_L(head)?"空":"非空")<<endl;
cout<<"5.请输入顺序表中待查元素位序:";
cin>>i;
if(GetElem_L(head,i,e))
cout<<"顺序表的"<<i<<"个元素是:"<<e<<endl;
else cout<<"输入数据非法!"<<endl;
cout<<"6.请输入线性表的待查元素:";
cin>>e;
if(i=LocateElem_L(head,e,equal))
cout<<"元素"<<e<<"在顺序表中的位序是"<<i<<endl;
else cout<<"元素"<<e<<"不在顺序表中"<<endl;
cout<<"7.请输入线性表的待查元素的前驱:";
cin>>e;
if(i=PriorElem_L(head,e,e1))
cout<<"元素"<<e<<"的前驱是"<<e1<<endl;
else cout<<"元素"<<e<<"没有前驱"<<endl;
cout<<"8.请输入线性表的待查元素的后继:";
cin>>e;
if(i=NextElem_L(head,e,e1))
cout<<"元素"<<e<<"的后继是"<<e1<<endl;
else cout<<"元素"<<e<<"没有后继"<<endl;
cout<<"9.输入要插入顺序表的位置和元素:"<<endl;
cin>>i>>e;
cout<<(ListInsert_L(head,i,e)?"插入成功!":"插入失败!")<<endl;
cout<<"插入后输入线性表L:";
ListTraverse_L(head);
cout<<"10.输入要删除的元素位置:";
cin>>i;
cout<<(ListDelete_L(head,i,e)?"删除成功!":"删除失败!")<<endl;
cout<<"删除后输出线性表L:";
ListTraverse_L(head);
cout<<"11.销毁线性表L."<<endl;
DestroyList_L(head);
return 0;
}