数据结构:带头结点的双向循环链表

头文件及函数声明:

#include
#include
#include

typedef int DataType;

typedef struct DListNode
{
    struct DListNode* _next;
    struct DListNode* _prev;
    DataType _data;
}DListNode;


DListNode* BuyDListNode(DataType x);//创建新的节点
DListNode* DListInit();//初始化
void DListDestory(DListNode* head);//销毁链表
void DListPrint(DListNode* head);//打印链表

void DListPushBack(DListNode* head, DataType x);//尾插
void DListPushFront(DListNode* head, DataType x);//头插
void DListPopBack(DListNode* head);//尾删
void DListPopFront(DListNode* head);//头删

DListNode* DListFind(DListNode* head, DataType x);//寻找指定数,返回地址
void DListInsert(DListNode* pos, DataType x);//指定位置之前插入
void DListErase(DListNode* pos);//指定位置删除

函数实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include"DList.h"
DListNode* BuyDListNode(DataType x)
{
    DListNode* newNode = (DListNode*)malloc(sizeof(DListNode));
    if (newNode == NULL)
    {
        printf("创建失败\n");
    }
    newNode->_data = x;
    newNode->_next = NULL;
    newNode->_prev = NULL;
    return newNode;
}
DListNode* DListInit()
{
    DListNode* Head = (DListNode*)malloc(sizeof(DListNode));
    if (Head== NULL)
    {
        printf("创建失败\n");
        return NULL;
    }
    Head->_next = Head;
    Head->_prev = Head;
    return Head;
}
void DListDestory(DListNode* head)
{
    assert(head);
    DListNode* next = head;
    DListNode* cur = head;
    if (head == NULL)
    {
        return;
    }
    while (next)
    {
        cur = next;
        next = next->_next;
        free(cur);
        cur = NULL;
    }
    head = NULL;
}
void DListPrint(DListNode* head)
{

    DListNode*next = head->_next;
    if (head == NULL)
    {
        printf("链表为空\n");
        return;
    }
    printf("Head->");
    while (next != head)
    {

        printf("%d->", next->_data);
        next = next->_next;
    }
    printf("Head\n");
}
void DListPushBack(DListNode* head, DataType x)
{
    DListNode*newNode = BuyDListNode(x);
    DListNode*prev = head->_prev;
    newNode->_next = head;
    newNode->_prev = prev;
    prev->_next = newNode;
    head->_prev = newNode;
}
void DListPushFront(DListNode* head, DataType x)
{
    DListNode*newNode = BuyDListNode(x);
    DListNode*next = head->_next;
    newNode->_next = next;
    next->_prev = newNode;
    newNode->_prev = head;
    head->_next = newNode;
}
void DListPopBack(DListNode* head)
{
    assert(head);
    DListNode*prev1 = head->_prev;
    DListNode*prev2 = prev1->_prev;
    prev2->_next = head;
    head->_prev = prev2;
    free(prev1);

}
void DListPopFront(DListNode* head)
{
    assert(head);
    DListNode*next1 = head->_next;
    DListNode*next2 = next1->_next;
    head->_next = next2;
    next2->_prev = head;
    free(next1);
}
DListNode* DListFind(DListNode* head, DataType x)
{
    assert(head);
    DListNode*cur = NULL;
    DListNode*next = head;
    while (next != cur)
    {
        if (next->_data == x)
        {
            return next;
        }
        next = next->_next;
        cur = head;
    }
    printf("寻找元素不存在\n");
    return NULL;
}
void DListInsert(DListNode* pos, DataType x)
{
    assert(pos);
    DListNode*newNode = BuyDListNode(x);
    DListNode*prev = pos->_prev;
    newNode->_next = pos;
    pos->_prev = newNode;
    newNode->_prev = prev;
    prev->_next = newNode;
}
void DListErase(DListNode* pos)
{
    assert(pos);
    DListNode*prev = pos->_prev;
    DListNode*next = pos->_next;
    prev->_next = next;
    next->_prev = prev;
    free(pos);
    pos = NULL;
}

你可能感兴趣的:(数据结构:带头结点的双向循环链表)