scire
Sadh's C++ Impromptu Routines Ensemble
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
queue.hpp
Go to the documentation of this file.
1 // scire/struct/queue
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/queue.hpp
9 
10  scire Queue abstraction:
11  - AQueue : queue abstraction (Enque, Deque, Front)
12 
13  scire Queue implementations:
14  - Queue : standard linked list implementation of queue
15  - QueueCrate : array implementation of queue
16 
17  other required scire files:
18  scire/struct/container.hpp
19 
20  author:
21  ~nafSadh
22  */
23 #ifndef SCIRE_struct_queue_HPP
24 #define SCIRE_struct_queue_HPP
25 
26 #include "container.hpp"
27 
28 namespace scire
29 {
30 
32 #define SCIRE_Queue_ABSTR
33 
34  /**
35  * A Queue should support Enque(), Deque() and Front() operations
36  */
37  template<typename Type, typename SzType>
38  class AQueue : public AContainer < Type, SzType >
39  {
40  public:
41  // -- please implement -- //
42  /**
43  * Enqueue a new element at the end of queue
44  * @parama element element to enqueue
45  * @return true on success
46  */
47  virtual bool Enqueue(const Type& element) = 0;
48 
49  /**
50  * Dequeue the an element from the front of queue
51  * @return true on success
52  */
53  virtual bool Dequeue() = 0;
54 
55  /**
56  * Access the front (next in line) element of queue
57  * @return front element of the queue
58  */
59  virtual Type Front() const = 0;
60 
61  //@implement AContainer
62  virtual SzType Size() const = 0;
63 
64  // -- functions aliases : do not override -- //
65  /** @copydoc AQueue::Enqueue */
66  bool Enque(const Type& element)
67  {
68  return Enqueue(element);
69  }
70 
71  /** @copydoc AQueue::Enqueue */
72  bool Deque()
73  {
74  return Dequeue();
75  }
76 
77  /** @copydoc AQueue::Enqueue */
78  Type Fore() const
79  {
80  return Front();
81  }
82 
83  // -- AQueue wrapped as AContainer -- //
84  //@implement AContainer
85  bool Add(const Type& element)
86  {
87  return Enqueue(element);
88  }
89 
90  //@implement AContainer
91  bool Deduce()
92  {
93  return Dequeue();
94  }
95 
96  //@implement AContainer
97  Type Peek() const
98  {
99  return Front();
100  }
101  };
102 
103 #endif//SCIRE_Queue_ABSTR
104 
106 #define SCIRE_Queue_CLASS
107 
108  /**
109  * Linked List implementation of Queue.
110  *
111  * Queue implemented as linked items. A pointer points to head and another to
112  * tail of the queue. A new element is always added to the tail of the queue
113  * and removed from the head. This is a FIFO structure.
114  */
115  template<typename Type, typename SzType = int>
116  class Queue
117  : public AQueue < Type, SzType >
118  {
119  public:
120  /** initialize a Queue object, with head and tail pointing to nullptr */
121  Queue();
122 
123  /** finalize a Queue object by deleting all items from it */
124  ~Queue();
125 
126  //@implement AQueue
127  bool Enqueue(const Type& element);
128 
129  //@implement AQueue
130  bool Dequeue();
131 
132  //@implement AQueue
133  Type Front() const;
134 
135  //@implement AContainer
136  SzType Size() const;
137 
138  protected:
139 
140  private:
141  /** represent an item in the queue */
142  struct Node {
143  public:
144  Type element; /** element at this node */
145  Node* next; /** pointer to next item */
146 
147  Node(const Type& newElement, Node *nextNode)
148  : element(newElement), next(nextNode) {}
149  };
150 
151  /** entry point of the queue */
152  Node *head;
153 
154  /** exit point of the queue */
155  Node *tail;
156 
157  /** track count of items in the queue */
158  SzType size;
159  };
160 
161  template<typename Type, typename SzType>
162  Queue<Type, SzType>::Queue()
163  : head(0), tail(0), size(0)
164  {
165  //empty ctor body, all init on initlist
166  }
167 
168  template<typename Type, typename SzType>
169  Queue<Type, SzType>::~Queue()
170  {
171  Node *node = head;
172 
173  //remove all nodes to avoid leak
174  while (node != nullptr) {
175  Node *next = node->next;
176  delete node;
177  node = next;
178  }
179  }
180 
181  template<typename Type, typename SzType>
182  bool Queue<Type, SzType>::Enqueue(const Type& element)
183  {
184  Node *node = new Node(element, nullptr);
185  size++;
186 
187  if (head == nullptr) {
188  head = node;
189  }
190 
191  if (tail != nullptr) {
192  tail->next = node;
193  }
194  tail = node;
195 
196  return true;
197  }
198 
199  template<typename Type, typename SzType>
200  bool Queue<Type, SzType>::Dequeue()
201  {
202  if (head == nullptr) return false;
203 
204  Node* temp = head;
205  head = head->next;
206  size--;
207  delete temp;
208 
209  if (head == nullptr) {
210  tail = nullptr;
211  }
212 
213  return true;
214  }
215 
216  template<typename Type, typename SzType>
217  Type Queue<Type, SzType>::Front() const
218  {
219  return this->head->element;
220  }
221 
222  template<typename Type, typename SzType>
223  SzType Queue<Type, SzType>::Size() const
224  {
225  return this->size;
226  }
227 #endif//SCIRE_Queue_CLASS
229 #define SCIRE_QueueCrate_CLASS
230  /**
231  * Array implementation of Queue.
232  */
233  template<typename Type, typename SzType>
235  : public AQueue<Type, SzType>,
236  public ICrate<Type, SzType>
237  {
238  public:
239  QueueCrate();
240  ~QueueCrate();
241 
242  //@implement AQueue
243  bool Enqueue(const Type& element);
244 
245  //@implement AQueue
246  bool Dequeue();
247 
248  //@implement AQueue
249  Type Front() const;
250 
251  //@implement AContainer
252  SzType Size() const;
253 
254  //@implement ICrate
255  SzType Capacity() const;
256 
257  private:
258  /** capacity of the stack crate */
259  SzType capacity;
260 
261  /** array to create the stack */
262  Type *crate;
263 
264  /** track count of items stack */
265  SzType first, last;
266  };
267 #endif//SCIRE_QueueCrate_CLASS
268 }//scire namespace
269 #endif//SCIRE_struct_queue_HPP
Linked List implementation of Queue.
Definition: queue.hpp:116
Array implementation of Queue.
Definition: queue.hpp:234
Type Fore() const
Enqueue a new element at the end of queue element element to enqueue.
Definition: queue.hpp:78
Queue()
initialize a Queue object, with head and tail pointing to nullptr
Definition: queue.hpp:162
bool Enqueue(const Type &element)
Enqueue a new element at the end of queue element element to enqueue.
Definition: queue.hpp:182
virtual bool Enqueue(const Type &element)=0
Enqueue a new element at the end of queue element element to enqueue.
bool Dequeue()
Dequeue the an element from the front of queue.
Definition: queue.hpp:200
SzType Capacity() const
bool Deque()
Enqueue a new element at the end of queue element element to enqueue.
Definition: queue.hpp:72
virtual SzType Size() const =0
number of elements contained in
Type Front() const
Access the front (next in line) element of queue.
Container Inferface.
Definition: container.hpp:40
scire/graph/gale_shapley.hpp
SzType Size() const
number of elements contained in
Definition: queue.hpp:223
bool Deduce()
deduce one element
Definition: queue.hpp:91
bool Dequeue()
Dequeue the an element from the front of queue.
bool Add(const Type &element)
add an element
Definition: queue.hpp:85
SzType Size() const
number of elements contained in
Type Front() const
Access the front (next in line) element of queue.
Definition: queue.hpp:217
A Queue should support Enque(), Deque() and Front() operations.
Definition: queue.hpp:38
bool Enque(const Type &element)
Enqueue a new element at the end of queue element element to enqueue.
Definition: queue.hpp:66
virtual bool Dequeue()=0
Dequeue the an element from the front of queue.
~Queue()
finalize a Queue object by deleting all items from it
Definition: queue.hpp:169
bool Enqueue(const Type &element)
Enqueue a new element at the end of queue element element to enqueue.
Type Peek() const
peek into next element contained in
Definition: queue.hpp:97
Crate contains elements in an arrays.
Definition: container.hpp:89
virtual Type Front() const =0
Access the front (next in line) element of queue.