实现一个STL的list容器
封装了list
的数据结构,和push_back()
,push_front()
,pop_back()
,pop_front()
,size()
方法,内嵌了iterator
迭代器类,还写了for_each()
和accumulate()
模板算法
#include <iostream>
// #include <algorithm>
// #include <list>
#include <exception>
using namespace std;
namespace miniSTL{
template<typename T>
class list{
struct Node{
T val;
Node* prev;
Node* next;
Node(const T& val):val(val),prev(NULL),next(NULL){}
};
Node* head,*tail;
size_t _size;
public:
list():head(NULL),tail(NULL),_size(0){
head = tail = new Node(T());// 头节点
}
// 拷贝构造/赋值运算符重载函数
~list(){
while(NULL != head){
Node* next = head->next;
delete head;
head = next;
}
head = tail = NULL;
_size = 0;
}
void push_back(const T& val){
++_size;
Node* node = new Node(val);
// if(NULL == head){
// head = tail = node;
// }else{
tail->next = node;
node->prev = tail;
tail = node;
//}
}
void push_front(const T& val){
++_size;
Node* node = new Node(val);
if(head == tail){
node->prev = head;
head->next = node;
tail = node;
}else{
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
}
void pop_back(){
if(0==_size) return;
--_size;
Node* prev = tail->prev;
prev->next = NULL;
delete tail;
tail = prev;
}
void pop_front(){
if(0==_size) return;
--_size;
Node* next = head->next;
head->next = next->next;
if(NULL!=next->next) next->next->prev = head;
delete next;
if(NULL==head->next) tail=head;
}
size_t size()const{
return _size;
}
T& operator[](int index){
// if(index<0 || index >=_size) throw exception(string("invalid index"));
int count = 0;
Node* p = head->next;// 头节点的next是下标为0的元素
while(NULL != p){
if(count++ == index) break;
p = p->next;
}
return p->val;
}
class iterator{
Node* p;
public:
iterator(Node* p):p(p){}
T& operator*(){return p->val;}
T* operator->(){return &(p->val);}
iterator operator++(){
p=p->next;
return *this;
}
iterator operator++(int){
iterator tmp(*this);
p=p->next;
return tmp;
}
iterator operator--(){
p = p->prev;
return *this;
}
iterator operator--(int){
iterator tmp(*this);
p=p->prev;
return tmp;
}
bool operator==(const iterator& it)const{
return p == it.p;
}
bool operator!=(const iterator& it)const{
return p != it.p;
}
};
iterator begin(){return iterator(head->next);} // 头节点的next是下标为0的元素
iterator end(){return iterator(NULL);}
};
template <typename IT,typename FUNC>
void for_each(IT first,IT last,FUNC func){
while(first!=last){
func(*first++);
}
}
template <typename IT,typename VAL>
VAL accumulate(IT first,IT last,VAL val){
while(first!=last){
val+=*first++;
}
return val;
}
}
using namespace miniSTL;
int main(){
list<int> li;
li.push_front(1);
li.push_front(2);
li.push_front(3);
//cout << sizeof(li) << endl;
//cout << li.size() << endl;
//for(int i=0;i<li.size();++i){
// cout << li[i] << endl;
//}
list<int>::iterator it = li.begin();
while(it!=li.end()){
cout << *it++ << " ";
//++it;
}
cout << endl;
for(int i=0;i<li.size();++i){
li.pop_front();
for(auto n:li){
cout << n << " ";
}
cout << endl;
}
/*
li.push_front(0);
li.push_front(-1);
li.push_front(-2);
li.pop_back();
li.pop_back();
li.pop_back();
li.pop_back();
li.pop_back();
li.pop_back();
li.push_front(-1);
li.push_front(-2);
li.pop_front();
li.pop_front();
for_each(li.begin(),li.end(),[](int n){cout << n << endl;});
cout<< accumulate(li.begin(),li.end(),0) << endl;
cout << int() << endl;
cout << float() << endl;
*/
}