一个双向链表的实现

来源:互联网 发布:淘宝店刷一颗钻多少钱 编辑:程序博客网 时间: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
;
}
原创粉丝点击