List.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
ListNode* BuyListNode(LTDataType x);
ListNode* ListInit();
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);
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(ListNode* pos);
void ListDestory(ListNode* pHead);
List.c
#include"List.h"
ListNode* BuyListNode(LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = x;
return node;
}
ListNode* ListInit()
{
ListNode* plist = BuyListNode(-1);
plist->next = plist;
plist->prev = plist;
return plist;
}
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)
{
ListNode* newnode = BuyListNode(x);
pHead->prev->next = newnode;
newnode->prev = pHead->prev;
newnode->next = pHead;
pHead->prev = newnode;
}
void ListPopBack(ListNode* pHead)
{
ListNode* del = pHead->prev;
pHead->prev = del->prev;
del->prev->next = pHead;
free(del);
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
ListNode* newnode = BuyListNode(x);
pHead->next->prev = newnode;
newnode->next = pHead->next;
newnode->prev = pHead;
pHead->next = newnode;
}
void ListPopFront(ListNode* pHead)
{
ListNode* del = pHead->next;
pHead->next = del->next;
del->next->prev = pHead;
free(del);
}
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 = BuyListNode(x);
pos->prev->next = newnode;
newnode->prev = pos->prev;
newnode->next = pos;
pos->prev = newnode;
}
void ListErase(ListNode* pos)
{
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
free(pos);
}
void ListDestory(ListNode* pHead)
{
ListNode* cur = pHead->next;
while (cur != pHead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(pHead);
}