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