完成List的头删除和尾删除
This commit is contained in:
@@ -12,12 +12,13 @@ namespace TinySTL{
|
|||||||
template<class T>
|
template<class T>
|
||||||
struct node{
|
struct node{
|
||||||
T data;
|
T data;
|
||||||
|
node *prev;
|
||||||
node *next;
|
node *next;
|
||||||
List<T> *container;
|
List<T> *container;
|
||||||
node(const T& d, node *n, List<T> *c):
|
node(const T& d, node *p, node *n, List<T> *c):
|
||||||
data(d), node(n), container(c){}
|
data(d), prev(p), node(n), container(c){}
|
||||||
bool operator ==(const node& n){
|
bool operator ==(const node& n){
|
||||||
return data == n.data && next == n.next && container == n.container;
|
return data == n.data && prev == n.prev && next == n.next && container == n.container;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//the class of list iterator
|
//the class of list iterator
|
||||||
@@ -40,6 +41,15 @@ namespace TinySTL{
|
|||||||
++*this;
|
++*this;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
listIterator& operator --(){
|
||||||
|
p = p->prev;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
listIterator operator --(int){
|
||||||
|
auto res = *this;
|
||||||
|
--*this;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
T& operator *(){ return p->data; }
|
T& operator *(){ return p->data; }
|
||||||
T* operator &(){ return &(operator*()); }
|
T* operator &(){ return &(operator*()); }
|
||||||
bool operator ==(const listIterator& it){
|
bool operator ==(const listIterator& it){
|
||||||
@@ -82,7 +92,9 @@ namespace TinySTL{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void push_front(const value_type& val);
|
void push_front(const value_type& val);
|
||||||
|
void pop_front();
|
||||||
void push_back(const value_type& val);
|
void push_back(const value_type& val);
|
||||||
|
void pop_back();
|
||||||
|
|
||||||
iterator begin()const{ return head; }
|
iterator begin()const{ return head; }
|
||||||
iterator end()const{ return tail; }
|
iterator end()const{ return tail; }
|
||||||
@@ -91,23 +103,45 @@ namespace TinySTL{
|
|||||||
nodePtr res = nodeAllocator::allocate();
|
nodePtr res = nodeAllocator::allocate();
|
||||||
res->container = this;
|
res->container = this;
|
||||||
res->data = val;
|
res->data = val;
|
||||||
|
res->prev = nullptr;
|
||||||
res->next = nullptr;
|
res->next = nullptr;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
void deleteNode(nodePtr p){
|
||||||
|
p->prev = nullptr;
|
||||||
|
p->next = nullptr;
|
||||||
|
nodeAllocator::deallocate(p);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
template<class T>
|
template<class T>
|
||||||
void List<T>::push_front(const value_type& val){
|
void List<T>::push_front(const value_type& val){
|
||||||
auto node = newNode(val);
|
auto node = newNode(val);
|
||||||
|
head.p->prev = node;
|
||||||
node->next = head.p;
|
node->next = head.p;
|
||||||
head.p = node;
|
head.p = node;
|
||||||
}
|
}
|
||||||
template<class T>
|
template<class T>
|
||||||
|
void List<T>::pop_front(){
|
||||||
|
auto oldNode = head.p;
|
||||||
|
head.p = oldNode->next;
|
||||||
|
head.p->prev = nullptr;
|
||||||
|
deleteNode(oldNode);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
void List<T>::push_back(const value_type& val){
|
void List<T>::push_back(const value_type& val){
|
||||||
auto node = newNode();
|
auto node = newNode();
|
||||||
(tail.p)->data = val;
|
(tail.p)->data = val;
|
||||||
(tail.p)->next = node;
|
(tail.p)->next = node;
|
||||||
|
node->prev = tail.p;
|
||||||
tail.p = node;
|
tail.p = node;
|
||||||
}
|
}
|
||||||
|
template<class T>
|
||||||
|
void List<T>::pop_back(){
|
||||||
|
auto newTail = tail.p->prev;
|
||||||
|
newTail->next = nullptr;
|
||||||
|
deleteNode(tail.p);
|
||||||
|
tail.p = newTail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user