目录标题
带头双向循环链表的增删查改
在实际链表的结构中,带头双循环链表结构最复杂,一般用在单独存储数据。
实际中使用的链表数据结构,都是带头双向循环链表。
另外这个结构虽然复杂,但是使用代码实现以后会带来很多优势,实现反而简单了。
链表的实现接口
List.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 带头+双向+循环链表增删查改实现
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);
List.c
#include"List.h"
ListNode* BuyListNode(LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}
ListNode* ListCreate()
{
ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListDestory(ListNode* pHead)
{
ListNode* cur = pHead->next;
while (cur != pHead)
{
ListNode* next = cur->next;
ListErase(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("\n");
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = BuyListNode(x);
ListNode* cur = pHead->prev;
cur->next = newnode;
newnode->prev = cur;
pHead->prev = newnode;
newnode->next = pHead;
}
void ListPopBack(ListNode* pHead)
{
assert(pHead);
assert(pHead->next!=pHead);
ListNode* tail = pHead->prev;
ListNode* tailprev = tail->prev;
tailprev->next = pHead;
pHead->prev = tailprev;
free(tail);
tail = NULL;
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* head = pHead->next;
ListNode* newnode = BuyListNode(x);
pHead->next = newnode;
newnode->prev = pHead;
newnode->next = head;
head->prev = newnode;
}
void ListPopFront(ListNode* pHead)
{
assert(pHead);
assert(pHead->next != pHead);
ListNode* head = pHead->next;
ListNode* headnext = head->next;
pHead->next = headnext;
headnext->prev = pHead;
free(head);
head = NULL;
}
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* posprev = pos->prev;
ListNode* newnode = BuyListNode(x);
posprev->next = newnode;
newnode->prev = posprev;
pos->prev = newnode;
newnode->next = pos;
}
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* posPrev = pos->prev;
ListNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
pos = NULL;
}
Test.c
#include "List.h"
void ListTest1()
{
ListNode*phead=ListCreate(); // 空链表
ListPushBack(phead, 1);
ListPushBack(phead, 2);
ListPushBack(phead, 3);
ListPushBack(phead, 4);
ListPushBack(phead, 5);
ListPrint(phead);
ListPushFront(phead, 0);
ListPushFront(phead, -1);
ListPrint(phead);
ListPopBack(phead);
ListPrint(phead);
ListPopFront(phead);
ListPopFront(phead);
ListPrint(phead);
ListFind(phead, 2);
ListDestory(phead);
ListPrint(phead);
}
int main()
{
ListTest1();
return 0;
}
顺序表与链表的对比
顺序表(数组)
缺点:1、 头部或者中间插入删除数据,需要挪动数据,时间复杂度时O(N);
2、空间不够了,需要增容,增容有一些消耗。其次增容一般是2倍,那么就可能存在一定的空间浪费;
优点:1、相对而言空间利用率更高,不需要额外的空间,占用空间小(链表需要存储指针),物理空间是连续的不容易产生内存碎片;
2、a、物理空间连续,支持随机访问,可以使用下标访问;b、相比链表缓存命中率高。
双向带头循环链表
优点:支持任意位置O(1)插入删除,插入删除效率高,不需要增容,不存在空间浪费·
缺点:不支持随机访问,缓存命中率低。