双向链表

建表

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);
}

 

上一篇:Start with connect by prior 阶层式查询用法


下一篇:数据结构—习题2.9 交换双向循环链表的结点p和它的前驱结点