当前位置:编程学习 > C/C++ >>

Circular Doubly Linked List 双向循环链表 C++ 例子

What a circular doubly linked list looks like?

 

\

 


Look at Figure1, It is a circular doubly linked list have 8 nodes. If you compare it and the array in Figure2, you will find that each node in doubly linked list points to the next node and pre node in the list. Because of the way that linked lists are structured, you can easily add or remove nodes at the beginning or the end of a list, or even in the middle of the list. Below is a list class with simple property. We will add "Insert", "Delete" and other function later.

 

[cpp]
template <class T> class List; 
 
template <class T> 
class Node{ 
    friend List<T>; 
private: 
    T data; 
    Node<T>* pre,* next; 
}; 
 
template <class T> 
class List{ 
private: 
    Node<T>* first; 

template <class T> class List;

template <class T>
class Node{
 friend List<T>;
private:
 T data;
 Node<T>* pre,* next;
};

template <class T>
class List{
private:
 Node<T>* first;
}

 

How to insert a node
There are four cases when you insert a node into the linked list. (1) insert the node at the beginning of the list,  (2) insert the node at the middle of the list, (3) insert the node at the end of the list, (4) out of range

(1). The new node will become the new first node in the list.


(3). The new node will become the new tail node in the list.

(4). Throw a exception


We will talk detail about the second case using three figures below.

 

 

 

 

 

 

 

[cpp]
template<class T> 
void List<T>::Insert(const int& position, const T& v){ 
    if(position < 0 || position > Size()){ 
        throw OutOfBounds(); 
    } 
    Node<T>* newNode = new Node<T>; 
    newNode->data = v; 
 
    if(position == 0){// at the beginning of the list  
        if(!first){ //the list is empty  
            first = newNode; 
            first->next = first->pre = first; 
        }else{ //the new node became the first node  
            newNode->next = first; 
            first->pre = newNode; 
 
            newNode->pre = first->pre; 
            first->pre->pre->next = newNode; 
            first = newNode; 
        } 
    }else{ //at the middle or end of the list  
        Node<T>* nodeBeforePostion = first; 
        //find the node before insert position  
        for(int i = 1; i < position && nodeBeforePostion; ++i){ 
            nodeBeforePostion = nodeBeforePostion->next; 
        } 
        newNode->next = nodeBeforePostion->next; 
        newNode->next->pre = newNode; 
        newNode->pre = nodeBeforePostion; 
        nodeBeforePostion->next = newNode;        
    } 
    ++size; 

template<class T>
void List<T>::Insert(const int& position, const T& v){
 if(position < 0 || position > Size()){
  throw OutOfBounds();
 }
 Node<T>* newNode = new Node<T>;
 newNode->data = v;

 if(position == 0){// at the beginning of the list
  if(!first){ //the list is empty
   first = newNode;
   first->next = first->pre = first;
  }else{ //the new node became the first node
   newNode->next = first;
   first->pre = newNode;

   newNode->pre = first->pre;
   first->pre->pre->next = newNode;
   first = newNode;
  }
 }else{ //at the middle or end of the list
  Node<T>* nodeBeforePostion = first;
  //find the node before insert position
  for(int i = 1; i < position && nodeBeforePostion; ++i){
   nodeBeforePostion = nodeBeforePostion->next;
  }
  newNode->next = nodeBeforePostion->next;
  newNode->next->pre = newNode;
  newNode->pre = nodeBeforePostion;
  nodeBeforePostion->next = newNode;  
 }
 ++size;
}

 

 

 

 

How to delete a node

 

There three cases when you delete a node in the list. (1) Delete the first node, (2) Delete a node at the middle position of the list, (3) out of range

(1). The first node's next node become the new first node.

(3). Throw a exception.


Figure 6,7 and 8 show a graphical representation of how the case 2 acts.

 

 

 

 

 

 

 

 

[cpp]
template<class T> 
void List<T>::Delete(const int& position, T& out){ 
    if(position < 0 || position > Size() || !first){ 
        throw OutOfBounds(); 
    } 
 
    if(position == 0){// at the beginning of the list  
        out = first->data; 
        Node<int>* tmp = first->next; 
        if(tmp == first){ //only one node,just delete it  
            delete first; 
            first = 0; 
        }else{ // set first node's next node to be the new first node,delete the old first node  
            first->pre->next = first->next;&nbs

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,