文章目录
一,带哨兵位的双向循环链表的结构
带哨兵位的双向循环链表与此前讲过的单链表相比,主要有两大不同:一是,增加了哨兵位,减少了我们在写代码过程中的代码量,此前没有哨兵位是,当我们插入结点时需要判断头结点是否为空,再增加头结点后就不需要进行判断了;二是,增加了向后指的指针,这大大方便了我们对链表的操作。
结构体代码:
typedef int Datatype;
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
Datatype data;
}ListNode;
二,接口实现
2.1 链表的初始化
由于增加了哨兵位,因此我们需要对哨兵位结点初始化。
代码:
ListNode* InitList()
{
ListNode* phead = BuyListNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
2.2 获得一个新结点
代码:
ListNode* BuyListNode(Datatype x)
{
struct ListNode* newnode = malloc(sizeof(ListNode));
assert(newnode);
newnode->next = NULL;
newnode->prev = NULL;
newnode->data = x;
return newnode;
}
2.3 打印链表
代码:
void Print(ListNode* phead)
{
assert(phead);
printf("<=head=>");
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d<=>", cur->data);
cur = cur->next;
}
}
2.4 尾插及其前插
尾插代码:
void LTPushBack(ListNode* phead,Datatype x)
{
assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
前插代码:
void LTPushFront(ListNode* phead, Datatype x)
{
assert(phead);
ListNode* newnode=BuyListNode(x);
ListNode* pnext = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = pnext;
pnext->prev = newnode;
}
3.5 尾删及其头删
尾删代码:
void LTPopBack(ListNode* phead)
{
assert(phead);
assert(phead->next);
ListNode* tail = phead->prev;
phead->prev = tail->prev;
tail->prev->next = phead;
free(tail);
}
头删代码:
void LTPopFront(ListNode* phead)
{
assert(phead);
assert(phead->next);
ListNode* first = phead->next;
ListNode* next = first->next;
phead->next = next;
next->prev = phead;
free(first);
}
2.6 插入及其删除
Find( )函数
代码:
ListNode* LTFind(ListNode* phead, Datatype pos)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == pos)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
插入
ret= LTFind(plist, 2);
Erase(plist,ret );
代码中pos 为ret 的形参。
代码:
void LTInsert(ListNode* phead,ListNode* pos, Datatype x)
{
assert(phead);
assert(pos);
ListNode* newnode=BuyListNode(x);
ListNode* back = pos->next;
pos->next = newnode;
newnode->prev = pos;
back->prev = newnode;
newnode->next = back;
}
删除
代码:
void Erase(ListNode* phead, ListNode* pos)
{
assert(phead);
assert(phead->next);
assert(pos);
ListNode* nextnode = pos->next;
nextnode->prev = pos->prev;
pos->prev->next = nextnode;
free(pos);
pos = NULL;
}
2.6 销毁链表
代码:
void Destory(ListNode* phead)
{
assert(phead);
if (phead->next == phead)
{
free(phead);
phead = NULL;
return;
}
else
{
ListNode* cur = phead->next;
ListNode* nextnode = cur->next;
while (cur!=phead)
{
free(cur);
cur = nextnode;
if(nextnode)
nextnode = nextnode->next;
}
free(phead);
phead = NULL;
}
}
头插,尾插,头删,尾删优化
代码:
//头插
void LTPushFront(ListNode* phead, Datatype x)
{
assert(phead);
LTInsert(phead, phead, x);
}
//尾插
void LTPushBack(ListNode* phead,Datatype x)
{
assert(phead);
LTInsert(phead, phead->prev, x);
}
//头删
void LTPopFront(ListNode* phead)
{
assert(phead);
Erase(phead,phead->next);
}
//尾删
void LTPopBack(ListNode* phead)
{
assert(phead);
Erase(phead,phead->prev);
}