简单双向链表C/C++版
2009-08-31 00:38
267 查看
最近复习算法与数据结构时,看到双向链表,其操作方便,两个指针域方便查找,但是发现很多给出的示例都是循环双向链表,当然比普通双向链表跟有效,但是作为学习之用,也应该了解会写简单的无环双向链表,在网上发掘资料很少,故把自己写的一个简单的程序贴出来供初学者看看,希望大家提意见:
/*************************************************************************
* 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;
}
/*************************************************************************
* 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;
}
相关文章推荐
- (C++版)链表(三)——实现双向链表的创建、插入、删除等简单操作
- 简单数据结构之双向链表(C++实现)
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- (C++版)链表(四)——实现双向循环链表创建、插入、删除等简单操作
- C++ 双向链表简单实现通讯录
- C++实现简单的双向链表
- 简单双向链表(C++模版技术实现)
- C++ 双向链表的简单操作
- 微软系列面试题c/c++第一题双向链表
- 多文件编程动态开辟空间实现双向链表的应用 c++版
- 程序一:带头结点双向链表的简单实现
- 一个简单的双向链表
- c++链表简单实现
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
- C++实现双向循环链表
- C++实现双向链表
- 一、(2)C++ 实现简单的线性表(链式存储结构 - 单链表)
- 线性表-双向链表的简单知识
- C++实现双向链表的创建,插入,修改,删除
- C++模板类实现的简单的单链表