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

2014-11-24 03:01:58 · 作者: · 浏览: 7

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 List;

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

template
class List{
private:
Node* first;
}

template class List;

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

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