#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDateType;
typedef struct ListNode {
LTDateType data;
struct ListNode* next;
struct ListNode* prev;
}LTNode;
LTNode* ListInit();
void ListPrint(LTNode* phead);
void ListPushBack(LTNode* phead, LTDateType x);
void ListPopBack(LTNode* phead);
void ListPushFront(LTNode* phead, LTDateType x);
void ListPopFront(LTNode* phead);
#include"List.h"
#include<assert.h>
LTNode* ListInit() {
LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPrint(LTNode* phead) {
assert(phead);
LTNode* cur = phead->next;
while (cur != phead) {//中间要断开,断开中间隔着phead->next
printf("%d\n",cur->data);
cur = cur->next;
}
printf("\n");
}
LTNode* BuyListNode(LTDateType x) {
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
newnode->data = x;
newnode->next = newnode->prev = NULL;
}
void ListPushBack(LTNode* phead, LTDateType x) {
assert(phead);
LTNode* tail = phead->prev;
//LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
//newnode->data = x;
LTNode* newnode = BuyListNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
void ListPopBack(LTNode* phead) {
assert(phead);
assert(phead->next != phead);//后面往前推
LTNode* tail = phead->prev;
LTNode* tailPrev = tail->prev;//没有tailPrev的话会断了
free(tail);
tailPrev->next = phead;
phead->prev = tailPrev;
}
void ListPushFront(LTNode* phead, LTDateType x) {
assert(phead);
LTNode* newnode = BuyListNode(x);
LTNode* next = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = next;
next->prev = newnode;
}
void ListPopFront(LTNode* phead) {
assert(phead);
assert(phead->next!=phead);
LTNode* next = phead->next;
LTNode* nextnext = next->next;
phead->next = nextnext;
nextnext->prev = phead;
free(next);
}
#include"List.h"
void test() {
LTNode* plist = ListInit();
ListPushBack(plist,1);
ListPushBack(plist,2);
ListPushBack(plist,3);
ListPushBack(plist,4);
ListPrint(plist);
ListPopBack(plist);
ListPopBack(plist);
ListPopBack(plist);
ListPopBack(plist);
ListPrint(plist);
}
int main() {
test();
return 0;
}