带头双向循环链表概念及结构
概念:带头双向循环链表结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了。
带头双向循环链表的实现
List.h代码实现
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType val;
struct ListNode* next;
struct ListNode* prev;
}LTNode;
LTNode* BuyLTNode(LTDataType x);//创建一个新的节点
void ListPrint(LTNode* plist);//打印
LTNode* ListInit();//初始化
void ListPushBack(LTNode* plist, LTDataType x); //尾插
void ListPopBack(LTNode* plist);//尾删
void ListPushFront(LTNode* plist, LTDataType x);//头插
void ListPopFront(LTNode* plist);//头删
LTNode* ListFind(LTNode* plist, LTDataType x);//查找
void ListInsert(LTNode* pos, LTDataType x);//在pos的前面进行插入
void ListErese(LTNode* pos);//删除pos位置的节点
void ListDestory(LTNode* plist);//销毁
List.c代码实现
#include "List.h"
LTNode* BuyLTNode(LTDataType x)
{
LTNode* newnode = (LTNode*) malloc(sizeof(LTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->val = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
void ListPrint(LTNode* plist)
{
assert(plist);
LTNode* cur = plist->next;
while (cur != plist)
{
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
LTNode* ListInit()
{
LTNode* plist = BuyLTNode(0);
plist->next = plist;
plist->prev = plist;
return plist;
}
void ListDestory(LTNode* plist)
{
assert(plist);
LTNode* cur = plist->next;
while (cur != plist)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(plist);
}
尾插:将尾节点的next连接到新的节点,将新节点的前驱连接到尾节点,然后将新节点的next连接到head,将head的前驱连接到新节点即可完成。
代码实现:
void ListPushBack(LTNode* plist, LTDataType x)
{
assert(plist);
LTNode* newnode = BuyLTNode(x);
LTNode* Tail = plist->prev;
Tail->next = newnode;
newnode->prev = Tail;
plist->prev = newnode;
newnode->next = plist;
}
尾删:将尾节点的前驱当做新的尾,与head建立连接,将旧尾free掉。
代码实现:
void ListPopBack(LTNode* plist)
{
assert(plist);
assert(plist->next != plist);
LTNode* Tail = plist->prev;
LTNode* TailPrev = Tail->prev;
free(Tail);
Tail = NULL;
TailPrev->next = plist;
plist->prev = TailPrev;
}
头插:
代码实现:
void ListPushFront(LTNode* plist, LTDataType x)
{
assert(plist);
LTNode* newnode = BuyLTNode(x);
plist->next->prev = newnode;
newnode->next = plist->next;
plist->next = newnode;
newnode->prev = plist;
}
头删:
代码实现:
void ListPopFront(LTNode* plist)
{
assert(plist);
assert(plist->next != plist);
LTNode* HeadNext = plist->next;
HeadNext->next->prev = plist;
plist->next = HeadNext->next;
free(HeadNext);
HeadNext = NULL;
}
查找代码实现:
LTNode* ListFind(LTNode* plist, LTDataType x)
{
assert(plist);
LTNode* cur = plist->next;
while (cur != plist)
{
if (cur->val == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
pos位置插入代码实现:
void ListInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = BuyLTNode(x);
LTNode* posPrev = pos->prev;
newnode->next = pos;
pos->prev = newnode;
posPrev->next = newnode;
newnode->prev = posPrev;
}
pos位置删除代码实现:
void ListErese(LTNode* pos)
{
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
pos = NULL;
}
销毁代码实现:
void ListDestory(LTNode* plist)
{
assert(plist);
LTNode* cur = plist->next;
while (cur != plist)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(plist);
}