双向链表添加元素
前插法
#include "stdio.h"
#include "stdlib.h"


typedef struct DoubleLinkNode{
    int data;
//    下一个节点的指针域
    struct DoubleLinkNode *next;
//    上一个节点的指针域
    struct DoubleLinkNode *prev;
}DoubleLinkNode, DoubleLinkList;
//DoubleLinkList为指向结构体的指针

bool InitDoubleList(DoubleLinkList* &L){
    L = new DoubleLinkNode;
//    头结点分配失败
    if(!L){
        return false;
    }
//    next指针域和prev指针域都置空
    L->next = NULL;
    L->prev = NULL;
    L->data = -1;
    return true;
}


bool FrontInsertDoubleList(DoubleLinkList* &L, DoubleLinkNode *node){
    if(!L|| !node){
        return false;
    }
//    假如只存在头结点
    if(L->next == NULL){
//        新节点的prev指针执向头结点
        node->next = NULL;
//        头结点的next指针指向新节点
        node->prev = L;
        L->next = node;
    } else{
//        第二个节点的prev指向新节点
        L->next->prev = node;
//        新节点next指针指向第二个节点
        node->next = L->next;
//        新节点的prev指针指向头节点
        node->prev = L;
//        头结点next指针指向新节点
        L->next = node;
    }

    return true;
}


void DisplayDoubleList(DoubleLinkList * &L){
    DoubleLinkNode *p = NULL;
    if(!L){
        printf("链表为空\n");
        return;
    }
    p = L;
    while (p->next){
        printf("%d, ", p->next->data);
        p = p->next;
    }

//    反向打印
    printf("\n反向打印\n");
    while (p){
        printf("%d, ", p->data);
        p = p->prev;
    }
    printf("\n");
}


int main(){
    DoubleLinkList *L = NULL;
    DoubleLinkNode *s = NULL;
    InitDoubleList(L);
    int n = 16;
    printf("使用前插法\n");
    while (n > 0){
        s = new DoubleLinkNode;
        s->data = n;
        FrontInsertDoubleList(L, s);
        n--;
    }
    DisplayDoubleList(L);
    return 0;
}

双向链表_链表

后插法
#include "stdio.h"
#include "stdlib.h"


typedef struct DoubleLinkNode{
    int data;
//    下一个节点的指针域
    struct DoubleLinkNode *next;
//    上一个节点的指针域
    struct DoubleLinkNode *prev;
}DoubleLinkNode, DoubleLinkList;
//DoubleLinkList为指向结构体的指针

bool InitDoubleList(DoubleLinkList* &L){
    L = new DoubleLinkNode;
//    头结点分配失败
    if(!L){
        return false;
    }
//    next指针域和prev指针域都置空
    L->next = NULL;
    L->prev = NULL;
    L->data = -1;
    return true;
}


bool RearInsertDoubleList(DoubleLinkList* &L, DoubleLinkNode *node){
    DoubleLinkNode *last = NULL;
    if(!L || !node){
        return false;
    }
    last = L;
    while (last->next){
        last = last->next;
    }
    node->next = NULL;
    last->next = node;
    node->prev = last;
    return true;
}


void DisplayDoubleList(DoubleLinkList * &L){
    DoubleLinkNode *p = NULL;
    if(!L){
        printf("链表为空\n");
        return;
    }
    p = L;
    while (p->next){
        printf("%d, ", p->next->data);
        p = p->next;
    }

//    反向打印
    printf("\n反向打印\n");
    while (p){
        printf("%d, ", p->data);
        p = p->prev;
    }
    printf("\n");
}


int main(){
    DoubleLinkList *L = NULL;
    DoubleLinkNode *s = NULL;
    InitDoubleList(L);
    int n = 16;
    printf("使用后插法\n");
    while (n > 0){
        s = new DoubleLinkNode;
        s->data = n;
        RearInsertDoubleList(L, s);
        n--;
    }
    DisplayDoubleList(L);
    return 0;
}

双向链表_#include_02

任意位置插入
#include "stdio.h"
#include "stdlib.h"


typedef struct DoubleLinkNode{
    int data;
//    下一个节点的指针域
    struct DoubleLinkNode *next;
//    上一个节点的指针域
    struct DoubleLinkNode *prev;
}DoubleLinkNode, DoubleLinkList;
//DoubleLinkList为指向结构体的指针

