带头+双向+循环 链表 示意图:
文件代码: (注:因为使用了哨兵位,保证了链表一开始必有一个哨兵位节点所以不用单独考虑链表一开始没有节点的情况了,也就是不用传链表的二级指针了--&list;)
//List.h #pragma once #include#include #include // 带头+双向+循环链表增删查改实现 typedef int LTDataType; typedef struct ListNode { LTDataType data; struct ListNode* next; struct ListNode* prev; }ListNode; // 创建新节点 ListNode* Buynewnode(LTDataType x); //双向链表初始化--即创建哨兵位作为头节点 ListNode* ListInit(); // 双向链表销毁 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
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
ListNode* Buynewnode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
ListNode* ListInit()
{
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
assert(head);
head->next = head;
head->prev = head;
return head;
}
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur != pHead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("n");
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
//1.法一
//2.法二,复用插入
ListInsert(pHead->next, x);
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
//1.法一
//2.法二,复用插入
ListInsert(pHead, x);
}
void ListPopBack(ListNode* pHead)
{
ListNode* tail = pHead->prev;
ListNode* prev = tail->prev;
free(tail);
pHead->prev = prev;
prev->next = pHead;
}
void ListPopFront(ListNode* pHead)
{
ListNode* next = pHead->next->next;
free(pHead->next);
pHead->next = next;
next->prev = pHead;
}
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
ListNode* cur = pHead->next;
while (cur != pHead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)
{
ListNode* newnode = Buynewnode(x);
ListNode* prev = pos->prev;
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
void ListErase(ListNode* pos)
{
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "List.h"
void test1()
{
ListNode* list=ListInit();
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPrint(list);
ListPushFront(list, 4);
ListPushFront(list, 5);
ListPushFront(list, 6);
ListPrint(list);
ListPopBack(list);
ListPopBack(list);
ListPopBack(list);
ListPrint(list);
ListPopFront(list);
ListPrint(list);
ListPopFront(list);
ListPrint(list);
ListNode* pos=ListFind(list, 2);
if (pos == NULL)
{
printf("找不到");
}
pos = ListFind(list, 4);
printf("n");
if (pos != NULL)
{
printf("找到了%dn", pos->data);
ListPrint(list);
ListPushFront(list, 5);
ListPrint(list);
ListInsert(pos, 9);
ListPrint(list);
ListErase(pos);
ListPrint(list);
}
}
int main()
{
test1();
return 0;
}
测试代码:
运行截图:



