scire
Sadh's C++ Impromptu Routines Ensemble
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stack.hpp
Go to the documentation of this file.
1 // scire/struct/stack
2 
3 // Copyright (c) 2014, Khan 'Sadh' Mostafa (http://nafSadh.com/Khan)
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying text at http://www.boost.org/LICENSE_1_0.txt)
6 
7 /**
8  scire/struct/stack.hpp
9 
10  scire Stack abstraction:
11  - AStack : standard stack abstraction (Push, Pop, Top)
12 
13  scire Stack implementations:
14  - Stack : linked list implementation of stack
15  - StackCrate : array implementation of stack
16 
17  other required scire files:
18  scire/struct/container.hpp
19 
20  author:
21  ~nafSadh
22 
23  acknowledgement:
24  http://www.cplusplus.com/forum/general/81166/
25  */
26 #ifndef SCIRE_struct_stack_HPP
27 #define SCIRE_struct_stack_HPP
28 
29 #include "container.hpp"
30 
31 namespace scire
32 {
34 #define SCIRE_Stack_ABSTR
35  /**
36  * Standard stack abstraction (Push, Pop, Top).
37  */
38  template<typename Type, typename SzType = int>
39  class AStack
40  : public AContainer < Type, SzType >
41  {
42  public:
43  // -- please implement -- //
44  /**
45  * Push a new element at the top of stack.
46  * @param element element to push
47  * @return true on success
48  */
49  virtual bool Push(const Type& element) = 0;
50 
51  /**
52  * Pop (remove/deduce) an element from the top.
53  * @return true on success
54  */
55  virtual bool Pop() = 0;
56 
57  /**
58  * Access the top element of stack.
59  * @return element at the top of the stack
60  */
61  virtual Type Top() const = 0;
62 
63  // -- AQueue wrapped as AContainer -- //
64 
65  //@implement AContainer
66  virtual SzType Size() const = 0;
67 
68  // -- AQueue wrapped as AContainer -- //
69  //@implement AContainer
70  bool Add(const Type& element)
71  {
72  return Push(element);
73  }
74 
75  //@implement AContainer
76  bool Deduce()
77  {
78  return Pop();
79  }
80 
81  //@implement AContainer
82  Type Peek() const
83  {
84  return Top();
85  }
86  };
87 #endif//SCIRE_Stack_ABSTR
88 
90 #define SCIRE_Stack_CLASS
91  /**
92  * Linked list implementation of stack.
93  *
94  * Stack implemented as linked items. A top pointer points to first item in
95  * the stack. Each item points to next item of it and the last item points to
96  * nullptr. Items can be added (push) and removed (pop) at top of the stack.
97  */
98  template<typename Type, typename SzType = int>
99  class Stack
100  : public AStack<Type, SzType>
101  {
102  public:
103  /** initialize a Stack object, with top pointing to nullptr */
104  Stack();
105 
106  /** finalize a Stack object by deleting all items from it */
107  ~Stack();
108 
109  //@implement AStack
110  bool Push(const Type& element);
111 
112  //@implement AStack
113  bool Pop();
114 
115  //@implement AStack
116  Type Top() const;
117 
118  //@implement AContainer
119  SzType Size() const;
120 
121  protected:
122 
123  private:
124  /** represent an item in the stack */
125  struct Node {
126  public:
127  Type element; /** element at this node */
128  Node* next; /** pointer to next item */
129 
130  Node(const Type& newElement, Node *nextNode)
131  : element(newElement), next(nextNode) {}
132  };
133 
134  /** points to the top of the stack */
135  Node *top;
136 
137  /** track count of items in the stack */
138  SzType size;
139 
140  };
141 
142  template<typename Type, typename SzType>
143  Stack<Type, SzType>::Stack()
144  : top(0),
145  size(0)
146  {
147  //empty ctor body, all init on initlist
148  }
149 
150  template<typename Type, typename SzType>
151  Stack<Type, SzType>::~Stack()
152  {
153  Node *node = this->top;
154 
155  // remove all node pointers, or else memory will leak
156  while (node != nullptr) {
157  Node *next = node->next;
158  delete node;
159  node = next;
160  }
161  }
162 
163  template<typename Type, typename SzType>
164  SzType Stack<Type, SzType>::Size() const
165  {
166  return this->size;
167  }
168 
169  template<typename Type, typename SzType>
170  bool Stack<Type, SzType>::Push(const Type& element)
171  {
172  this->top = new Node(element, this->top);
173  this->size++;
174 
175  return true;
176  }
177 
178  template<typename Type, typename SzType>
179  bool Stack<Type, SzType>::Pop()
180  {
181  if(this->top==nullptr)
182  return false; //nothing to pop
183 
184  //pop existing top item
185  Node *temp = this->top;
186  this->top = this->top->next;
187  this->size--;
188  delete temp;
189  return true;
190  }
191 
192  template<typename Type, typename SzType>
193  Type Stack<Type, SzType>::Top() const
194  {
195  return this->top->element;
196  }
197 
198 #endif//SCIRE_Stack_CLASS
199 
201 #define SCIRE_StackCrate_CLASS
202  /**
203  * array implementation of stack
204  */
205  template<typename Type, typename SzType = int>
207  : public AStack<Type, SzType>,
208  public ICrate<Type, SzType>
209  {
210  public:
211  /** initialize a StackCrate object and allocate array crate */
212  StackCrate(SzType stackCapacity);
213 
214  /** finalize a StackCrate object by deleting array crate */
215  ~StackCrate();
216 
217  //@implement Crate
218  SzType Capacity() const;
219 
220  //@implement Container
221  SzType Size() const;
222 
223  //@implement AStack
224  bool Push(const Type& element);
225 
226  //@implement AStack
227  bool Pop();
228 
229  //@implement AStack
230  Type Top() const;
231 
232  protected:
233 
234  private:
235  /** capacity of the stack crate */
236  SzType capacity;
237 
238  /** array to create the stack */
239  Type *crate;
240 
241  /** track count of items stack */
242  SzType size;
243  };
244 
245  template<typename Type, typename SzType>
246  StackCrate<Type, SzType>::StackCrate(SzType stackCapacity)
248  crate(0),
249  size(0)
250  {
251  crate = new Type[capacity];
252  }
253 
254  template<typename Type, typename SzType>
255  StackCrate<Type, SzType>::~StackCrate()
256  {
257  delete[] crate;
258  }
259 
260  template<typename Type, typename SzType>
261  SzType StackCrate<Type, SzType>::Size() const
262  {
263  return this->size;
264  }
265 
266  template<typename Type, typename SzType>
267  SzType StackCrate<Type, SzType>::Capacity() const
268  {
269  return this->capacity;
270  }
271 
272  template<typename Type, typename SzType>
273  bool StackCrate<Type, SzType>::Push(const Type& element)
274  {
275  //if size reached the capacity, cannot push
276  if (this->size >= this->capacity) return false;
277 
278  this->crate[size] = element;
279  this->size++;
280 
281  return true;
282  }
283 
284 
285  template<typename Type, typename SzType>
286  bool StackCrate<Type, SzType>::Pop()
287  {
288  //if nothing left in the stack, cannot pop
289  if (size <= 0) return false;
290 
291  this->size--;
292  return true;
293  }
294 
295  template<typename Type, typename SzType>
296  Type StackCrate<Type, SzType>::Top() const
297  {
298  return this->crate[size - 1];
299  }
300 #endif//SCIRE_StackCrate_CLASS
301 }//scire namespace
302 #endif//SCIRE_struct_stack_HPP
Type Peek() const
peek into next element contained in
Definition: stack.hpp:82
virtual SzType Size() const =0
number of elements contained in
Type Top() const
Access the top element of stack.
Definition: stack.hpp:193
bool Add(const Type &element)
add an element
Definition: stack.hpp:70
virtual bool Push(const Type &element)=0
Push a new element at the top of stack.
Linked list implementation of stack.
Definition: stack.hpp:99
SzType Size() const
number of elements contained in
Definition: stack.hpp:164
bool Push(const Type &element)
Push a new element at the top of stack.
Definition: stack.hpp:273
virtual bool Pop()=0
Pop (remove/deduce) an element from the top.
Container Inferface.
Definition: container.hpp:40
scire/graph/gale_shapley.hpp
~StackCrate()
finalize a StackCrate object by deleting array crate
Definition: stack.hpp:255
bool Pop()
Pop (remove/deduce) an element from the top.
Definition: stack.hpp:179
Standard stack abstraction (Push, Pop, Top).
Definition: stack.hpp:39
~Stack()
finalize a Stack object by deleting all items from it
Definition: stack.hpp:151
StackCrate(SzType stackCapacity)
initialize a StackCrate object and allocate array crate
Definition: stack.hpp:246
Type Top() const
Access the top element of stack.
Definition: stack.hpp:296
SzType Size() const
number of elements contained in
Definition: stack.hpp:261
array implementation of stack
Definition: stack.hpp:206
bool Deduce()
deduce one element
Definition: stack.hpp:76
Stack()
initialize a Stack object, with top pointing to nullptr
Definition: stack.hpp:143
SzType Capacity() const
Definition: stack.hpp:267
Crate contains elements in an arrays.
Definition: container.hpp:89
bool Pop()
Pop (remove/deduce) an element from the top.
Definition: stack.hpp:286
bool Push(const Type &element)
Push a new element at the top of stack.
Definition: stack.hpp:170
virtual Type Top() const =0
Access the top element of stack.