Data Structures in C++
DynamicStack.hpp
Go to the documentation of this file.
1 /**
2  * @author Douglas De Rizzo Meneghetti (douglasrizzom@gmail.com)
3  * @date 2017-6-14
4  * @brief Stack implementation with dynamic memory allocation
5  */
6 
7 #ifndef AULA1_DYNAMICSTACK_HPP
8 #define AULA1_DYNAMICSTACK_HPP
9 
10 #include <stdexcept>
11 #include <iostream>
12 #include "Stack.hpp"
14 
15 //! Stack implementation with dynamic memory allocation.
16 //! This stack extends a ProtectedLinkedList in order to gain dynamic memory allocation powers.
17 //! \tparam T The type of object the data structure will contain
18 template<class T>
19 class DynamicStack : public ProtectedLinkedList<T>, public Stack<T> {
20  public:
21  explicit DynamicStack() {}
22 
23  //! create the structure and populate it with the data from the array
24  //! \param data an array with data with which the structure will be initialized
25  explicit DynamicStack(T data[]) : ProtectedLinkedList<T>(data) {}
26 
27  std::string getName() { return "Dynamic Stack"; }
28 
29  void push(T val) { ProtectedLinkedList<T>::insert(val, getSize()); }
30 
31  T pop() {
32  if (isEmpty())
33  throw std::out_of_range("The stack is empty");
34  return ProtectedLinkedList<T>::remove(getSize() - 1);
35  }
36 
37  T peek() {
38  if (isEmpty())
39  throw std::out_of_range("The stack is empty");
40  return ProtectedLinkedList<T>::get(getSize() - 1);
41  }
42 
43  int getSize() { return ProtectedLinkedList<T>::getSize(); }
44 
45  bool isEmpty() { return ProtectedLinkedList<T>::isEmpty(); }
46 
47  bool isFull() { return ProtectedLinkedList<T>::isFull(); }
48 };
49 
50 #endif
Stack implementation with dynamic memory allocation.
bool isFull()
Check whether the structure is full.
T peek()
See the value from the top of the stack, without removing it.
int getSize()
Outputs the number of elements stored in the structure.
Abstract stack interface.
Definition: Stack.hpp:16
<<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD
bool isFull()
Check whether the structure is full.
======= >>>>>>> 36f9b37... fixed dependency of ProtectedLinkedList to Iterator ======= >>>>>>> bded2143692dca559ffcc9e7202d9eb5fbfc45bf ======= >>>>>>> master
std::string getName()
Provides the name of the data structure as a string representation.
bool isEmpty()
Check whether the structure is empty.
Doubly-linked list implementation with dynamic memory allocation.
DynamicStack(T data[])
create the structure and populate it with the data from the array
T pop()
Remove an element from the top of the stack.
void push(T val)
Add an element to the top of the stack.