List.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int ListDataType;
typedef struct ListNode
{
struct ListNode *next;
struct ListNode *prev;
ListDataType data;
}ListNode;
ListNode* ListInit();
ListNode* BuyListNode(ListDataType x);
void ListPrint(ListNode *phead);
void ListDestory(ListNode *phead);
void ListPushFront1(ListNode *phead, ListDataType x);
void ListPushFront2(ListNode *phead, ListDataType x);
void ListPushBack(ListNode *phead, ListDataType x);
void ListPopFront(ListNode *phead);
void ListPopBack1(ListNode *phead);
void ListPopBack2(ListNode *phead);
void ListInsert(ListNode *pos, ListDataType x);
void ListErase(ListNode *pos);
ListNode* Find(ListNode *phead, ListDataType x);
List.c
#include "List.h"
ListNode* ListInit()
{
ListNode *phead = (ListNode *)malloc(sizeof(ListNode));
if (phead == NULL)
{
printf("malloc fail\n");
exit(-1);
}
phead->next = phead;
phead->prev = phead;
return phead;
}
ListNode* BuyListNode(ListDataType x)
{
ListNode *newnode = (ListNode *)malloc(sizeof(ListNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
}
void ListPrint(ListNode *phead)
{
assert(phead);
ListNode *tmp = phead->next;
while (tmp != phead)
{
printf("%d ", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
void ListDestory(ListNode *phead)
{
ListNode *tmp = phead->next;
while (tmp != phead)
{
ListNode *tmpNext = tmp->next;
ListErase(tmp);
tmp = tmpNext;
}
free(phead);
phead = NULL;
}
void ListPushFront1(ListNode *phead, ListDataType x)
{
assert(phead);
ListNode *newnode = BuyListNode(x);
ListNode *pheadNext = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = pheadNext;
pheadNext->prev = newnode;
}
void ListPushFront2(ListNode *phead, ListDataType x)
{
ListInsert(phead->next, x);
}
void ListPushBack(ListNode *phead, ListDataType x)
{
assert(phead);
ListNode *newnode = BuyListNode(x);
ListNode *tmp = phead->prev;
tmp->next = newnode;
newnode->prev = tmp;
phead->prev = newnode;
newnode->next = phead;
}
void ListPopFront(ListNode *phead)
{
assert(phead);
assert(phead->next != phead);
ListNode *tmp = phead->next;
ListNode *tmpNext = tmp->next;
phead->next = tmpNext;
tmpNext->prev = phead;
free(tmp);
}
void ListPopBack1(ListNode *phead)
{
assert(phead);
assert(phead->next != phead);
ListNode *tmp = phead->prev;
ListNode *tmpPrev = tmp->prev;
phead->prev = tmpPrev;
tmpPrev->next = phead;
free(tmp);
}
void ListPopBack2(ListNode *phead)
{
ListErase(phead->prev);
}
void ListInsert(ListNode *pos, ListDataType x)
{
assert(pos);
ListNode *newnode = BuyListNode(x);
ListNode *posPrev = pos->prev;
posPrev->next = newnode;
newnode->prev = posPrev;
newnode->next = pos;
pos->prev = newnode;
}
void ListErase(ListNode *pos)
{
assert(pos);
ListNode *posNext = pos->next;
ListNode *posPrev = pos->prev;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
}
ListNode* Find(ListNode *phead, ListDataType x)
{
assert(phead);
ListNode *tmp = phead->next;
while (tmp != phead)
{
if (tmp->data == x)
{
return tmp;
}
tmp = tmp->next;
}
}
Test.c
#include "List.h"
int main()
{
ListNode *L = ListInit();
ListPushFront1(L, 1);
ListPushFront1(L, 2);
ListPushFront1(L, 3);
printf("PushFront1:");
ListPrint(L);
ListPushBack(L, 4);
ListPushBack(L, 5);
ListPushBack(L, 6);
printf("PushBack:");
ListPrint(L);
ListPopFront(L);
ListPopFront(L);
printf("PopFront:");
ListPrint(L);
ListPopBack1(L);
ListPopBack1(L);
printf("PopBack1:");
ListPrint(L);
ListPushFront2(L, 10);
printf("PushFront2:");
ListPrint(L);
ListPopBack2(L);
printf("PopBack2:");
ListPrint(L);
ListNode *tmp=Find(L, 10);
tmp->data = 20;
printf("After change:");
ListPrint(L);
ListDestory(L);
printf("After destory:");
printf("\n");
ListPrint(L);
return 0;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/806c3b1faa9d2307ff43d0c38c6390ce.png)