定义
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
}ListNode;
ListNode* ListInit(void);
void ListPrint(ListNode* phead);
void ListDestory(ListNode* phead);
void ListPushback(ListNode* phead, LTDataType x);
void ListPushFront(ListNode* phead, LTDataType x);
ListNode* ListFind(ListNode* phead, LTDataType x);
void ListErase(ListNode* pos);
void ListNodeInset(ListNode* pos, LTDataType x);
void ListPopBack(ListNode* phead);
void ListPopFront(ListNode* phead);
创造节点
ListNode* BuyNode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("buynode");
exit(-1);
}
else
{
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
}
初始化
ListNode* ListInit(void)
{
ListNode* phead = BuyNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
尾插
void ListPushback(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newnode = BuyNode(x);
tail->next = newnode;
newnode->next = phead;
phead->prev = newnode;
}
头插
void ListPushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* first = phead->next;
ListNode* newnode = BuyNode(x);
//phead newnode first
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
尾删
void ListPopBack(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
tail = NULL;
}
头删
void ListPopFront(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);
first = NULL;
}
查找
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur!=phead)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
return NULL;
}
}
插入
void ListNodeInset(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newndoe = BuyNode(x);
prev->next= newndoe;
newndoe->prev = prev;
newndoe->next = pos;
pos->prev = newndoe;
}
删除
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
pos=NULL;
}
打印
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while(cur!=phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
销毁
void ListDestory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
复用 头删尾尾删
void ListPopFront(ListNode* phead)
{
assert(phead);
ListErase(phead->next);
}
void ListPopBack(ListNode* phead)
{
assert(phead);
ListErase(phead->prev);
}
复用头插尾插
void ListPushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNodeInset(phead->next,x);
}
尾插
void ListPushback(ListNode* phead, LTDataType x)
{
assert(phead);
ListNodeInset(phead->prev,x);
}