一个双向链表的实现
来源:互联网 发布:淘宝店刷一颗钻多少钱 编辑:程序博客网 时间:2024/06/11 02:17
问题来源:http://topic.csdn.net/u/20080423/00/c5530034-9327-47ea-baaa-07c39a61b756.html
本来是想改改算了,最后变成全部重写。既然都是自己写的,也算是原创吧!
代码贴在这儿。随手写的,有些地方不是很合理,也没经过测试。姑且供大家参考。
struct node
{
int data;
struct node *next;
struct node *prior;
node(int val = 0):data(val),next(0),prior(0){next = prior = this;}
node(int val,node * nextNode, node * priorNode):data(val),next(nextNode),prior(priorNode){}
};
node * headnode=new node;
node * CreateDLink(node * head){
int val;
node * currNode;
for(cin>>val;val != 0;cin>>val){
currNode = new node(val,head,head->prior->prior);
head->prior->next = currNode;
head->prior = currNode;
}
return (headnode);
}
void ListDLink(node * head){
node * currNode;
cout <<"now the items of list are: ";
for(currNode = head->next;currNode != head;currNode = currNode->next)
{
cout <<currNode->data <<' ';
}
cout<<endl;
}
int GetNodeCount(node * head){
int count;
node * currNode;
for(count = 0,currNode = head->next;currNode != head;currNode = currNode->next,count++);
return count;
}
node * GetNodeByIndex(node *head, int idx){
int count;
node * currNode;
for(count = 0,currNode = head->next;currNode != head;currNode = currNode->next,count++){
if(count == idx){
return currNode;
}
}
return NULL;
}
int GetNodeValueByIndex(node *head, int idx){
node * targetNode = GetNodeByIndex(head, idx);
if(targetNode)return targetNode->data;
return -1;
}
node * GetNodeByValue(node *head, int val){
node * currNode;
for(currNode = head->next;currNode != head;currNode = currNode->next){
if(currNode->data == val){
return currNode;
}
}
return NULL;
}
int GetNodeIndexByValue(node * head, int val){
int count;
node * currNode;
for(count = 0,currNode = head->next;currNode != head;currNode = currNode->next,count++){
if(currNode->data == val){
return count;
}
}
return -1;
}
node * InsertNode(node *posNode, int x){
node * newNode = NULL;
if(posNode){
newNode = new node(x, posNode->next, posNode);
posNode->next->prior = newNode;
posNode->next = newNode;
}
return newNode;
}
node * InsertNode (node *head,int idx,int x){
node *posNode = GetNodeByIndex(head, idx);
return InsertNode(posNode, x);
}
bool DelNode(node * delNode){
if(delNode){
delNode->next->prior = delNode->prior;
delNode->prior->next = delNode->next;
delete delNode;
return true;
}
return false;
}
bool DelNodeByIndex(node *head, int idx){
node * targetNode = GetNodeByIndex(head, idx);
if(targetNode && targetNode != head){
return DelNode(targetNode);
}
return false;
}
bool DelNodeByValue(node *head, int val){
node * targetNode = GetNodeByValue(head, val);
if(targetNode && targetNode != head){
return DelNode(targetNode);
}
return false;
}
{
int data;
struct node *next;
struct node *prior;
node(int val = 0):data(val),next(0),prior(0){next = prior = this;}
node(int val,node * nextNode, node * priorNode):data(val),next(nextNode),prior(priorNode){}
};
node * headnode=new node;
node * CreateDLink(node * head){
int val;
node * currNode;
for(cin>>val;val != 0;cin>>val){
currNode = new node(val,head,head->prior->prior);
head->prior->next = currNode;
head->prior = currNode;
}
return (headnode);
}
void ListDLink(node * head){
node * currNode;
cout <<"now the items of list are: ";
for(currNode = head->next;currNode != head;currNode = currNode->next)
{
cout <<currNode->data <<' ';
}
cout<<endl;
}
int GetNodeCount(node * head){
int count;
node * currNode;
for(count = 0,currNode = head->next;currNode != head;currNode = currNode->next,count++);
return count;
}
node * GetNodeByIndex(node *head, int idx){
int count;
node * currNode;
for(count = 0,currNode = head->next;currNode != head;currNode = currNode->next,count++){
if(count == idx){
return currNode;
}
}
return NULL;
}
int GetNodeValueByIndex(node *head, int idx){
node * targetNode = GetNodeByIndex(head, idx);
if(targetNode)return targetNode->data;
return -1;
}
node * GetNodeByValue(node *head, int val){
node * currNode;
for(currNode = head->next;currNode != head;currNode = currNode->next){
if(currNode->data == val){
return currNode;
}
}
return NULL;
}
int GetNodeIndexByValue(node * head, int val){
int count;
node * currNode;
for(count = 0,currNode = head->next;currNode != head;currNode = currNode->next,count++){
if(currNode->data == val){
return count;
}
}
return -1;
}
node * InsertNode(node *posNode, int x){
node * newNode = NULL;
if(posNode){
newNode = new node(x, posNode->next, posNode);
posNode->next->prior = newNode;
posNode->next = newNode;
}
return newNode;
}
node * InsertNode (node *head,int idx,int x){
node *posNode = GetNodeByIndex(head, idx);
return InsertNode(posNode, x);
}
bool DelNode(node * delNode){
if(delNode){
delNode->next->prior = delNode->prior;
delNode->prior->next = delNode->next;
delete delNode;
return true;
}
return false;
}
bool DelNodeByIndex(node *head, int idx){
node * targetNode = GetNodeByIndex(head, idx);
if(targetNode && targetNode != head){
return DelNode(targetNode);
}
return false;
}
bool DelNodeByValue(node *head, int val){
node * targetNode = GetNodeByValue(head, val);
if(targetNode && targetNode != head){
return DelNode(targetNode);
}
return false;
}
- 一个双向链表的实现
- 双向链表的一个C++实现
- 一个双向链表的实现
- 双向链表的一个C++实现
- 实现一个双向链表
- 一个简单的双向循环链表的实现
- 一个双向链表的单指针实现
- C语言一个双向链表的实现
- C语言一个双向链表的实现
- C语言一个双向链表的实现
- C语言一个双向链表的实现
- C语言一个双向链表的实现
- 用双向链表实现一个栈
- 使用一个指针实现双向链表
- 双向链表实现一个队列
- 双向链表的实现
- 双向链表的实现
- 双向链表的实现
- Hibernate+Spring+Struts2+ExtJS开发CRUD功能
- Windows中多指针输入技术的实现与应用(5 利用多鼠标输入框架软件实现)
- 2008年度 Webware 100 名单
- Windows中多指针输入技术的实现与应用(6 Single Display Groupware Toolkit的应用)
- SQLServer2005分解并导入xml文件
- 一个双向链表的实现
- Windows中多指针输入技术的实现与应用(4多鼠标输入的底层实现)
- Windows中多指针输入技术的实现与应用(7 分时多鼠标控制系统鼠标的问题)
- 一段重入锁的代码分析
- CSDN改版
- Windows中多指针输入技术的实现与应用(8 总结及继续MFC的讨论
- Windows中多指针输入技术的实现与应用(9 我设想用来实现MFC多鼠标的透明窗口源代码。。。)
- 瘦身饮食的八个数字食谱
- 吃饱吃好又健康四条饮食新建议