目录
1.带头双向循环链表的介绍
结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了。
带头:链表有一个头结点,这个头结点也可以称为哨兵位的头结点,它的data为随机值。
双向:表示后一个结点可以找到自己的前驱结点。
循环:头结点的前驱结点是尾结点,尾结点的后继结点是头结点,构成一个环状结构。
2.带头双向循环链表的接口实现
// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
// 创建返回链表的头结点.
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);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
2.1 创建返回链表的头结点
ListNode* ListCreate()
{
ListNode* guard = (ListNode*)malloc(sizeof(ListNode));
if (guard == NULL)
{
perror("malloc fail");
exit(-1);
}
guard->next = guard;
guard->prev = guard;
return guard;
}
2.2 双向链表销毁
由于传的一级指针,在调用完函数后要将哨兵位的头结点置空。
void ListDestory(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(pHead);
}
2.3 双向链表打印
void ListPrint(ListNode* pHead)
{
assert(pHead);
printf("pHead<=>");
ListNode* cur = pHead->next;
while (cur != pHead)
{
printf("%d<=>", cur->data);
cur = cur->next;
}
printf("\n");
}
2.4 双向链表尾插
由于我们的链表是动态的,每次插入需要malloc开辟新结点,因此我们单独写一个创建结点的接口。
ListNode* BuyListNode(LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (node == NULL)
{
perror("malloc fail");
exit(-1);
}
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = BuyListNode(x);
ListNode* tail = pHead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = pHead;
pHead->prev = newnode;
//ListInsert(pHead, x);
}
2.5 双向链表尾删
在删除结点的时候,我们需要判断链表是否为空,如果只有哨兵位的头结点,不能进行删除。
bool ListEmpty(ListNode* pHead)
{
assert(pHead);
if (pHead->next == pHead)
return true;
else
return false;
}
void ListPopBack(ListNode* pHead)
{
assert(pHead);
assert(!ListEmpty(pHead));
ListNode* tail = pHead->prev;
pHead->prev = tail->prev;
tail->prev->next = pHead;
free(tail);
tail = NULL;
//ListErase(pHead->prev);
}
2.6 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* first = BuyListNode(x);
ListNode* second = pHead->next;
pHead->next = first;
first->prev = pHead;
first->next = second;
second->prev = first;
//ListInsert(pHead->next, x);
}
2.7 双向链表头删
void ListPopFront(ListNode* pHead)
{
assert(pHead);
assert(!ListEmpty(pHead));
ListNode* head = pHead->next;
pHead->next = head->next;
head->next->prev = pHead;
free(head);
head = NULL;
//ListErase(pHead->next);
}
2.8 双向链表查找
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;
}
2.9 双向链表在pos的前面进行插入
尾插(哨兵位的头结点的前驱结点就是尾)和头插可以直接调用ListInsert函数实现。
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = BuyListNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
2.10 双向链表删除pos位置的节点
尾删和头删一样可以直接调用。
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
pos = NULL;
}