Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
stack< Type > Class Template Reference

#include <stack.h>

Collaboration diagram for stack< Type >:
[legend]

Public Member Functions

void display ()
 
 stack ()
 
 stack (const stack< Type > &otherStack)
 
 ~stack ()
 
bool isEmptyStack ()
 
void push (Type item)
 
Type top ()
 
void pop ()
 
void clear ()
 
stack< Type > & operator= (const stack< Type > &otherStack)
 

Private Attributes

node< Type > * stackTop
 
int size
 size of stack
 

Detailed Description

template<class Type>
class stack< Type >

Definition of the stack class

Template Parameters
Typetype of data nodes of the linked list in the stack should contain

Constructor & Destructor Documentation

◆ stack() [1/2]

template<class Type >
stack< Type >::stack ( )
inline

Default constructor

41  {
42  stackTop = nullptr;
43  size = 0;
44  }

◆ stack() [2/2]

template<class Type >
stack< Type >::stack ( const stack< Type > &  otherStack)
inlineexplicit

Copy constructor

47  {
48  node<Type> *newNode, *current, *last;
49 
50  /* If stack is no empty, make it empty */
51  if (stackTop != nullptr) {
52  stackTop = nullptr;
53  }
54  if (otherStack.stackTop == nullptr) {
55  stackTop = nullptr;
56  } else {
57  current = otherStack.stackTop;
58  stackTop = new node<Type>;
59  stackTop->data = current->data;
60  stackTop->next = nullptr;
61  last = stackTop;
62  current = current->next;
63  /* Copy the remaining stack */
64  while (current != nullptr) {
65  newNode = new node<Type>;
66  newNode->data = current->data;
67  newNode->next = nullptr;
68  last->next = newNode;
69  last = newNode;
70  current = current->next;
71  }
72  }
73  size = otherStack.size;
74  }

◆ ~stack()

template<class Type >
stack< Type >::~stack ( )
inline

Destructor

77 {}

Member Function Documentation

◆ clear()

template<class Type >
void stack< Type >::clear ( )
inline

Clear stack

112 { stackTop = nullptr; }

◆ display()

template<class Type >
void stack< Type >::display ( )
inline

Show stack

29  {
30  node<Type> *current = stackTop;
31  std::cout << "Top --> ";
32  while (current != nullptr) {
33  std::cout << current->data << " ";
34  current = current->next;
35  }
37  std::cout << "Size of stack: " << size << std::endl;
38  }
Here is the call graph for this function:

◆ isEmptyStack()

template<class Type >
bool stack< Type >::isEmptyStack ( )
inline

Determine whether the stack is empty

80 { return (stackTop == nullptr); }

◆ operator=()

template<class Type >
stack<Type>& stack< Type >::operator= ( const stack< Type > &  otherStack)
inline

Overload "=" the assignment operator

115  {
116  node<Type> *newNode, *current, *last;
117 
118  /* If stack is no empty, make it empty */
119  if (stackTop != nullptr) {
120  stackTop = nullptr;
121  }
122  if (otherStack.stackTop == nullptr) {
123  stackTop = nullptr;
124  } else {
125  current = otherStack.stackTop;
126  stackTop = new node<Type>;
127  stackTop->data = current->data;
128  stackTop->next = nullptr;
129  last = stackTop;
130  current = current->next;
131  /* Copy the remaining stack */
132  while (current != nullptr) {
133  newNode = new node<Type>;
134  newNode->data = current->data;
135  newNode->next = nullptr;
136  last->next = newNode;
137  last = newNode;
138  current = current->next;
139  }
140  }
141  size = otherStack.size;
142  return *this;
143  }

◆ pop()

template<class Type >
void stack< Type >::pop ( )
inline

Remove the top element of the stack

99  {
100  node<Type> *temp;
101  if (!isEmptyStack()) {
102  temp = stackTop;
103  stackTop = stackTop->next;
104  delete temp;
105  size--;
106  } else {
107  std::cout << "Stack is empty !" << std::endl;
108  }
109  }
Here is the call graph for this function:

◆ push()

template<class Type >
void stack< Type >::push ( Type  item)
inline

Add new item to the stack

83  {
84  node<Type> *newNode;
85  newNode = new node<Type>;
86  newNode->data = item;
87  newNode->next = stackTop;
88  stackTop = newNode;
89  size++;
90  }

◆ top()

template<class Type >
Type stack< Type >::top ( )
inline

Return the top element of the stack

93  {
94  assert(stackTop != nullptr);
95  return stackTop->data;
96  }

Member Data Documentation

◆ stackTop

template<class Type >
node<Type>* stack< Type >::stackTop
private

Pointer to the stack


The documentation for this class was generated from the following file:
node< Type >
std::cout
stack::size
int size
size of stack
Definition: stack.h:147
std::endl
T endl(T... args)
stack::isEmptyStack
bool isEmptyStack()
Definition: stack.h:80
stack::stackTop
node< Type > * stackTop
Definition: stack.h:146