#include <stdio.h>
#include <stdlib.h>
typedef struct _Node
{
int nValue;
struct _Node* pPre;
struct _Node* pNext;
}Node;
void Push_Front(Node** pHead,Node** pEnd,int nValue)
{
// 申请一个节点
Node* temp = (Node*)malloc(sizeof(Node));
temp->nValue = nValue;
temp->pNext = NULL;
temp->pPre = NULL;
// 把这个节点放到链表 头
if ((*pHead) == NULL)
{
(*pHead) = temp;
(*pEnd) = temp;
}
else
{
temp->pNext = (*pHead);
(*pHead)->pPre = temp;
// 新来的变成头
(*pHead) = temp;
}
// 头尾连接
(*pHead)->pPre = (*pEnd);
(*pEnd)->pNext = (*pHead);
}
void Push_Back(Node** pHead,Node** pEnd,int nValue)
{
// 申请一个节点
Node* temp = (Node*)malloc(sizeof(Node));
temp->nValue = nValue;
temp->pNext = NULL;
temp->pPre = NULL;
// 把这个节点放到链表 头
if ((*pHead) == NULL)
{
(*pHead) = temp;
(*pEnd) = temp;
}
else
{
(*pEnd)->pNext = temp;
temp->pPre = (*pEnd);
(*pEnd) = temp;
}
// 头尾连接
(*pHead)->pPre = (*pEnd);
(*pEnd)->pNext = (*pHead);
}
void Pop_Front(Node** pHead,Node** pEnd)
{
if (*pHead == NULL) // 没有节点就结束
{
return;
}
if ((*pHead) == (*pEnd)) // 有一个节点
{
free(*pHead);
(*pHead) = NULL;
(*pEnd) = NULL;
return;
}
// 有多个节点
// 记住要删除的这个头
Node* pDel = (*pHead);
(*pHead) = (*pHead)->pNext;
free(pDel);
pDel = NULL;
// 头尾连接
(*pHead)->pPre = (*pEnd);
(*pEnd)->pNext = (*pHead);
}
void Pop_Back(Node** pHead,Node** pEnd)
{
if (*pHead == NULL) // 没有节点就结束
{
return;
}
if ((*pHead) == (*pEnd)) // 有一个节点
{
free(*pHead);
(*pHead) = NULL;
(*pEnd) = NULL;
return;
}
// 有多个节点
// 记住要删除的这个头
Node* pDel = (*pEnd);
(*pEnd) = (*pEnd)->pPre;
free(pDel);
pDel = NULL;
// 头尾连接
(*pHead)->pPre = (*pEnd);
(*pEnd)->pNext = (*pHead);
}
void Insert(Node** pHead,Node** pEnd,int nFindValue,int nInsertValue)
{
if (pHead == NULL)
{
return;
}
Node* temp = *pHead;
do
{
// 看这个节点 是不是要查找值
if (temp->nValue == nFindValue)
{
// 看这个节点是不是 头
if (temp == (*pHead))
{
Push_Front(pHead,pEnd,nInsertValue);
}
else
{
// 申请一个节点
Node* node = (Node*)malloc(sizeof(Node));
node->nValue = nInsertValue;
// 连接
node->pNext = temp;
node->pPre = temp->pPre;
// 断开
temp->pPre = node;
node->pPre->pNext = node;
}
return;
}
temp = temp->pNext;
} while (temp != (*pHead));
}
void Delete(Node** pHead,Node** pEnd,int nDeleteValue)
{
if (pHead == NULL)
{
return;
}
Node* temp = (*pHead);
do
{
if (temp->nValue == nDeleteValue)
{
// 看这个节点 是头还是中间 还是尾
if (temp == (*pHead))
{
Pop_Front(pHead,pEnd);
}
else if(temp == (*pEnd))
{
Pop_Back(pHead,pEnd);
}
else
{
temp->pNext->pPre = temp->pPre;
temp->pPre->pNext = temp->pNext;
free(temp);
temp = NULL;
}
return;
}
temp = temp->pNext;
} while (temp != (*pHead));
}
void Show_Head(Node* pHead)
{
if (pHead == NULL)
{
return;
}
Node* temp = pHead;
do
{
printf("%d ",temp->nValue);
temp = temp->pNext;
} while (temp != pHead);
}
void Show_End(Node* pEnd)
{
if (pEnd == NULL)
{
return;
}
Node* temp = pEnd;
do
{
printf("%d ",temp->nValue);
temp = temp->pPre;
} while (temp != pEnd);
}
int main()
{
Node* pHead = NULL;
Node* pEnd = NULL;
Push_Back(&pHead,&pEnd,1);
Show_Head(pHead);
printf("\n-------------------------------------------\n");
Show_End(pEnd);
printf("\n-------------------------------------------\n");
Pop_Back(&pHead,&pEnd);
Pop_Front(&pHead,&pEnd);
Show_Head(pHead);
printf("\n-------------------------------------------\n");
Show_End(pEnd);
printf("\n-------------------------------------------\n");
Push_Back(&pHead,&pEnd,2);
Push_Front(&pHead,&pEnd,3);
Show_Head(pHead);
printf("\n-------------------------------------------\n");
Show_End(pEnd);
printf("\n-------------------------------------------\n");
Insert(&pHead,&pEnd,3,100);
Show_Head(pHead);
printf("\n-------------------------------------------\n");
Show_End(pEnd);
printf("\n-------------------------------------------\n");
Insert(&pHead,&pEnd,3,200);
Show_Head(pHead);
printf("\n-------------------------------------------\n");
Show_End(pEnd);
printf("\n-------------------------------------------\n");
Delete(&pHead,&pEnd,200);
Show_Head(pHead);
printf("\n-------------------------------------------\n");
Show_End(pEnd);
printf("\n-------------------------------------------\n");
system("pause");
return 0;
}