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

双链表

作者:互联网

双链表

与单链表相比双链表访问前后相邻结点更加灵活

代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef struct DNode(){
    int data;
    struct DNode *prior, *next;
}DNode, *DLinkList;

//初始化双链表
bool InitDLinkList(DLinkList &L){
    L = (DLNode *)malloc(sizeof(DNode));
    if (L == NULL)
        return false;
    L->prior = NULL;
    L->next = NULL;
    return true;
}

//删除p结点的后继结点
bool DeleteNextDNode(DNode *p){
    if (p == NULL) return false;
    DNode *q = p->next;
    if (q == NULL) return false;
    p->next = q->next;
    if (q->next != NULL)
        q->next->prior = p;
    free(q);
    return true;
}

//销毁链表
bool DestoryList(DLinkList &L){
    while (L->next != NULL)
        DeleteNextDNode(L);
    free(L);
    L = NULL;
    return true;
}

//在p结点后插入s结点
bool InsertNextDNode(DNode *p,DNode *s){
    if (p == NULL || s == NULL)
        return false;
    s->next = p->next;
    if (p->next != NULL)//如果p有后继结点
        p->next->prior = s;
    s->prior = p;
    p->next = s;
}

//双链表的遍历
void PrintList(DLinkList &L){
    DNode *p = L;
    //后向遍历
    while (p != NULL){
        p = p->next;
    }
    //前向遍历
    while (p != NULL){
        p = p->prior;
    }
    //前向遍历(跳过头结点)
    while (p->prior != NULL){
        p = p->prior;
    }
}


int main()
{
    DLinkList L;
    InitDLinkList(L);
    return 0;
}

标签:结点,return,next,prior,双链,NULL,DNode
来源: https://www.cnblogs.com/lijiaji/p/15035983.html