view algs4-c++/src/Stack.hpp @ 15:63df3e6590e2

Bag and RandomBag (1.3.34) classes. There are not really robust enough to use as they will leak memory on destruction if you use them to store dynamically allocated objects.
author Eris Caffee <discordia@eldalin.com>
date Thu, 11 Jun 2015 16:30:14 -0500
parents 0f99a5ae6cd4
children a149b424b4e2
line source
1 #ifndef STACK_HPP
2 #define STACK_HPP
4 #include <cstddef>
6 // Linked list based stack after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2
8 template <typename T>
9 class Stack {
11 private:
13 ////////////////////////////////////
14 class Node {
15 public:
16 T item;
17 Node *next;
18 };
21 public:
23 ////////////////////////////////////
24 class iterator;
25 friend class iterator;
26 class iterator {
28 public:
30 iterator( Node *c );
32 iterator& operator++();
33 iterator operator++(int);
35 T operator*();
36 bool operator!=( typename Stack<T>::iterator );
38 private:
40 Node *curr;
41 };
43 ////////////////////////////////////
45 Stack( void );
46 ~Stack( void );
48 void push( T &item );
49 T pop( void );
51 bool is_empty( void );
52 size_t size( void );
54 iterator begin( void );
55 iterator end( void );
57 #ifdef EXERCISES
59 T delete_bottom( void ); // 1.3.19
60 T remove( size_t k ); // 1.3.20
61 bool find( T key ); // 1.3.21
63 #endif
65 private:
67 size_t N;
68 Node *list;
70 };
74 #endif