简单双向链表C/C++版

来源:互联网 发布:windows快速返回桌面 编辑:程序博客网 时间:2024/05/30 02:24

最近复习算法与数据结构时,看到双向链表,其操作方便,两个指针域方便查找,但是发现很多给出的示例都是循环双向链表,当然比普通双向链表跟有效,但是作为学习之用,也应该了解会写简单的无环双向链表,在网上发掘资料很少,故把自己写的一个简单的程序贴出来供初学者看看,希望大家提意见:

/*************************************************************************
* Copyright(c)2009-2010 Company Name:CUIT
* All rights reserved
*
* 文件名称:DoubleList.cpp
* 简要描述:该程序实现了双向循环链表的初始化、插入、删除、打印等基本操作
*     此程序仅用于学习交流之用,有问题可E-MAIL:304135053w@.sina.com
*
* 当前版本:2.0
* 作者/修改者:王发修(Acekiller)
* 完成日期:2009.8.30
* 修订说明:编写、调试环境Visual studio 2005
*
* 取代版本:1.0
* 修改人:
* 完成日期:
* 修订说明:
*************************************************************************/

#include <iostream>
#include <stdlib.h>

using namespace std;

#ifdef _MSC_VER
#pragma pack(push, 4)
#endif
typedef struct _DoubleList{
unsigned int data;
struct _DoubleList *rChild, *lChild;
}DoubleList, *pDoubleList;
#ifdef _MSC_VER
#pragma pack(pop, 4)
#endif

pDoubleList InitialList(void);
void InsertNewNode(pDoubleList h, unsigned int data);
void DeleteOneNode(pDoubleList h, unsigned int data);
void ClearList(pDoubleList h);
void ShowList(pDoubleList h);
unsigned int CountNode(pDoubleList h);

const int LEN = sizeof(DoubleList);

//main function
int main(int argc, char **argv)
{
unsigned int data;
pDoubleList head = NULL;

head = InitialList();
cout<<"Please input the data:";
for (int i = 0; i <= 5; i++)
{
   cin>>data;
   InsertNewNode(head, data);
}
ShowList(head);
cout<<"The list node is:"<<CountNode(head)<<endl;
cout<<"Input you want to delete:";
cin>>data;
DeleteOneNode(head, data);
ShowList(head);
ClearList(head);
ShowList(head);
system("pause");
return 0;
}

//Initialize the head node
pDoubleList InitialList(void)
{
pDoubleList head = NULL;
head = (pDoubleList)malloc(LEN);
if (head == NULL)
{
   cout<<"Out of memory!"<<endl;
   exit(-1);
}//end of if

head->data = 0;
head->lChild = NULL;
head->rChild = NULL;

return head;
}

//Insert a new node to the list
void InsertNewNode(pDoubleList h, unsigned int data)
{
pDoubleList pNext = NULL;

if ((pNext = (pDoubleList)malloc(LEN)) == NULL)
{
   cout<<"Out of memory!"<<endl;
   exit(-1);
}//end of if

pNext->data = data;
pNext->rChild = h->rChild;
pNext->lChild = h;
h->rChild = pNext;

return;
}

//Delete a node from the list
void DeleteOneNode(pDoubleList h, unsigned int data)
{
pDoubleList pNext = NULL;
pDoubleList pFount = NULL;
pNext = h->rChild;
pFount = h;

if (pNext == NULL)
{
   cout<<"The list is empty!"<<endl;
   return;
}//end of if

while (pNext->data != data && pNext != NULL)
{
   pFount = pNext;
   pNext = pNext->rChild;
}//end of while

if (pNext == NULL)
{
   cout<<"Haven't find the node!"<<endl;
   return;
}
else if (pNext->rChild == NULL)
{
   pFount->rChild = NULL;
   free(pNext);
   return;
}
else
{
   pFount->rChild = pNext->rChild;
   pNext->rChild->lChild = pNext->lChild;
   free(pNext);
}

return;
}

//Clear the list
void ClearList(pDoubleList h)
{
pDoubleList pNext = NULL;
pDoubleList pFont = NULL;
pNext = h->rChild;
pFont = h;
if (pNext == NULL)
{
   cout<<"The list is empty!"<<endl;
   return;
}//end of if

pFont = pNext;
while (pFont != NULL)
{
   pNext = pFont->rChild;
   free(pFont);
   pFont = pNext;
}//end of while

h->rChild = h->lChild = NULL;
cout<<"Begin to clear...."<<endl;
return;
}

//Display the member of the list
void ShowList(pDoubleList h)
{
pDoubleList pNext = NULL;
pNext = h->rChild;

if (pNext == NULL)
{
   cout<<"The list is empty!"<<endl;
   return;
}//end of if

cout<<"The list is :";
while (pNext != NULL)
{
   cout<<pNext->data<<" ";
   pNext = pNext->rChild;
}//end of while
cout<<endl;

return;
}

//Count the number of the list
unsigned int CountNode(pDoubleList h)
{
unsigned int sum = 0;
pDoubleList pNext = NULL;

pNext = h->rChild;
if (pNext == NULL)
{
   cout<<"The list is empty!"<<endl;
   return sum;
}//end of if

while (pNext != NULL)
{
   ++sum;
   pNext = pNext->rChild;
}//end of while

return sum;
}