目录
前言:
链表的概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
链表的类型:
1. 单向 / 双向 2. 带头 / 不带头 3. 循环 / 非循环
组合起来共有八种不同的链表结构,这篇文章介绍的是其中最复杂的带头循环双向链表。
带头循环双向链表:带头循环双向链表是最复杂的链表,一般用于单独存储数据,实际中使用的数据结构链表,一般都是带头循环双向链表。另外这个结构虽然复杂,但是使用代码实现以后会发现结构会带 来很多优势,实现反而简单了.
一、函数声明
typedef int LTDataType; //方便后续更改数据类型
typedef struct ListNode //链表的声明定义
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
ListNode* BuyListNode(LTDataType x); //创造一个结点并返回其地址
ListNode* ListCreate(); //链表初始化
void ListDestory(ListNode* pHead); // 链表销毁
void ListPrint(ListNode* pHead); // 双向链表打印
void ListPushBack(ListNode* pHead, LTDataType x); // 双向链表尾插
void ListPopBack(ListNode* pHead); // 双向链表尾删
void ListPushFront(ListNode* pHead, LTDataType x); // 双向链表头插
void ListPopFront(ListNode* pHead); // 双向链表头删
ListNode* ListFind(ListNode* pHead, LTDataType x); // 双向链表查找
void ListInsert(ListNode* pos, LTDataType x); // 双向链表在pos的前面进行插入
void ListErase(ListNode* pos); // 双向链表删除pos位置的节点
二、链表的定义与初始化
ListNode* BuyListNode(LTDataType x) //创造一个结点并返回其地址
{
ListNode* newcode = (ListNode*)malloc(sizeof(ListNode));
if (!newcode) //取消空指针的引用
{
perror("BuyListNode");
exit(-1);
}
newcode->_data = x;
newcode->_next = NULL;
newcode->_prev = NULL;
return newcode;
}
ListNode* ListCreate() //链表的初始化
{
assert("ListNode");
ListNode* newcode = BuyListNode(0);
newcode->_data = 0;
newcode->_next = newcode;
newcode->_prev = newcode;
return newcode;
}
三、链表的销毁
void ListDestory(ListNode* pHead) //链表的销毁
{
ListNode* cur = pHead->_next;
while (cur != pHead)
{
ListNode* next = cur->_next;
free(cur);
cur = next;
}
free(pHead);
pHead = NULL;
}
四、链表的打印
void ListPrint(ListNode* pHead) //链表的打印
{
assert(pHead);
ListNode* cur = pHead->_next;
while (cur != pHead) //到头结点停止循环
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("NULL\n");
}
五、链表尾插尾删
void ListPushBack(ListNode* pHead, LTDataType x) //链表尾插
{
assert(pHead);
ListNode* cur = pHead->_prev;
ListNode* newcode = BuyListNode(x);
cur->_next = newcode;
newcode->_prev = cur;
newcode->_next = pHead;
pHead->_prev = newcode;
}
void ListPopBack(ListNode* pHead) //链表尾删
{
assert(pHead);
assert(pHead->_next != pHead);
ListNode* tail = pHead->_prev;
ListNode* prev = tail->_prev;
prev->_next = pHead;
pHead->_prev = prev;
free(tail);
}
六、链表头插头删
void ListPushFront(ListNode* pHead, LTDataType x) //链表头插
{
assert(pHead);
ListNode* newcode = BuyListNode(x);
newcode->_prev = pHead;
newcode->_next = pHead->_next;
pHead->_next = newcode;
}
void ListPopFront(ListNode* pHead) //链表头删
{
assert(pHead);
if (pHead->_next == pHead)
{
ListDestory(pHead);
}
ListNode* cur = pHead->_next;
pHead->_next = cur->_next;
cur->_next->_prev = pHead;
free(cur);
}
七、链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x) //链表的查找
{
assert(pHead);
ListNode* cur = pHead->_next;
while (cur != pHead)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL; //查找失败返回空指针
}
八、链表的指定位置插入和删除
void ListInsert(ListNode* pos, LTDataType x) //在指定位置插入
{
assert(pos);
ListNode* cur = pos->_prev;
ListNode* newcode = BuyListNode(x);
newcode->_prev = cur;
newcode->_next = pos;
cur->_next = newcode;
pos->_prev = newcode;
}
void ListErase(ListNode* pos) //在指定位置删除
{
assert(pos);
ListNode* prev = pos->_prev;
prev->_next = pos->_next;
pos->_next->_prev = prev;
free(pos);
}