哪位大侠帮忙看看,双链表插入这块出问题了。
[code=C/C++][/code]
#include "iostream"
typedef int Stutus;
typedef int Elemplty;
#define OK 1
#define ERROR -1
using namespace std;
typedef struct DoubleNode
{
int data;
struct DoubleNode *next,*prior;
}DoubleNode,*DoubleList;
Stutus CreateList(DoubleList &L,int n)
{
L=(DoubleList)malloc(sizeof(DoubleNode));
L->next=L->prior=L;
cin>>L->data;
for (int i = n-1; i > 0; --i)
{
DoubleNode *p=(DoubleList)malloc(sizeof(DoubleNode));
cin>>p->data;
p->prior=L->prior;
L->prior->next=p;
p->next=L;
L->prior=p;
}
cout<<endl;
return OK;
}
Elemplty InsertList(DoubleList &L,int i,int e)
{
DoubleNode *p=(DoubleList)malloc(sizeof(DoubleNode));
int j=0;
p=L;
while(p&&j<i-1)
{
p=p->next;
++j;
}
p->data=e;
p->prior=L->prior;
L->prior->next=p;
p->next=L;
L->prior=p;
if(!p||j>i-1)return ERROR;
//p=(DoubleList)malloc(sizeof(DoubleNode));
return OK;
}
Stutus DeleteList(DoubleList &L,int i)
{
DoubleList s,p;
int k=0;
p=L;
while(p!=NULL&&k<i)
{
p=p->next;
k++;
}
s=(DoubleList)malloc(sizeof(DoubleNode));
if(s)
{
//e=p->data;
p->next->prior=p->prior;
p->prior->next=p->next;
free(p);
}
return OK;
}
void PrintList(DoubleList &L,int n)
{
DoubleList p;
p=L;
int i=0;
while(i<n)
{
++i;
cout<<p->data;
p=p->next;
cout<<" ";
}
cout<<endl;
}
void main()
{
int i,n,e;
DoubleList La;
cout<<"please input DoubleList num : "<<endl;
cin>>n;
CreateList(La,n);
PrintList(La,n);
cout<<"please insert DoubleList : "<<endl;
cin>>i>>e;
InsertList(La,i,e);
PrintList(La,i);
cout<<"please Detele DoubleList : "<<endl;
cin>>i;
DeleteList(La,i);
PrintList(La,i);
}
[解决办法]
- C/C++ code
#include "iostream"typedef int Stutus;typedef int Elemplty;#define OK 1#define ERROR -1using namespace std;typedef struct DoubleNode{int data;struct DoubleNode *next,*prior;}DoubleNode,*DoubleList;Stutus CreateList(DoubleList &L,int n){ L=(DoubleList)malloc(sizeof(DoubleNode)); L->next=L->prior=L;cin>>L->data;for (int i = n-1; i > 0; --i){DoubleNode *p=(DoubleList)malloc(sizeof(DoubleNode));cin>>p->data;p->prior=L->prior;L->prior->next=p;p->next=L;L->prior=p;}cout<<endl;return OK;}Elemplty InsertList(DoubleList &L,int i,int e){DoubleNode *p, *q=(DoubleList)malloc(sizeof(DoubleNode));//改DoubleNode *p=(DoubleList)malloc(sizeof(DoubleNode));int j=0;p=L; while(p&&j<i-1){p=p->next;++j;}q->data=e;//改p->data=e;q->prior=p->prior;//改p->prior=L->prior;p->prior->next=q;//改L->prior->next=p;q->next=p;//改p->next=L;p->prior=q;//改L->prior=p;if(!p||j>i-1)return ERROR;//p=(DoubleList)malloc(sizeof(DoubleNode));return OK;}Stutus DeleteList(DoubleList &L,int i){DoubleList s,p;int k=0;p=L;while(p!=NULL&&k<i){p=p->next; k++;}//改s=(DoubleList)malloc(sizeof(DoubleNode));//改if(s)//改{//e=p->data;p->next->prior=p->prior;p->prior->next=p->next;free(p);//改}return OK;}void PrintList(DoubleList &L,int n){ DoubleList p;p=L;int i=0;while(i<n){++i; cout<<p->data;p=p->next;cout<<" ";}cout<<endl;}void main(){int i,n,e;DoubleList La;cout<<"please input DoubleList num : "<<endl;cin>>n;CreateList(La,n); PrintList(La,n); cout<<"please insert DoubleList : "<<endl;cin>>i>>e;InsertList(La,i,e); PrintList(La,n+1);//改PrintList(La,i);cout<<"please Detele DoubleList : "<<endl;cin>>i;DeleteList(La,i);PrintList(La,n);//改PrintList(La,i);}