DualCirculationList.h
#ifndef DUALCIRCULATIONLIST_H
#define DUALCIRCULATIONLIST_H
#include<iostream>
//list node
template<typename T>
struct list_node{
typedef list_node<T>* pointer;
T data;
pointer prior;
pointer next;
list_node() :prior(NULL), next(NULL){}
list_node(const T value): data(value), prior(NULL), next(NULL){}
};
//DualCirculationList
template<typename T>
class DualCirculationList{
public:
typedef T value_type;
typedef T* pointer;
typedef list_node<T>* link_type;
DualCirculationList();//constructor
~DualCirculationList();//destructor
int get_length();//get length
bool is_empty();//whether DualCirculationList is empty or not
void traverse_head();//traverse DualCirculationList from head
void traverse_tail();//traverse DualCirculationList from tail
void clear();//clear DualCirculationList
bool get_elem(int i, pointer value);//get the ith data
bool insert(int i, value_type value);//insert data in the ith data
bool remove(int i, pointer value);//remove the ith data
private:
link_type head;
};
//constructor
template<typename T>
DualCirculationList<T>::DualCirculationList() :head(new list_node<T>()){
head->prior = head;
head->next = head;
}
//destructor
template<typename T>
DualCirculationList<T>::~DualCirculationList(){
clear();
delete head;
}
//get length
template<typename T>
int DualCirculationList<T>::get_length(){
int count = 0;
link_type p = head->next;
while (p != head){
p = p->next;
count++;
}
return count;
}
//whether DualCirculationList is empty or not
template<typename T>
bool DualCirculationList<T>::is_empty(){
return head->next == head ? true : false;
}
//traverse DualCirculationList from head
template<typename T>
void DualCirculationList<T>::traverse_head(){
link_type p = head->next;
while (p != head){
std::cout << p->data << " ";
p = p->next;
}
std::cout << std::endl;
}
//traverse DualCirculationList from tail
template<typename T>
void DualCirculationList<T>::traverse_tail(){
link_type p = head->prior;
while (p != head){
std::cout << p->data << " ";
p = p->prior;
}
std::cout << std::endl;
}
//clear DualCirculationList
template<typename T>
void DualCirculationList<T>::clear(){
link_type p = head->next, del;
while (p != head){
del = p;
p = p->next;
delete del;
}
head->prior = head;
head->next = head;
}
//get the ith data
template<typename T>
bool DualCirculationList<T>::get_elem(int i, pointer value){
if (i < 1)
return false;
link_type p = head;
for (int j = 0; j < i; j++){
p = p->next;
if (p == head)
return false;
}
*value = p->data;
return true;
}
//insert data in the ith data
template<typename T>
bool DualCirculationList<T>::insert(int i, value_type value){
if (i < 1)
return false;
link_type p = head;
for (int j = 1; j < i; j++){
p = p->next;
if (p == head)
return false;
}
link_type q = new list_node<T>(value);
q->prior = p;
q->next = p->next;
p->next->prior = q;
p->next = q;
return true;
}
//remove the ith data
template<typename T>
bool DualCirculationList<T>::remove(int i, pointer value){
if (i < 1)
return false;
link_type p = head;
for (int j = 0; j < i; j++){
p = p->next;
if (p == head)
return false;
}
p->prior->next = p->next;
p->next->prior = p->prior;
*value = p->data;
delete p;
return true;
}
#endif
main.cpp
#include"DualCirculationList.h"
using namespace std;
int main(){
DualCirculationList<int> int_list;
cout << int_list.get_length() << endl; //0
cout << boolalpha << int_list.is_empty() << endl;//true
for (int i = 0; i < 10; i++){
int_list.insert(i, i);
}
int_list.traverse_head();//1 2 3 4 5 6 7 8 9
int value;
if (int_list.get_elem(5, &value))
cout << "get element succeed,value is " << value << endl;//get element succeed,value is 5
else
cout << "get element fail" << endl;
if (int_list.insert(7, 20))
cout << "insert succeed" << endl;//insert succeed
else
cout << "insert fail" << endl;
if (int_list.remove(8, &value))
cout << "remove succeed,remove value is " << value << endl;//remove succeed,remove value is 7
else
cout << "remove fail" << endl;
int_list.traverse_head();//1 2 3 4 5 6 20 8 9
int_list.traverse_tail();//9 8 20 6 5 4 3 2 1
cout << int_list.get_length() << endl;//9
int_list.clear();
cout << int_list.get_length() << endl;//0
return 0;
}