#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct DListNode
{
struct DListNode* _next;
struct DListNode* _prev;
DataType _data;
}DListNode;
DListNode* BuyDListNode(DataType x)
{
DListNode *NewNode;
NewNode = (DListNode*)malloc(sizeof(DListNode));
if (NewNode == NULL)
{
printf("空间分配失败\n");
}
else
{
NewNode->_data = x;
NewNode->_next = NULL;
NewNode->_prev = NULL;
}
return NewNode;
}
DListNode* DListInit()
{
DListNode *head;
head = BuyDListNode(0);
head->_next = head;
head->_prev = head;
return head;
}
void DListDestory(DListNode* head)
{
assert(head);
DListNode *cur;
cur = head;
while (head->_next != head)
{
cur = head;
head = (head)->_next;
free(cur);
}
}
void DListPushBack(DListNode* head, DataType x)
{
assert(head);
DListNode *cur = NULL;
DListNode *NewNode;
NewNode = BuyDListNode(x);
if (head->_next == head)
{
head->_next = NewNode;
head->_prev = NewNode;
NewNode->_next = head;
NewNode->_prev = head;
}
cur = head->_prev;
cur->_next = NewNode;
head->_prev = NewNode;
NewNode->_next = head;
NewNode->_prev = cur;
}
void DListPushFront(DListNode* head, DataType x)
{
assert(head);
DListNode *cur = NULL;
DListNode *NewNode;
NewNode = BuyDListNode(x);
cur = head->_next;
head->_next = NewNode;
NewNode->_prev = head;
NewNode->_next = cur;
cur->_prev = NewNode;
}
void DListPopBack(DListNode* head)
{
assert(head);
DListNode *cur = NULL;
/*DListNode *cur2 = NULL;*/
/*cur = head->_prev->_prev;
cur2 = head->_prev;
head->_prev = cur;
cur->_next = head;
free(cur2);
cur2->_next = NULL;
cur2->_prev = NULL;*/
cur = head->_prev;
head->_prev = head->_prev->_prev;
head->_prev->_next = head;
free(cur);
}
void DListPopFront(DListNode* head)
{
assert(head);
DListNode *cur = NULL;
/*DListNode *cur2 = NULL;
cur = head->_next->_next;
cur2 = head->_next;
head->_next = cur;
cur->_prev = head;
free(cur2);
cur2->_next = NULL;
cur2->_prev = NULL;*/
cur = head->_next;
head->_next = head->_next->_next;
head->_next->_prev = head;
free(cur);
}
DListNode* DListFind(DListNode* head, DataType x)
{
assert(head);
DListNode *cur = NULL;
cur = head;
while (cur->_data != x)
{
cur = cur->_next;
}
return cur;
}
void DListInsert(DListNode* pos, DataType x)
{
DListNode *cur = NULL;
DListNode *NewNode;;
NewNode = BuyDListNode(x);
cur = pos->_prev;
cur->_next = NewNode;
NewNode->_prev = cur;
NewNode->_next = pos;
pos->_prev = NewNode;
}
void DListErase(DListNode* pos)
{
DListNode *cur;
DListNode *cur2;
cur = pos->_prev;
cur2 = pos->_next;
cur->_next = cur2;
cur2->_prev = cur;
free(pos);
}
void DListPrint(DListNode* head)
{
assert(head);
DListNode *cur;
cur = head;
while (cur->_next != head)
{
printf("%d", cur->_data);
cur = cur->_next;
}
printf("%d", cur->_data);
}
void TestDList1()
{
DListNode* list=DListInit();
DListPushBack(list, 1);
DListPushBack(list, 2);
DListPushBack(list, 3);
DListPushBack(list, 4);
DListPushBack(list, 5);
DListPrint(list);
DListPopBack(list);
DListPopBack(list);
DListPopBack(list);
DListPopBack(list);
DListPopBack(list);
//DListPrint(list);
}
void TestDList2()
{
DListNode *list=DListInit();
DListPushFront(list, 1);
DListPushFront(list, 2);
DListPushFront(list, 3);
DListPushFront(list, 4);
DListPushFront(list, 5);
DListPrint(list);
DListPopFront(list);
DListPopFront(list);
DListPopFront(list);
DListPopFront(list);
DListPopFront(list);
DListPrint(list);
}
int main()
{
TestDList1();
system("pause");
TestDList2();
system("pause");
return 0;
}