bool InitDoubleList(DoubleLinkList* &L){
    L = new DoubleLinkNode;
//    头结点分配失败
    if(!L){
        return false;
    }
//    next指针域和prev指针域都置空
    L->next = NULL;
    L->prev = NULL;
    L->data = -1;
    return true;
}


bool RearInsertDoubleList(DoubleLinkList* &L, DoubleLinkNode *node){
    DoubleLinkNode *last = NULL;
    if(!L || !node){
        return false;
    }
    last = L;
    while (last->next){
        last = last->next;
    }
    node->next = NULL;
    last->next = node;
    node->prev = last;
    return true;
}


bool InsertDoubleList(DoubleLinkList* &L, int pos, int value){
    if(!L || !L->next || pos < 1){
        return false;
    }
    int count = 0;
    DoubleLinkList *p, *s;
    p = L;
//    把pos找到,p指向该节点
    while (p && count < pos){
        p = p->next;
        count++;
    }
    if(!p || count != pos){
        printf("第%d个节点不存在\n", pos);
        return false;
    }

    s = new DoubleLinkNode;
    s->data = value;
    s->next = p;
    s->prev = p->prev;
    p->prev->next = s;
    p->prev = s;
    return true;
}


void DisplayDoubleList(DoubleLinkList * &L){
    DoubleLinkNode *p = NULL;
    if(!L){
        printf("链表为空\n");
        return;
    }
    p = L;
    while (p->next){
        printf("%d, ", p->next->data);
        p = p->next;
    }
    printf("\n");
}


int main(){
    DoubleLinkList *L = NULL;
    DoubleLinkNode *s = NULL;
    InitDoubleList(L);
    int n = 16;
    printf("使用后插法\n");
    while (n > 0){
        s = new DoubleLinkNode;
        s->data = n;
        RearInsertDoubleList(L, s);
        n--;
    }
    DisplayDoubleList(L);

//    任意位置插入
    int pos = 5, value = 888;
    InsertDoubleList(L, pos, value);
    printf("任意插入后的结果:\n");
    DisplayDoubleList(L);
    return 0;
}

双向链表_链表_03

按照索引获取值
#include "stdio.h"
#include "stdlib.h"


typedef struct DoubleLinkNode{
    int data;
//    下一个节点的指针域
    struct DoubleLinkNode *next;
//    上一个节点的指针域
    struct DoubleLinkNode *prev;
}DoubleLinkNode, DoubleLinkList;
//DoubleLinkList为指向结构体的指针

bool InitDoubleList(DoubleLinkList* &L){
    L = new DoubleLinkNode;
//    头结点分配失败
    if(!L){
        return false;
    }
//    next指针域和prev指针域都置空
    L->next = NULL;
    L->prev = NULL;
    L->data = -1;
    return true;
}


bool RearInsertDoubleList(DoubleLinkList* &L, DoubleLinkNode *node){
    DoubleLinkNode *last = NULL;
    if(!L || !node){
        return false;
    }
    last = L;
    while (last->next){
        last = last->next;
    }
    node->next = NULL;
    last->next = node;
    node->prev = last;
    return true;
}


bool GetElem(DoubleLinkList* &L, int pos, int &value){
    int index = 1;
    DoubleLinkList *p;
    if(!L || !L->next){
        return false;
    }
    p = L->next;
    while (p && index < pos){
        p = p->next;
        index++;
    }
    if(!p || index > pos){
        return false;
    }
    value = p->data;
    return true;
}

void DisplayDoubleList(DoubleLinkList * &L){
    DoubleLinkNode *p = NULL;
    if(!L){
        printf("链表为空\n");
        return;
    }
    p = L;
    while (p->next){
        printf("%d, ", p->next->data);
        p = p->next;
    }
    printf("\n");
}


int main(){
    DoubleLinkList *L = NULL;
    DoubleLinkNode *s = NULL;
    InitDoubleList(L);
    int n = 16;
    printf("使用后插法\n");
    while (n > 0){
        s = new DoubleLinkNode;
        s->data = n;
        RearInsertDoubleList(L, s);
        n--;
    }
    DisplayDoubleList(L);
    int value = 0, pos = 3;
    if(GetElem(L, pos, value)){
        printf("成功,第%d个的值为%d\n", pos, value);
    } else{
        printf("查找失败\n");
    }

    return 0;
}

双向链表_#include_04

任意节点的删除
#include "stdio.h"
#include "stdlib.h"


