其他分享
首页 > 其他分享> > 双向链表

双向链表

作者:互联网

建表

typedef struct Dnode
{
    int data;
    Dnode *prior;
    Dnode *next;
}*Dlink;

Dlink p,q,s,H;

void D_init(Dlink &L)
{
    L = new Dnode;
    L->next = L;
    L->prior = L;
}

插入

void D_insert(Dlink &L,int x)//在首位插入
{
    s = new Dnode;
    s->data = x;
    s->prior = L;
    L->next->prior = s;    
    s->next = L->next;
    L->next = s;
}

删除

void D_delete(Dlink &L,int x)//找到与x值相等的数并删除,若没有则忽略
{
    p = L->next;
    while(p && p != L)
    {
        if(p->data  == x)
        {
            p->next->prior=p->prior;
            p->prior->next=p->next;
            delete(p);
            break;
        }
        p = p->next;
    }
}

删除第一个/最后一个

void D_deleteFirst(Dlink &L)
{
    p = L->next;
    L->next->next->prior = L;
    L->next = L->next->next;
    delete(p);
}

void D_deleteLast(Dlink &L)
{
    p = L->prior;
    L->prior = p->prior;
    p->prior->next = L;
    delete(p);
}

 

标签:Dlink,void,next,链表,prior,双向,Dnode,delete
来源: https://www.cnblogs.com/Cindy-Chan/p/11182033.html