双向链表插入、删除节点(详细图解+代码)

双向链表

在单链表的基础上增加了前缀结点,一定程度上提升了查找元素的速度

 在查找元素时,可以反向查找前缀结点

插入结点过程(分为4步)

这四个步骤的顺序可调整,但是必须要保证需要的链不断!!比如,如果先做④再做①,画红叉的链断了,p->next为新的结点node了,那么node又怎么和后面的接结点连上呢?后面的结点已经不能通过p来找到了。

删除结点(分两步)

代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef int ElemType;
/*双向链表的结点,包含一个数据域,两个指针域*/
typedef struct DoubleNode{
    ElemType data;
    struct DoubleNode * prev; // 指向前缀结点
    struct DoubleNode * next; //指向后继结点
}DoubleNode,* DoubleLinkList;


DoubleLinkList InitDoubleLinkList()
{
    DoubleLinkList dlLink;
    dlLink = (DoubleLinkList)malloc(sizeof(DoubleNode));
    if(dlLink == NULL)
        cout << "申请空间失败!";
    dlLink->next = NULL;
    return dlLink;
}
void OutPut(DoubleLinkList dlLink)
{
    DoubleLinkList temp = dlLink;
    cout << "此双向链表为:";
    while(temp->next != NULL)
    {
        temp = temp->next;
        cout << temp->data << " ";
    }
    cout << endl;
}
/*头插法始终有问题,第49行不报错,但是调试的时候会退出,希望有好心人士帮忙解答一下*/
/*void CreatDoubleLinkListHead(DoubleLinkList &dlLink,int length)
{
    DoubleLinkList node = NULL;
    for(int i = 0;i <length;i++)
    {
        node = (DoubleLinkList)malloc(sizeof(DoubleNode));
        cout << "输入第" << i+1 <<"个元素:";
        ElemType x;
        cin >> x;
        node->data = x;

        node->next = dlLink->next;
        dlLink->next->prev = node;
        node->prev = dlLink;
        dlLink->next = node;
    }
}*/

void CreatDoubleLinkListTail(DoubleLinkList &head,int length)
{
    DoubleLinkList node = NULL;
    DoubleLinkList tail = head;
    for(int i = 0;i < length;i++)
    {
        node = (DoubleLinkList)malloc(sizeof(DoubleNode));
        cout << "输入第" << i+1 <<"个元素:";
        ElemType x;
        cin >> x;
        node->data = x;

        tail->next = node;
        node ->prev = tail;
        tail = node;

    }
    tail ->next = NULL;
}
void InsertNode(DoubleLinkList &dlLink,int pos,ElemType element)
{
    DoubleLinkList node = (DoubleLinkList)malloc(sizeof(DoubleNode));
    DoubleLinkList p = dlLink;
    int j = 1;
    while(j < pos)
    {
        p = p->next;
        j++;
    }
    //此时的p为前驱结点
    node->data = element;

    node->next = p->next;
    p->next->prev = node;
    p->next = node;
    node->prev = p;
}
void DeleteNode(DoubleLinkList &dlLink,int pos)
{
    DoubleLinkList p = dlLink;
    DoubleLinkList deleteNode;
    int j = 1;
    while(j < pos)
    {
        p = p->next;
        j++;
    }
     //此时的p为前驱结点
     deleteNode = p->next;
     deleteNode->next ->prev = p;
     p->next = deleteNode->next;
     free(deleteNode);


}

int main()
{
    DoubleLinkList dlLink = InitDoubleLinkList();
    int length;
    cout << "请输入链表长度:";
    cin >> length;
   // CreatDoubleLinkListHead(dlLink,length);
   CreatDoubleLinkListTail(dlLink,length);
    OutPut(dlLink);
    cout << "请输入要插入的元素:";
    int element;
    cin >> element;
    cout << "请输入要插入的位置:";
    int pos;
    cin >> pos;
    InsertNode(dlLink,pos,element);
    OutPut(dlLink);
    cout << "请输入要删除元素的位置:";
    cin >> pos;
    DeleteNode(dlLink,pos);
    OutPut(dlLink);
    return 0;
}

 

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页