Mercurial > Algorithms__Sedgewick
diff algs4-c++/src/Stack.hpp @ 6:438f5608900e
Linked list based Stack class.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Fri, 29 May 2015 20:52:18 -0500 |
parents | |
children | a5052f77ba7e |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/algs4-c++/src/Stack.hpp Fri May 29 20:52:18 2015 -0500 1.3 @@ -0,0 +1,66 @@ 1.4 +#ifndef STACK_HPP 1.5 +#define STACK_HPP 1.6 + 1.7 +#include <cstddef> 1.8 + 1.9 +// Linked list based stack after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2 1.10 + 1.11 +template <typename T> 1.12 +class Stack { 1.13 + 1.14 +public: 1.15 + 1.16 + //////////////////////////////////// 1.17 + class Node { 1.18 + public: 1.19 + T item; 1.20 + Node *next; 1.21 + }; 1.22 + 1.23 + 1.24 + //////////////////////////////////// 1.25 + class iterator; 1.26 + friend class iterator; 1.27 + class iterator { 1.28 + 1.29 + public: 1.30 + 1.31 + iterator( Node *c ); 1.32 + 1.33 + iterator& operator++(); 1.34 + iterator operator++(int); 1.35 + 1.36 + T operator*(); 1.37 + bool operator!=( typename Stack<T>::iterator ); 1.38 + 1.39 + private: 1.40 + 1.41 + Node *curr; 1.42 + }; 1.43 + 1.44 + //////////////////////////////////// 1.45 + 1.46 + Stack( void ); 1.47 + ~Stack( void ); 1.48 + 1.49 + void push( T &item ); 1.50 + T pop( void ); 1.51 + 1.52 + bool is_empty( void ); 1.53 + size_t size( void ); 1.54 + 1.55 + iterator begin( void ); 1.56 + iterator end( void ); 1.57 + 1.58 +private: 1.59 + 1.60 + size_t N; 1.61 + Node *list; 1.62 + 1.63 + void resize( size_t new_max ); 1.64 + }; 1.65 + 1.66 + 1.67 + 1.68 +#endif 1.69 +