Data Structures in C++
Public Member Functions | Private Attributes
StaticStack< T > Class Template Reference

Stack implementation using a native C++ array as data storage. More...

#include <StaticStack.hpp>

Inheritance diagram for StaticStack< T >:
Collaboration diagram for StaticStack< T >:

Public Member Functions

std::string getName ()
 Provides the name of the data structure as a string representation. More...
 
 StaticStack (int size)
 Create a fixed-size stack. More...
 
 StaticStack (T data[])
 create the structure and populate it with the data from the array More...
 
 ~StaticStack ()
 
void push (T val)
 Add an element to the top of the stack. More...
 
pop ()
 Remove an element from the top of the stack. More...
 
peek ()
 See the value from the top of the stack, without removing it. More...
 
int getSize ()
 Outputs the number of elements stored in the structure. More...
 
bool isEmpty ()
 Check whether the structure is empty. More...
 
bool isFull ()
 Check whether the structure is full. More...
 

Private Attributes

int top
 
int size
 
T * data
 

Detailed Description

template<class T>
class StaticStack< T >

Stack implementation using a native C++ array as data storage.

Author
Douglas De Rizzo Meneghetti (dougl.nosp@m.asri.nosp@m.zzom@.nosp@m.gmai.nosp@m.l.com)
Date
2017-6-14Stack implementation using a native C++ array as data storage.
Template Parameters
TThe type of object the data structure will contain

Definition at line 17 of file StaticStack.hpp.

Constructor & Destructor Documentation

◆ StaticStack() [1/2]

template<class T>
StaticStack< T >::StaticStack ( int  size)
inlineexplicit

Create a fixed-size stack.

Parameters
sizethe size of the stack

Definition at line 27 of file StaticStack.hpp.

27  {
28  this->size = size;
29  top = - 1;
30  data = new T[size];
31  }

◆ StaticStack() [2/2]

template<class T>
StaticStack< T >::StaticStack ( data[])
inlineexplicit

create the structure and populate it with the data from the array

Parameters
dataan array with data with which the structure will be initialized

Definition at line 35 of file StaticStack.hpp.

35  : this->data(data) {
36  top = sizeof(data) / sizeof(data[0]);
37  }

◆ ~StaticStack()

template<class T>
StaticStack< T >::~StaticStack ( )
inline

Definition at line 39 of file StaticStack.hpp.

39 { delete[]data; }

Member Function Documentation

◆ getName()

template<class T>
std::string StaticStack< T >::getName ( )
inlinevirtual

Provides the name of the data structure as a string representation.

Returns
name of the data structure

Reimplemented from Stack< T >.

Definition at line 23 of file StaticStack.hpp.

23 { return "Static Stack"; }

◆ getSize()

template<class T>
int StaticStack< T >::getSize ( )
inlinevirtual

Outputs the number of elements stored in the structure.

Returns
number of elements stored in the structure

Implements DataStructure.

Definition at line 59 of file StaticStack.hpp.

59  {
60  return top + 1;
61  }

◆ isEmpty()

template<class T>
bool StaticStack< T >::isEmpty ( )
inlinevirtual

Check whether the structure is empty.

Returns
True if empty, otherwise false

Implements DataStructure.

Definition at line 63 of file StaticStack.hpp.

63  {
64  return top == - 1;
65  }

◆ isFull()

template<class T>
bool StaticStack< T >::isFull ( )
inlinevirtual

Check whether the structure is full.

Returns
True if full, otherwise false

Implements DataStructure.

Definition at line 67 of file StaticStack.hpp.

67  {
68  return top == size - 1;
69  }

◆ peek()

template<class T>
T StaticStack< T >::peek ( )
inlinevirtual

See the value from the top of the stack, without removing it.

Returns
The value on the top of the stack

Implements Stack< T >.

Definition at line 53 of file StaticStack.hpp.

53  {
54  if (isEmpty())
55  throw std::out_of_range("The stack is empty.");
56  return data[top];
57  }
bool isEmpty()
Check whether the structure is empty.
Definition: StaticStack.hpp:63

◆ pop()

template<class T>
T StaticStack< T >::pop ( )
inlinevirtual

Remove an element from the top of the stack.

Returns
the value that is being removed

Implements Stack< T >.

Definition at line 47 of file StaticStack.hpp.

47  {
48  if (isEmpty())
49  throw std::out_of_range("The stack is empty.");
50  return data[top --];
51  }
bool isEmpty()
Check whether the structure is empty.
Definition: StaticStack.hpp:63

◆ push()

template<class T>
void StaticStack< T >::push ( val)
inlinevirtual

Add an element to the top of the stack.

Parameters
valthe value to be added to the stack

Implements Stack< T >.

Definition at line 41 of file StaticStack.hpp.

41  {
42  if (isFull())
43  throw std::out_of_range("The stack is full.");
44  data[++ top] = val;
45  }
bool isFull()
Check whether the structure is full.
Definition: StaticStack.hpp:67

Field Documentation

◆ data

template<class T>
T* StaticStack< T >::data
private

Definition at line 20 of file StaticStack.hpp.

◆ size

template<class T>
int StaticStack< T >::size
private

Definition at line 19 of file StaticStack.hpp.

◆ top

template<class T>
int StaticStack< T >::top
private

Definition at line 19 of file StaticStack.hpp.


The documentation for this class was generated from the following file: