双向链表
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct DLNode
{
int iNum;
struct DLNode *prior;
struct DLNode *next;
}DLNodes, *PDLNodes;
void list_insert_head(PDLNodes *pphead, PDLNodes *pptail, int i)
{
PDLNodes pnew;
pnew = (PDLNodes)calloc(1, sizeof(DLNodes));
pnew->iNum = i;
if(*pptail == NULL)
{
*pphead = pnew;
*pptail = pnew;
}
else
{
pnew->next = *pphead;
(*pphead)->prior = pnew;
*pphead = pnew;
}
}
void list_print_func(PDLNodes phead,PDLNodes ptail)
{
PDLNodes pcur = phead;
printf("\n顺序输出双向链表的所有结点:\n");
while(pcur != NULL)
{
printf("%d\t",pcur->iNum);
pcur = pcur->next;
}
printf("\n");
pcur = ptail;
printf("\n逆序输出双向链表的所有结点:\n");
while(pcur != NULL)
{
printf("%d\t",pcur->iNum);
pcur = pcur->prior;
}
printf("\n");
}
void list_delete_func(PDLNodes *pphead,PDLNodes *pptail,int i)
{
PDLNodes pcur = *pphead;
PDLNodes pbefore = *pphead;
if(NULL == *pptail)
{
printf("\n双向链表为空.\n");
}
else if(i == (*pphead)->iNum)
{
(*pphead)->next->prior = (*pphead)->prior;
*pphead = (*pphead)->next;
free(pcur);
if(*pphead == NULL)
{
*pptail = NULL;
}
}
else
{
while(NULL != pcur)
{
if(i == pcur->iNum)
{
pcur = pcur->next;
pbefore->next = pcur;
pcur->prior = pbefore;
return;
}
else
{
pbefore = pcur;
pcur = pcur->next;
}
}
if(NULL == pcur)
{
printf("\n证明你要删除的数据结点不存在");
}
}
}
void list_find_func(PDLNodes *pphead,PDLNodes *pptail,int i)
{
PDLNodes pcur = *pphead;
PDLNodes pbefore = *pptail;
if(NULL == *pptail)
{
printf("\n双向链表为空.\n");
}
else
{
while(NULL != pcur)
{
if(i == pcur->iNum)
{
printf("\n查询成功,你要查找的数据存在.\n");
return;
}
else
{
pbefore = pcur;
pcur = pcur->next;
}
}
if(NULL == pcur)
{
printf("\n证明你要查找的数据结点不存在.\n\n");
}
}
}
int main(int argc,char* argv[])
{
int i;
PDLNodes phead = NULL;
PDLNodes ptail = NULL;
printf("\n\n请输入你要插入的结点:\n");
while(scanf("%d",&i) != EOF)
{
list_insert_head(&phead, &ptail, i);
}
list_print_func(phead,ptail);
printf("\n\n请输入你要删除结点的数据:");
while (scanf("%d",&i) != EOF)
{
list_delete_func(&phead,&ptail,i);
printf("\n删除之后链表的结果为:");
list_print_func(phead,ptail);
}
printf("\n请你输入要查找的结点:");
while(scanf("%d",&i) != EOF)
{
list_find_func(&phead,&ptail,i);
}
return 0;
}