# 双向链表的概念

``````typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LDataType data;
}ListNode;
``````

# 双向链表的相关函数

## 初始化创建

``````ListNode* ListCreate()
{
}
``````

## 新建一个节点

``````ListNode* BuyListNode(LDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->next = NULL;
node->prev = NULL;
node->data = x;
return node;
}
``````

## 头插

``````void ListPushFront(ListNode* phead, LDataType x)
{
}
``````

``````void ListPushFront(ListNode* phead, LDataType x)
{
ListNode* tail = phead->prev;
ListNode* tailPrev = tail->prev;
free(tail);
}
``````

## 尾插

``````void ListPushBack(ListNode* phead, LDataType x)
{
}
``````

``````void ListPushBack(ListNode* phead, LDataType x)
{
ListNode* tail = phead->prev;
ListNode* newnode = BuyListNode(x);
tail->next = newnode;
newnode->prev = tail;
}
``````

## 头删

``````
{
}
``````

``````void ListPopFront(ListNode* phead)
{
ListNode* first = phead->next;
ListNode* firstNext = first->next;
free(first);
}
``````

## 尾删

``````void ListPopBack(ListNode* phead)
{
}
``````
``````void ListPopBack(ListNode* phead)
{
ListNode* tail = phead->prev;
ListNode* tailPrev = tail->prev;
free(tail);
}
``````

## 打印节点

``````void ListPrint(ListNode* phead)
{
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("n");
}
``````

## 查找

``````ListNode* ListFind(ListNode* phead, LDataType x)
{
ListNode* cur = phead->next;//指向头节点的next才是真实的值域
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
``````

## pos处插入节点

``````void  ListInsert(ListNode* pos, LDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newNode = BuyListNode(x);
prev->next = newNode;
newNode->prev = prev;
pos->prev = newNode;
newNode->next = pos;
}
``````

## pos处删除节点

``````void  ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
free(pos);
prev->next = next;
next->prev = prev;
}
``````

## 判断是否为空

``````int ListEmpty(ListNode* phead)
{
return phead->next == phead ? 1 : 0;
}
``````

## 判断大小

``````int ListSize(ListNode* phead)
{
int len = 0;
ListNode* cur = phead->next;//指向头节点的next才是真实的值域
while (cur != phead)
{
len++;
cur = cur->next;
}
return len;
}
``````

## 销毁操作

``````void ListDestory(ListNode* phead)
{
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
}
``````

THE END