带头双向循环链表~C语言(附代码)~简单实现
带头双向循环链表是链表中用起来最方便的一种了.
对这种链表的实现较为简单,我们来看看具体实现的功能有哪些吧
目录
- // 创建返回链表的头结点.
- 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);
创建返回链表的头结点
- // 创建返回链表的头结点.
- ListNode* ListCreate()
- {
- ListNode* Head = (ListNode*)malloc(sizeof(ListNode));
- Head->next = Head;
- Head->prev = Head;
- return Head;
- }
双向链表销毁
- // 双向链表销毁
- void ListDestory(ListNode* pHead)
- {
- assert(pHead);
- ListNode* cur = pHead->next;
- while (cur!=pHead)
- {
- ListNode* next = cur->next;
- free(cur);
- cur = next;
- }
- //最后释放头节点
- free(pHead);
- }
双向链表打印
- // 双向链表打印
- void ListPrint(ListNode* pHead)
- {
- assert(pHead);
- ListNode* cur = pHead->next;
- while (cur != pHead)
- {
- printf("%%d ", cur->data);
- cur = cur->next;
- }
- printf("
");
- }
双向链表尾插
- // 双向链表尾插
- void ListPushBack(ListNode* pHead, LTDataType x)
- {
- assert(pHead);
- //法一
- //ListNode*prev = pHead->prev;
- //ListNode*NewNode = (ListNode*)malloc(sizeof(ListNode));
- //NewNode->data = x;
- //prev->next = NewNode;
- //NewNode->next = pHead;
- //法二
- ListInsert(pHead->prev, x);
- }
双向链表尾删
- // 双向链表尾删
- void ListPopBack(ListNode* pHead)
- {
- assert(pHead);
- //assert(pHead-> next!= pHead);
- //ListNode*tail=pHead->prev;
- //ListNode* prev = tail->prev;
- //prev->next = pHead;
- //pHead->next = prev;
- //free(tail);
- ListErase(pHead->prev);
- }
双向链表头插
- // 双向链表头插
- void ListPushFront(ListNode* pHead, LTDataType x)
- {
- //assert(pHead);
- //ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
- //tmp->data = x;
- // ListNode*head = pHead->next;
- // tmp->next = head;
- // tmp->prev = pHead;
- // pHead->next = tmp;
- // head->prev = tmp;
- ListInsert( pHead->next, x);
- }
双向链表头删
- // 双向链表头删
- void ListPopFront(ListNode* pHead)
- {
- assert(pHead);
- /*assert(pHead->next != pHead);
- ListNode* head = pHead->next;
- pHead->next = head->next;
- head->next->prev = pHead;
- free(head);*/
- ListErase(pHead->next);
- }
双向链表查找
- // 双向链表查找
- ListNode* ListFind(ListNode* pHead, LTDataType x)
- {
- assert(pHead);
- ListNode* cur = pHead;
- while (cur->next != cur)
- {
- if(cur->data==x)
- {
- return cur;
- }
- cur = cur->next;
- }
- return NULL;
- }
双向链表在pos的前面进行插入
- // 双向链表在pos的前面进行插入
- void ListInsert(ListNode* pos, LTDataType x)
- {
- assert(pos);
- ListNode* prev = pos->prev;
- ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
- tmp->data = x;
- prev->next = tmp;
- tmp->prev = prev;
- pos->prev = tmp;
- tmp->next = pos;
- }
双向链表删除pos位置的节点
- // 双向链表删除pos位置的节点
- void ListErase(ListNode* pos)
- {
- assert(pos);
- ListNode* prev = pos->prev;
- ListNode* next = pos->next;
- prev->next = next;
- next->prev = prev;
- free(pos);
- }