
题目
双向循环链表
解决代码及点评
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#include<assert.h>
typedef struct Link
{
int nValue;
struct Link *pPrev;
struct Link *pNext;
}Link, *PLink;
PLink Create( )
{
PLink pLink = (PLink)malloc(sizeof(Link));
pLink->nValue = 0;
pLink->pPrev = NULL;
pLink->pNext = NULL;
return pLink;
}
//按从小到大的顺序插入数据
PLink Insert(PLink pLink, int nValue)
{
if (NULL == pLink)
{
printf("链表未创建成功!\n");
return NULL;
}
PLink pTmp = (PLink)malloc(sizeof(Link));
pTmp->nValue = nValue;
pTmp->pNext = NULL;
pTmp->pPrev = NULL;
PLink pCur = pLink;
while (pCur->pNext != NULL && nValue >= pCur->pNext->nValue)
{
pCur = pCur->pNext;
}
if (pCur->pNext == NULL)
{//插入到最后一个结点后面
pTmp->pPrev = pCur;
pCur->pNext = pTmp;
}
if (nValue < pCur->pNext->nValue)
{//插入到中间位置
pTmp->pNext = pCur->pNext;
pCur->pNext->pPrev = pTmp;
pTmp->pPrev = pCur;
pCur->pNext = pTmp;
}
return pLink;
}
void Delete(PLink pLink, int nValue)
{
if (NULL == pLink)
{
printf("链表未创建成功!\n");
return;
}
PLink pCur = pLink;
bool bDel = false;
while (pCur->pNext != NULL )
{
//将判断放在while里面是为了能删除所有等于nValue的结点
if (pCur->pNext->nValue == nValue)
{
PLink pTmp = pCur->pNext;
if (NULL == pTmp->pNext)
{//删除最后一个结点
pCur->pNext = NULL;
}
else
{
pCur->pNext = pTmp->pNext;
pTmp->pNext->pPrev = pCur;
}
bDel = true;
free(pTmp);
}
else
{
pCur = pCur->pNext;
}
}
if (!bDel)
{
printf("链表中没有数据 %d,无法删除!\n", nValue);
}
}
PLink Find(PLink pLink, int nValue)
{
PLink pCur = pLink->pNext;
while (NULL != pCur->pNext)
{
if (pCur->nValue == nValue)
{
return pCur;
}
pCur = pCur->pNext;
}
return NULL;
}
void Print(PLink pLink)
{
if (pLink->pNext == NULL)
{
printf("链表为空!\n");
return;
}
PLink pCur = pLink->pNext;
while (pCur != NULL)
{
printf("%d ", pCur->nValue);
pCur = pCur->pNext;
}
}
bool IsEmpty(PLink pLink)
{
return pLink->pNext == NULL;
}
void Destroy(PLink pLink)
{
PLink pCur = pLink;
PLink pDel = NULL;
PLink pNext = NULL;
while (pCur->pNext != NULL)
{
pDel = pCur->pNext;
if (NULL == pDel->pNext)
{//销毁最后一个结点
pCur->pNext = NULL;
}
else
{
pDel->pNext->pPrev = pCur;
pCur->pNext = pDel->pNext;
}
free(pDel);
printf("\n");
Print(pLink);
}
free(pLink);
}
int main()
{
PLink pLink = Create();
pLink = Insert(pLink, 1);
pLink = Insert(pLink, 5);
pLink = Insert(pLink, 8);
pLink = Insert(pLink, 4);
pLink = Insert(pLink, 4);
Delete(pLink, 8);
// if (NULL != Find(pLink, 4))
// {
// printf("链表中有该数据!\n");
// }
// else
// {
// printf("链表中没有该数据!\n");
// }
Print(pLink);
// Destroy(pLink);
system("pause");
return 0;
}
代码下载及其运行
代码下载地址:http://download.csdn.net/detail/yincheng01/6858815
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行
程序运行结果