view algs4-c++/src/Bag.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
children a149b424b4e2
line source
1 #ifndef BAG_HPP
2 #define BAG_HPP
4 #include <cstddef>
6 // Linked list based bag after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.2
8 template <typename T>
9 class Bag {
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 Bag<T>::iterator );
38 private:
40 Node *curr;
41 };
43 ////////////////////////////////////
45 Bag( void );
46 ~Bag( void );
48 void add( T &item );
50 bool is_empty( void );
51 size_t size( void );
53 iterator begin( void );
54 iterator end( void );
56 private:
58 size_t N;
59 Node *list;
61 };
65 #endif