fork download
  1. /**
  2. * Sean Vogel
  3. * 11 -28 -2012
  4. * Just a linked list
  5. */
  6. #ifndef SV_LINK_LIST
  7. #define SV_LINK_LIST
  8. #define NULL 0
  9. #include <limits>
  10.  
  11. namespace sv
  12. {
  13. template <class T>
  14. class list
  15. {
  16. class node
  17. {
  18. public:
  19. node *prev,
  20. *next;
  21.  
  22. T data;
  23.  
  24. node() : prev(NULL), next(NULL), data(NULL){}
  25. node(const T& t) : prev(NULL), next(NULL), data(t){}
  26. };
  27.  
  28. node *_front,
  29. *_back;
  30. unsigned int _size;
  31.  
  32. public:
  33. class iterator
  34. {
  35. node* _node;
  36. public:
  37. iterator() : _node(NULL){}
  38. iterator(node* n) : _node(n){}
  39. iterator(const iterator& it) : _node(it._node){}
  40. T& operator*(){ if(_node)return _node->data; }
  41. T& operator->(){ if(_node)return _node->data; }
  42. iterator& operator++()
  43. {
  44. if(_node)
  45. {
  46. _node = _node->next;
  47. }
  48. return *this;
  49. }
  50. iterator operator++(int)
  51. {
  52. iterator it(*this);
  53. ++(*this);
  54. return it;
  55. }
  56. iterator& operator--()
  57. {
  58. if(_node)
  59. {
  60. _node = _node->prev;
  61. }
  62. return *this;
  63. }
  64. iterator& operator--(int)
  65. {
  66. iterator it(*this);
  67. --(*this);
  68. return it;
  69. }
  70. bool operator!=(const iterator& it) const { return (this->_node != it._node); }
  71.  
  72. iterator& operator=(const iterator& it)
  73. {
  74. _node = it._node;
  75. return *this;
  76. }
  77. };
  78.  
  79. //constructors
  80. list() : _front(NULL), _back(NULL), _size(NULL){}
  81. list(unsigned int , const T& t = T()){}
  82. list(const list<T>&);
  83. template < class InputIterator >
  84. list(InputIterator first, InputIterator last);
  85. ~list();
  86.  
  87. //iterators
  88. iterator begin();
  89. iterator end();
  90. //iterator rbegin();
  91. //iterator rend();
  92.  
  93. //operators
  94. list<T>& operator=(const list<T>&);
  95.  
  96. //element access
  97. T& front();
  98. T& back();
  99.  
  100. //capacity
  101. bool empty() const;
  102. unsigned int size() const;
  103. unsigned int max_size() const;
  104. void resize(unsigned int, const T& t);
  105.  
  106. //modifiers
  107. void push_front(const T&);
  108. void pop_front();
  109. void push_back(const T&);
  110. void pop_back();
  111. //iterator insert(iterator pos, const T&);
  112. //void insert(iterator pos, unsigned int size, const T&);
  113. //void insert(iterator position, InputIterator first, InputIterator last);
  114. //iterator erase(iterator position);
  115. //iterator erase(iterator first, iterator last);
  116. //void swap(list<T>& lst);
  117. void clear();
  118. //void assign(size_type n, const T& u);
  119. //void assign(InputIterator first, InputIterator last);
  120.  
  121. //operations
  122. //void remove(const T& value);
  123. //template <class Predicate>
  124. //void remove_if(Predicate pred);
  125. //void sort(list<T>& lst);
  126. //void merge(list<T>& lst):
  127. //void splice(iterator position, list<T,Allocator>& x);
  128. //void splice(iterator position, list<T,Allocator>& x, iterator i);
  129. //void splice(iterator position, list<T,Allocator>& x, iterator first, iterator last);
  130. //void unique();
  131. //template <class BinaryPredicate>
  132. //void unique(BinaryPredicate binary_pred);
  133. //void reverse();
  134. };
  135.  
  136. template <class T>
  137. typename list<T>::iterator list<T>::begin()
  138. {
  139. return iterator(_front);
  140. }
  141.  
  142. template <class T>
  143. typename list<T>::iterator list<T>::end()
  144. {
  145. return iterator(_back->next);
  146. }
  147.  
  148. /*template <class T>
  149.   typename list<T>::iterator list<T>::rbegin()
  150.   {
  151.   return iterator(_back);
  152.   }
  153.  
  154.   template <class T>
  155.   typename list<T>::iterator list<T>::rend()
  156.   {
  157.   return iterator(_front->prev);
  158.   }*/
  159.  
  160. template <class T>
  161. list<T>::list(const list<T>& lst) : _front(NULL), _back(NULL), _size(NULL)
  162. {
  163. if(!lst.empty())
  164. {
  165. node* n = lst._front;
  166. while(n)
  167. {
  168. push_back(n->data);
  169. n = n->next;
  170. }
  171. }
  172. }
  173.  
  174. template <class T>
  175. list<T>& list<T>::operator=(const list<T>& lst)
  176. {
  177. if(this != &lst && !lst.empty())
  178. {
  179. clear();
  180.  
  181. node* n = lst._front;
  182. while(n)
  183. {
  184. push_back(n->data);
  185. n = n->next;
  186. }
  187. }
  188. return this;
  189. }
  190.  
  191. template <class T>
  192. list<T>::~list()
  193. {
  194. clear();
  195. }
  196.  
  197. template <class T>
  198. bool list<T>::empty()const {return !_front;}
  199.  
  200. template <class T>
  201. unsigned int list<T>::size()const{return _size;}
  202.  
  203. template <class T>
  204. unsigned int list<T>::max_size()const {return std::numeric_limits<T>::max();}
  205.  
  206. template <class T>
  207. void list<T>::resize(unsigned int sz, const T& t = T())
  208. {
  209. while(sz > _size)
  210. {
  211. push_back(t);
  212. }
  213. }
  214.  
  215. template <class T>
  216. T& list<T>::front()
  217. {
  218. if(_front)
  219. return _front->data;
  220. }
  221.  
  222. template <class T>
  223. T& list<T>::back()
  224. {
  225. if(_back)
  226. return _back->data;
  227. }
  228.  
  229. template <class T>
  230. void list<T>::push_front(const T& t)
  231. {
  232. node* n = new node(t);
  233.  
  234. if(_front)
  235. {
  236. n->next = _front;
  237. _front->prev = n;
  238. _front = n;
  239. }
  240. else
  241. {
  242. _back = _front = n;
  243. }
  244. ++_size;
  245. }
  246.  
  247. template <class T>
  248. void list<T>::pop_front()
  249. {
  250. if(front)
  251. {
  252. node* n = _front->next;
  253. delete _front;
  254. _front = n;
  255. --_size;
  256. }
  257. }
  258.  
  259. template <class T>
  260. void list<T>::push_back(const T& t)
  261. {
  262. node* n = new node(t);
  263.  
  264. if(_back)
  265. {
  266. n->prev = _back;
  267. _back->next = n;
  268. _back = n;
  269. }
  270. else
  271. {
  272. _front = _back = n;
  273. }
  274. ++_size;
  275. }
  276.  
  277. template <class T>
  278. void list<T>::pop_back()
  279. {
  280. if(back)
  281. {
  282. node* n = _back->prev;
  283. delete _back;
  284. _back = n;
  285. --_size;
  286. }
  287. }
  288.  
  289. template <class T>
  290. void list<T>::clear()
  291. {
  292. node* n;
  293. while(_front)
  294. {
  295. n = _front->next;
  296. delete _front;
  297. _front = n;
  298. --_size;
  299. }
  300. _front = _back = NULL;
  301. }
  302. }
  303. #endif
  304. #include <iostream>
  305. int main()
  306. {
  307. sv::list<int> lst;
  308. for(int i = 0; i < 10; ++i)
  309. lst.push_front(i + 1);
  310. lst.resize(20, 9333);
  311. if(!lst.empty())
  312. std::cout<<"front= " << lst.front()<<" back= "<<lst.back()<<" size= "<<lst.size()<<std::endl;
  313. {
  314. sv::list<int> lst2 = lst;
  315. if(!lst2.empty())
  316. std::cout<<"front= " << lst2.front()<<" back= "<<lst2.back()<<" size= "<<lst2.size()<<std::endl;
  317. }
  318.  
  319. sv::list<int>::iterator it;
  320. sv::list<int>::iterator end = lst.end();
  321. for(it=lst.begin(); it != lst.end(); it++)
  322. std::cout<<*it<<std::endl;
  323.  
  324. //std::cout<<l.max_size()<<std::endl;
  325. //std::cout<<lst.max_size()<<std::endl;
  326. }
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
front= 10 back= 9333 size= 20
front= 10 back= 9333 size= 20
10
9
8
7
6
5
4
3
2
1
9333
9333
9333
9333
9333
9333
9333
9333
9333
9333