#pragma once
#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);
#include"list.h"
void test1()
{
ListNode* plist = ListCreate();
ListPushFront(plist, 1);
ListPushFront(plist, 2);
ListPushFront(plist, 3);
ListPushFront(plist, 4);
ListPushBack(plist, 5);
ListPrint(plist);
ListPopBack(plist);
ListPopFront(plist);
ListPrint(plist);
ListNode* pos = ListFind(plist, 2);
//ListInsert(pos, 7);
ListErase(pos);
ListPrint(plist);
ListDestory(plist);
}
int main()
{
test1();
return 0;
}
#include"list.h"
ListNode* Buynode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc fail");
return NULL;
}
newnode->_data = x;
newnode->_next = NULL;
newnode->_prev = NULL;
return newnode;
}
bool LTEmpty(ListNode* phead)
{
assert(phead);
return phead->_next == phead;
}
// 创建返回链表的头结点.
ListNode* ListCreate()
{
ListNode* phead = Buynode(-1);
phead->_next = phead;
phead->_prev = phead;
return phead;
}
// 双向链表打印
void ListPrint(ListNode* 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 = Buynode(x);
//ListNode* tail = pHead->_prev;
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = pHead;
//pHead->_prev = newnode;
ListInsert(pHead, x);
}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
assert(pHead);
assert(!LTEmpty(pHead));
/*ListNode* tail = pHead->_prev;
ListNode* first = tail->_prev;
free(tail);
first->_next = pHead;
pHead->_prev = first;*/
ListErase(pHead->_prev);
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = Buynode(x);
ListNode* first = pHead->_next;//先进行备份
newnode->_next = first;
first->_prev = newnode;
pHead->_next = newnode;
newnode->_prev = pHead;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
assert(pHead);
assert(!LTEmpty(pHead));
ListNode* del = pHead->_next;
ListNode* first = del->_next;
pHead->_next = first;
first->_prev = pHead;
free(del);
}
// 双向链表查找
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;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* newnode = Buynode(x);
ListNode* first = pos->_prev;
newnode->_next = pos;
pos->_prev = newnode;
newnode->_prev = first;
first->_next = newnode;
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* last = pos->_prev;
ListNode* end = pos->_next;
last->_next = end;
end->_prev = last;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->_next;
while (cur != pHead)
{
ListNode* next = cur->_next;
free(cur);
cur = next;
}
free(pHead);
}