双向链表的创建,错误如图


#include  <iostream>

using  namespace  std;

int  OK  =  0;
int  ERROR  =  1;

typedef  char  ElemType;

struct  LNode
{
        ElemType      data;      //数据域
        LNode                *prev;      //指针域
        LNode                *next;      //指针域
};

//  *LinkList为Lnode类型的指针
int  InitList_L(LNode  *&L)
{
        
L = new LNode;
L->next = NULL;

        
        return  OK;
}

//在线性表L中查找值为e的数据元素
LNode  *LocateELem_L(LNode  *L,  ElemType  e)
{
//返回L中值为e的数据元素的地址,查找失败返回NULL
        
LNode  *p;
p=L->next;
while(p&&p->data!=e)
p=p->next;
return p;

}
//在L中第i个元素之前插入数据元素e
int  ListInsert_L(LNode  *&L,int  i,ElemType  e)
{
        LNode  *p=L;
        int  j=0;
        while(p  &&  j<i-1)
        {
                p=p->next;        //寻找第i−1个结点
                ++j;
        }
        if(!p||j>i-1)
                return  ERROR;

        
LNode *s;
 s=new LNode;
    s->data=e;
s->next=p->next;
p->next->prev=s;
s->prev=p;
p->next=s;



        return  OK;
}//ListInsert_L
//将线性表L中第i个数据元素删除
int  ListDelete_L(LNode  *  &L,int  i,ElemType  &e)
{
        LNode  *p=L;
        int  j=0;
        while(p->next  &&j<i)  //寻找第i个结点,并令p指向其前驱
        {
                p=p->next;
                ++j;
        }
        if(!p)  return  ERROR;  //删除位置不合理

        
LNode *q;
    q = new LNode;
    q = p->next;
    e = q->data;
    p->next = q->next;
    q->next->prev = p;
    delete q;

        return  OK;
}//ListDelete_L
int  DestroyList_L(LNode  *&L)
{
        
L = new LNode;
L->next = NULL;


        L  =  NULL;
        return  OK;
}
void  CreateList_F(LNode  *&L,int  n)
{
        if  (InitList_L(L)!=  OK)  return;
          //先建立一个带头结点的单链表
        for(int  i=n;  i>0;  --i)
        {
                LNode  *p=new  LNode;  //生成新结点
                cin>>p->data;  //输入元素值

                p->prev  =  L;
                p->next=L->next;
                if  (L->next  !=  NULL)
                        L->next->prev  =  p;
                L->next=p;          //插入到表头
        }
}//CreateList_F
void  output(LNode  *L)
{
        L  =  L->next;
        while(L!=NULL)
        {
                cout<<L->data<<'  ';
                L  =  L->next;
        }
        cout<<endl;
}
void  invoutput(LNode  *L)
{
        LNode  *T  =  L;
        while(T->next!=NULL)
        {
                T  =  T->next;
        }
        while(T!=L)
        {
                cout<<T->data<<'  ';
                T  =  T->prev;
        }
        cout<<endl;
}
int  main()
{
        LNode  *head;

        //创建链表
        int  n;
        cin>>n;
        CreateList_F(head,  n);
        invoutput(head);

        //插入元素
        int  loc;
        ElemType  ne;
        cin>>loc;
        cin>>ne;
        ListInsert_L(head,loc,ne);
        invoutput(head);

        //定位并输出其下一个
        cin>>ne;
        LNode  *pt  =  LocateELem_L(head,  ne);
        if  (pt==NULL)  cout<<"no  found!"<<endl;
        else  if  (pt->next==NULL)  cout<<"NULL"<<endl;
        else  cout<<pt->next->data<<endl;
        invoutput(head);

        //删除元素
        cin>>loc;
        ListDelete_L(head,loc,ne);
        invoutput(head);

        //销毁链表
        DestroyList_L(head);

        return  0;
}



img

输入是什么?不明所以。直接说想要什么效果。发发一个运行截图就好了