数据机构 : 双向带环循环链表实现

概述

这个链表区分于单链表 , 结构比较复杂 , 但是设计的角度精妙绝伦 让我们无需考虑二级指针和空指针访问的问题

我由三个文件实现 , 分别是

  • 头文件和声明 List.h
  • 实现功能的源码List.c
  • 测试链表功能的测试文件 Test.c

List.h

#pragma once
#include 
#include 
#include 
#include 

//带头双向循环链表
typedef int LTDataType;
typedef struct ListNode
{
    struct ListNode* next;
    struct ListNode* prev;  
    LTDataType data;
}LTNode;

LTNode* LTInit();
bool LTEmpty(LTNode* phead);

void LTPrint(LTNode* phead);
void LTPushBack(LTNode* phead, LTDataType x);   // LTInsert(phead ,LTDataType x)
void LTPushFront(LTNode* phead, LTDataType x);  // LTInsert(phead->next ,LTDataType x)

void LTPopBack(LTNode* phead);                  // LTErase(phead->next)
void LTPopFront(LTNode* phead);                 // LTerase(phead->prev)

LTNode* LTFind(LTNode* phead, LTDataType x);

void LTInsert(LTNode* pos, LTDataType x);      //在pos之前插入  这个是实现快速链表的关键  复用头插尾插
void LTErase(LTNode* pos);                     //删除pos处的值
void LTDestroy(LTNode* phead);

注意头插尾插 / 头删尾删的 功能是可以被LTInsert/LTErase 完美复用的

List.c

#include "List.h"

LTNode* BuyLTNode(LTDataType x)  
{
    LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));  //注意开12字节还是4字节
    if(newnode == NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    newnode->data = x;
    newnode->next = NULL;
    newnode->prev = NULL;
    return newnode;
}

LTNode* LTInit()
{
    LTNode* phead = BuyLTNode(-1);  
    phead->next = phead;
    phead->prev = phead;
    
    return phead;
}


void LTPrint(LTNode* phead)
{
    assert(phead);
    printf("哨兵位<==>");
    LTNode* cur = phead->next;
    while (cur != phead)
    {
        printf("%d<==>", cur->data);
        cur = cur->next;
    }
    printf("\n");
}

bool LTEmpty(LTNode* phead)
{
    assert(phead);
    
    return phead->next == phead;
}

void LTPushBack(LTNode* phead, LTDataType x)
{
    assert(phead);

    LTNode* tail = phead->prev;
    LTNode* newnode = BuyLTNode(x);

    tail->next = newnode;
    newnode->prev = tail;
    newnode->next = phead;
    phead->prev = newnode;                      //链表为空也可以使用 因为有哨兵位 都不需要二级指针
}

void LTPushFront(LTNode* phead, LTDataType x)  
{
    assert(phead);
    LTNode* newnode = BuyLTNode(x);              //注意不能先修改phead->next = newnode  这样子newnode->next找不到位置 先链接后边就可以
    
    newnode->next = phead->next;   
    phead->next->prev = newnode;
    phead->next = newnode;
    newnode->prev = phead;
}


void LTPopBack(LTNode* phead)
{
    assert(phead);
    assert(!LTEmpty(phead));

    LTNode* tail = phead->prev;
    LTNode* tailprev = tail->prev;
    free(tail);
    tailprev->next = phead;
    phead->prev = tailprev;
}

void LTPopFront(LTNode* phead)
{
    assert(phead);
    assert(!LTEmpty(phead));

    LTNode* second = phead->next->next;
    free(phead->next);
    phead->next = second;
    second->prev = phead;
}

LTNode* LTFind(LTNode* phead, LTDataType x)
{
    assert(phead);
    LTNode* cur = phead->next;
    while (cur != phead)
    {
        if (cur->data == x)
        {
             return cur;
        }
        cur = cur->next;
    }
    return NULL;
}

void LTInsert(LTNode* pos, LTDataType x)
{
    assert(pos);
    LTNode* prev = pos->prev;
    LTNode* newnode = BuyLTNode(x);

    prev->next = newnode;
    newnode->prev = prev;
    newnode->next = pos;
    pos->prev = newnode;
}

void LTErase(LTNode* pos)
{
    assert(pos);
    LTNode* PosPrev = pos->prev;
    LTNode* PosNext = pos->next;

    PosPrev->next = PosNext;
    PosNext->prev = PosPrev;
    free(pos);
}

void LTDestroy(LTNode* phead)
{
    assert(phead);

    LTNode* cur = phead->next;
    while (cur != phead)
    {
        LTNode* next = cur-next;
        free(cur);
        cur = cur->next;
    }
    free(phead);
}

Test.c

#include "List.h"

void  test1()
{
    LTNode* plist = LTInit();
    LTPushBack(plist,1);
    LTPushBack(plist,2);
    LTPushBack(plist,3);
    LTPrint(plist);

    LTNode* pos = LTFind(plist, 3);
    LTInsert(pos,77);
    LTErase(pos);
    LTPrint(plist);

    LTDestroy(plist);
    plist = NULL;
}

int main()
{
    test1();
    
    return 0;
}

你可能感兴趣的:(数据结构与算法,链表,数据结构,c语言)