#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#pragma once
#include <stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType val;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
ListNode* ListInit(); //初始化双向链表
void ListPushBack(ListNode* pplist, LTDataType x); //双向链表尾插
void ListPrint(ListNode* pplist); //双向链表打印
void ListPopBcak(ListNode* pplist); //双向链表尾删
void ListPushFront(ListNode* pplist, LTDataType x); //双向链表头插
void ListPopFront(ListNode* pplist); //双向链表头删
ListNode* ListFind(ListNode* pplist, LTDataType x); //双向链表查找
void ListInsert(ListNode* pos,LTDataType x); //双向链表在pos的前面进行插入
void ListErase(ListNode* pos); //双向链表删除pos位置的节点
void ListDestory(ListNode* pplist); //双向链表销毁
ListNode* BuyListNode(LTDataType x) //新建一个节点
{
ListNode* newnode = malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc::ListNode");
exit(-1);
}
newnode->val = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
ListNode* ListInit() //初始化双向链表
{
ListNode* phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPushBack(ListNode* pplist, LTDataType x) //双向链表尾插
{
//assert(pplist);
//ListNode* newnode = BuyListNode(x);
//ListNode* next = pplist->prev;
//pplist->prev = newnode;
//newnode->next = pplist;
//next->next = newnode;
//newnode->prev = next;
ListInsert(pplist,x);
}
void ListPrint(ListNode* pplist) //双向链表打印
{
assert(pplist);
ListNode* cur = pplist->next;
while (cur != pplist)
{
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
void ListPopBcak(ListNode* pplist) //双向链表尾删
{
//assert(pplist);
//assert(pplist->next != pplist)
//ListNode* next = pplist->prev->prev;
//free(pplist->prev);
//pplist->prev = NULL;
//pplist->prev = next;
//next->next = pplist;
ListErase(pplist->prev);
}
void ListPushFront(ListNode* pplist, LTDataType x) //双向链表头插
{
//assert(pplist);
//ListNode* next = pplist->next;
//ListNode* newnode = BuyListNode(x);
//pplist->next = newnode;
//newnode->prev = pplist;
//newnode->next = next;
//next->prev = newnode;
ListInsert(pplist->next, x);
}
void ListPopFront(ListNode* pplist) //双向链表头删
{
//assert(pplist);
//assert(pplist->next != pplist);
//ListNode* next = pplist->next->next;
//free(pplist->next);
//pplist->next = next;
//next->prev = pplist;
ListErase(pplist->next);
}
ListNode* ListFind(ListNode* pplist, LTDataType x) //双向链表查找
{
assert(pplist);
ListNode* cur = pplist->next;
while (cur != pplist)
{
if (cur->val != x)
{
cur = cur->next;
}
else
{
return cur;
}
}
return NULL;
}
void ListInsert(ListNode* pos, LTDataType x) //双向链表在pos的前面进行插入
{
ListNode* newnode = BuyListNode(x);
ListNode* cur = pos->prev;
cur->next = newnode;
newnode->prev = cur;
newnode->next = pos;
pos->prev = newnode;
}
void ListErase(ListNode* pos) //双向链表删除pos位置的节点
{
assert(pos);
ListNode* tail = pos->next;
ListNode* cur = pos->prev;
free(pos);
pos = NULL;
cur->next = tail;
tail->prev = cur;
}
void ListDestory(ListNode* pplist) //双向链表销毁
{
assert(pplist);
ListNode* cur = pplist->next;
while (cur != pplist)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(pplist);
}
void Test1()
{
ListNode* plist = ListInit();
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 6);
ListPushBack(plist, 5);
ListPushBack(plist, 3);
ListPrint(plist);
ListPopBcak(plist);
ListPopBcak(plist);
ListPopBcak(plist);
ListPopBcak(plist);
ListPopBcak(plist);
ListPrint(plist);
ListPushFront(plist, 10);
ListPushFront(plist, 11);
ListPushFront(plist, 12);
ListPushFront(plist, 13);
ListPushFront(plist, 14);
ListPrint(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPrint(plist);
ListNode* pos = ListFind(plist, 10);
ListInsert(pos, 100);
ListPrint(plist);
ListErase(pos);
ListPrint(plist);
ListDestory(plist);
ListPrint(plist);
}
int main()
{
Test1();
return 0;
}
带头双向循环链表的实现
于 2022-05-06 10:13:12 首次发布