目的
实现双向循环带头节点的链表的增删改查
创建返回链表头节点
// 创建返回链表的头结点.
ListNode* ListCreate()
{
ListNode *head = (ListNode*)malloc(sizeof(ListNode));
head->next = head;
head->prev = head;
return head;
}
双向链表的销毁
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
assert(pHead != NULL);
ListNode *p = (pHead)->next;
while (p != pHead){
p->prev->next = p->next;
p->next->prev = p->prev;
free(p);
p = pHead->next;//更新p指针的指向
}
free(pHead);
pHead = NULL;//避免野指针
}
双向链表打印
// 双向链表打印
void ListPrint(ListNode* pHead)
{
assert(pHead != NULL);
ListNode *p = pHead->next;
while (p != pHead){
//当不止存在一个头节点的时候
printf("%d", p->data);
p = p->next;
}
printf("\n");
}
双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead != NULL);
ListNode* s = (ListNode*)malloc(sizeof(ListNode));
assert(s != NULL);
s->data = x;
s->prev = pHead->prev;
s->next = pHead;
pHead->prev->next = s;
pHead->prev = s;
}
双向链表尾删
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
assert(pHead != NULL);
ListNode *head = pHead;
if (head->next == pHead)