双向链表的插入算法
2015-01-23 14:11
134 查看
该实现方法为《C与指针》上的方法!自己为了学习按照该书中给出的思路重新实现并测试了一下!以供以后学习之用!
算法要求:
实现一个双向链表,将给定值插入到链表中,并且保证链表中没有相同的值。
如果要插入的值已经在链表中,直接返回(0)。
如果没有内存可以分配,直接返回(-1);
如果成功插入,直接返回(1);
#include <iostream>
using namespace std;
typedef struct Node{
struct Node* fwdP;
struct Node* bwdP;
int value;
}NODE_ST;
NODE_ST rootNode={0,0,0};
int InsertNodeToTwoWayLinkedList(NODE_ST* rootnodeP,int val)
{
NODE_ST* thisNodeP = rootnodeP;
NODE_ST* nextNodeP = NULL;
NODE_ST* newNodeP = NULL;
/*检测链表中是否已经有该值存在,如果有直接返回零*/
while((nextNodeP=thisNodeP->fwdP)!= NULL)
{
if(nextNodeP->value == val)
return 0;
if(nextNodeP->value > val)
break;
thisNodeP = nextNodeP;
}
newNodeP = new NODE_ST;
if(NULL == newNodeP)
return -1;
newNodeP->value = val;
if( NULL != nextNodeP) //(1)插在链表的非尾部
{
if( rootnodeP != thisNodeP)
//(1.1)插在链表非头部.
{
thisNodeP->fwdP = newNodeP;
newNodeP->fwdP = nextNodeP;
nextNodeP->bwdP = newNodeP;
newNodeP->bwdP = thisNodeP;
}else{
// (1.2)插在链表头部
thisNodeP->fwdP = newNodeP;
newNodeP->fwdP = nextNodeP;
nextNodeP->bwdP = newNodeP;
newNodeP->bwdP = NULL;
}
}else{ //(2)插在链表尾部
if(rootnodeP != thisNodeP)
//(2.1)非空链表并且插在链尾
{
thisNodeP->fwdP = newNodeP;
rootnodeP->bwdP = newNodeP;
newNodeP->fwdP = NULL;
newNodeP->bwdP = thisNodeP;
}else{ //(2.2)这种情况为空链表(空链表并且插在链头)
thisNodeP->fwdP = newNodeP;
thisNodeP->bwdP = newNodeP;
newNodeP->fwdP = NULL;
newNodeP->bwdP = NULL;
}
}
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
InsertNodeToTwoWayLinkedList(&rootNode,4);
InsertNodeToTwoWayLinkedList(&rootNode,2);
InsertNodeToTwoWayLinkedList(&rootNode,1);
InsertNodeToTwoWayLinkedList(&rootNode,7);
InsertNodeToTwoWayLinkedList(&rootNode,5);
InsertNodeToTwoWayLinkedList(&rootNode,7);
NODE_ST* currentNodeP = &rootNode;
//正向测试链表
for(;currentNodeP->fwdP!= NULL; currentNodeP= currentNodeP->fwdP)
cout<< currentNodeP->fwdP->value << endl;
//反向测试链表
currentNodeP = &rootNode;
for(;currentNodeP->bwdP!= NULL; currentNodeP= currentNodeP->bwdP)
cout<< currentNodeP->bwdP->value << endl;
return 0;
}
点评:
与单向链表不同,这里使用的是一个根节点,而不是根指针!根节点中的fwdP字段指向链表的首节点,根节点的bwdP字段指向链表的尾节点。
算法要求:
实现一个双向链表,将给定值插入到链表中,并且保证链表中没有相同的值。
如果要插入的值已经在链表中,直接返回(0)。
如果没有内存可以分配,直接返回(-1);
如果成功插入,直接返回(1);
#include <iostream>
using namespace std;
typedef struct Node{
struct Node* fwdP;
struct Node* bwdP;
int value;
}NODE_ST;
NODE_ST rootNode={0,0,0};
int InsertNodeToTwoWayLinkedList(NODE_ST* rootnodeP,int val)
{
NODE_ST* thisNodeP = rootnodeP;
NODE_ST* nextNodeP = NULL;
NODE_ST* newNodeP = NULL;
/*检测链表中是否已经有该值存在,如果有直接返回零*/
while((nextNodeP=thisNodeP->fwdP)!= NULL)
{
if(nextNodeP->value == val)
return 0;
if(nextNodeP->value > val)
break;
thisNodeP = nextNodeP;
}
newNodeP = new NODE_ST;
if(NULL == newNodeP)
return -1;
newNodeP->value = val;
if( NULL != nextNodeP) //(1)插在链表的非尾部
{
if( rootnodeP != thisNodeP)
//(1.1)插在链表非头部.
{
thisNodeP->fwdP = newNodeP;
newNodeP->fwdP = nextNodeP;
nextNodeP->bwdP = newNodeP;
newNodeP->bwdP = thisNodeP;
}else{
// (1.2)插在链表头部
thisNodeP->fwdP = newNodeP;
newNodeP->fwdP = nextNodeP;
nextNodeP->bwdP = newNodeP;
newNodeP->bwdP = NULL;
}
}else{ //(2)插在链表尾部
if(rootnodeP != thisNodeP)
//(2.1)非空链表并且插在链尾
{
thisNodeP->fwdP = newNodeP;
rootnodeP->bwdP = newNodeP;
newNodeP->fwdP = NULL;
newNodeP->bwdP = thisNodeP;
}else{ //(2.2)这种情况为空链表(空链表并且插在链头)
thisNodeP->fwdP = newNodeP;
thisNodeP->bwdP = newNodeP;
newNodeP->fwdP = NULL;
newNodeP->bwdP = NULL;
}
}
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
InsertNodeToTwoWayLinkedList(&rootNode,4);
InsertNodeToTwoWayLinkedList(&rootNode,2);
InsertNodeToTwoWayLinkedList(&rootNode,1);
InsertNodeToTwoWayLinkedList(&rootNode,7);
InsertNodeToTwoWayLinkedList(&rootNode,5);
InsertNodeToTwoWayLinkedList(&rootNode,7);
NODE_ST* currentNodeP = &rootNode;
//正向测试链表
for(;currentNodeP->fwdP!= NULL; currentNodeP= currentNodeP->fwdP)
cout<< currentNodeP->fwdP->value << endl;
//反向测试链表
currentNodeP = &rootNode;
for(;currentNodeP->bwdP!= NULL; currentNodeP= currentNodeP->bwdP)
cout<< currentNodeP->bwdP->value << endl;
return 0;
}
点评:
与单向链表不同,这里使用的是一个根节点,而不是根指针!根节点中的fwdP字段指向链表的首节点,根节点的bwdP字段指向链表的尾节点。
相关文章推荐
- C#实现双向链表
- 将二叉搜索树转换成一个排序的双向链表
- 双向循环链表
- 双向循环链表模板
- 【C++】模版实现双向链表的各种操作(如:逆置、去重Unique、分类(冒泡)、合并)
- 将一颗搜索树转换成一个排序的双向链表。要求不能创建任何新的节点
- JAVA单向/双向链表的实现
- 把二元查找树转变成排序的双向链表
- 双向链表的创建/插入/删除
- C语言---双向链表的插入、删除、查找操作
- 数据结构实验之链表九:双向链表
- 数据结构与算法--双向链表
- 二元查找树变双向链表
- 将搜索二叉树转换成双向链表
- [编程之美-11]把二元查找树变成为排序的双向链表
- 【双向链表】 hdu4286 Data Handler
- 二级指针实现单链表的插入、删除及 linux内核源码双向链表之奇技
- 剑指offer 之 二叉搜索树与双向链表
- STL泛型编程-list双向链表容器
- 二叉搜索树与双向链表