Node
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;
first->next->pre = first->pre;
delete first;
first = tmp;
}
}else{//at the middle or end of the list
Node
//find the node which need to delete
for(int i = 0; i< position && nodeDelete->next != first; ++i){
nodeDelete = nodeDelete->next;
}
nodeDelete->pre->next = nodeDelete->next;
nodeDelete->next->pre = nodeDelete->pre;
out = nodeDelete->data;
delete nodeDelete;
}
--size;
}
template
void List
if(position < 0 || position > Size()){
throw OutOfBounds();
}
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
//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
Node
newNode->data = v;
if(!first){ // the list is empty
first = newNode;
first->next = first->pre = first;
}else{
Node
first->pre = newNode;
newNode->next = first;
lastNode->next = newNode;
newNode->pre = lastNode;
}
++size;
}
template
int List
return size;
}
template
T List
if(!first){
throw OutOfBounds();
}else{
return first->data;
}
}
template
T List
if(!first){
throw OutOfBounds();
}else{
return first->pre->data;
}
}
template
void List
current = first;
firstTime = true;
}
template
void List
current = current->next;
}
template
bool List
if(!first || (current == first && !firstTime)){
return false;
}else{
firstTime = false;
return true;
}
}
template
T List
return current->data;
}
template
void List
while(Size() != 0){
Delete(0);
}
size = 0;
}
#endif
#ifndef _List_
#define _List_
#include
using namespace std;
//exception
class OutOfBounds{
public:
OutOfBounds(){}
};
template
template
class Node{
friend List
private:
T data;
Node
};
template
class List{
//overwrite operator <<
template
private:
Node
int size;
bool fir