typedef struct DoubleLinkNode{
    int data;
//    下一个节点的指针域
    struct DoubleLinkNode *next;
//    上一个节点的指针域
    struct DoubleLinkNode *prev;
}DoubleLinkNode, DoubleLinkList;
//DoubleLinkList为指向结构体的指针

bool InitDoubleList(DoubleLinkList* &L){
    L = new DoubleLinkNode;
//    头结点分配失败
    if(!L){
        return false;
    }
//    next指针域和prev指针域都置空
    L->next = NULL;
    L->prev = NULL;
    L->data = -1;
    return true;
}


bool RearInsertDoubleList(DoubleLinkList* &L, DoubleLinkNode *node){
    DoubleLinkNode *last = NULL;
    if(!L || !node){
        return false;
    }
    last = L;
    while (last->next){
        last = last->next;
    }
    node->next = NULL;
    last->next = node;
    node->prev = last;
    return true;
}


bool DeleteDoubleLink(DoubleLinkList* &L, int pos){
    DoubleLinkList  *p;
    int index = 0;
    p = L;
//    pos < 1保证头结点不被删除
    if(!L || !L->next || pos < 1){
        return false;
    }
    while (p && index < pos){
        p = p->next;
        index++;
    }
//    节点不存在
    if(!p){
        return false;
    }
//    改变删除节点前驱节点的next指针域
    p->prev->next = p->next;
   if(p->next){
//       改变删除节点的后继节点的prev指针域
       p->next->prev = p->prev;
   }
//   删除节点,释放空间
    delete p;
    return true;
}


void DisplayDoubleList(DoubleLinkList * &L){
    DoubleLinkNode *p = NULL;
    if(!L){
        printf("链表为空\n");
        return;
    }
    p = L;
    while (p->next){
        printf("%d, ", p->next->data);
        p = p->next;
    }
    printf("\n");
}


int main(){
    DoubleLinkList *L = NULL;
    DoubleLinkNode *s = NULL;
    InitDoubleList(L);
    int n = 16;
    printf("使用后插法\n");
    while (n > 0){
        s = new DoubleLinkNode;
        s->data = n;
        RearInsertDoubleList(L, s);
        n--;
    }
    DisplayDoubleList(L);
    int pos = 5;
    DeleteDoubleLink(L,pos);
    printf("删除之后:\n");
    DisplayDoubleList(L);
    return 0;
}

双向链表_#include_05

链表的销毁
#include "stdio.h"
#include "stdlib.h"


typedef struct DoubleLinkNode{
    int data;
//    下一个节点的指针域
    struct DoubleLinkNode *next;
//    上一个节点的指针域
    struct DoubleLinkNode *prev;
}DoubleLinkNode, DoubleLinkList;
//DoubleLinkList为指向结构体的指针

bool InitDoubleList(DoubleLinkList* &L){
    L = new DoubleLinkNode;
//    头结点分配失败
    if(!L){
        return false;
    }
//    next指针域和prev指针域都置空
    L->next = NULL;
    L->prev = NULL;
    L->data = -1;
    return true;
}


bool RearInsertDoubleList(DoubleLinkList* &L, DoubleLinkNode *node){
    DoubleLinkNode *last = NULL;
    if(!L || !node){
        return false;
    }
    last = L;
    while (last->next){
        last = last->next;
    }
    node->next = NULL;
    last->next = node;
    node->prev = last;
    return true;
}


void DestoryDoubleList(DoubleLinkList* &L){
    DoubleLinkList *p = L;
    printf("开始销毁此链表\n");
    while (p){
//        因为后面要删除节点所以p=p->next,不好处理
//        所以我们借助完成遍历,每次给p赋值并删除,完成了遍历
        L = L->next;
        printf("删除%d, ", p->data);
        delete p;
        p = L;
    }
}


void DisplayDoubleList(DoubleLinkList * &L){
    DoubleLinkNode *p = NULL;
    if(!L){
        printf("链表为空\n");
        return;
    }
    p = L;
    while (p->next){
        printf("%d, ", p->next->data);
        p = p->next;
    }
    printf("\n");
}


int main(){
    DoubleLinkList *L = NULL;
    DoubleLinkNode *s = NULL;
    InitDoubleList(L);
    int n = 16;
    printf("使用后插法\n");
    while (n > 0){
        s = new DoubleLinkNode;
        s->data = n;
        RearInsertDoubleList(L, s);
        n--;
    }
    DisplayDoubleList(L);
    DestoryDoubleList(L);
    return 0;
}

双向链表_链表_06