双向链表的增删查改

双向链表顾名思义,链表的每一个节点都有两个指针,一个指向上一个节点,一个指向下一个节点,通过指针将链表的头和尾连起来形成一个循环结构。双向链表的增删查改_第1张图片

SeqList.h

#pragma once
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
typedef int DataType;
typedef struct DListNode {
    struct DListNode* _next;
    struct DListNode* _prev;
    DataType _data;

}DListNode;
DListNode*BuyNode(DataType x)//创建空间
{
    DListNode*newnode = (DListNode*)malloc(sizeof(DListNode));
    newnode->_data = x;
    newnode->_next = newnode;//新创建的节点的自己指向自己构成一个新的循环节点
    newnode->_prev = newnode;

    return newnode;
}
DListNode* DListInit()//头节点的初始化
{
    DListNode* head = BuyNode(0);
    return head;
}
void DListPushFront(DListNode*head, DataType x)//头插
{
    DListNode*newnode = BuyNode(x);
    DListNode*cur = head->_next;
    newnode->_prev = head;
    head->_next = newnode;
    newnode->_next = cur;
    cur->_prev = newnode;
}
void DListPushback(DListNode*head, DataType x)//尾插
{
    DListNode*newnode = BuyNode(x);
    newnode->_next = head;
    newnode->_prev = head->_prev;
    head->_prev->_next=newnode;
    head->_prev = newnode; 
}
DListPopback(DListNode*head)//尾删
{
    if (head->_prev == head)
        return;
    DListNode *back = head->_prev;
    head->_prev = back->_prev;
    back->_prev->_next = head;
    free(back);
}
DListPopFront(DListNode*head)//头删
{
    if (head->_prev == head)
        return;
    DListNode*Front = head->_next;
    DListNode*cur = Front->_next;
    head->_next = cur;
    cur->_prev = head;
    free(Front);
}
DListNode* DListFind(DListNode* head, DataType x)//查找
{
    DListNode *first = head->_next;
    DListNode *end = head->_prev;
    while (first != end)
    {
        if (first->_data == x)
            return first;
        else if (end->_data == x)
            return end;
        first = first->_next;
        end = end->_prev;
    }
    return NULL;
}
void DListInsert(DListNode* pos, DataType x)//插入
{
    DListNode *NewNode = BuyNode(x);
    NewNode->_prev = pos->_prev;
    NewNode->_next = pos;
    pos->_prev->_next = NewNode;
    pos->_prev = NewNode;
}
void DListErase(DListNode* pos)//删除
{
    DListNode* prev = pos->_prev;
    prev->_next = pos->_next;
    pos->_next->_prev = prev;
    free(pos);
}
void Print(DListNode*head)//打印
{
    DListNode*cur = head->_next;
    printf("head");
    while (cur!= head)
    {
        printf("->%d", cur->_data);
        cur = cur->_next;
    }
    printf("\n");
}

void TestDList()
{

    DListNode* list = DListInit();
    DListPushback(list, 2);
    DListPushback(list, 3);
    DListPushback(list, 4);
    DListPushback(list, 6);
    Print(list);
    DListPopback(list);
    Print(list);
    DListPopFront(list);
    Print(list);
    DListPushFront(list,2);
    Print(list);
    DListFind(list,2);
}
void TestDList1()
{
    DListNode* list = DListInit();
    DListInsert(list, 1);
    DListInsert(list, 2);
    DListInsert(list, 3);
    DListInsert(list, 4);
    DListInsert(list, 5);

    Print(list);
    DListErase(list->_prev);
    DListErase(list->_next);
    DListErase(list->_prev);
    Print(list);

}

text.c

“`

include”SeqList.h”

int main()
{
TestDList();
TestDList1();
system(“pause”);
return 0;
}
“`双向链表的增删查改_第2张图片
双向链表的增删查改_第3张图片

你可能感兴趣的:(双向链表的增删查改)