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

  Type | type 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

{
stackTop = nullptr;
size = 0;
}
stack::stackTop
node< Type > * stackTop
Definition: stack.h:146
stack::size
int size
size of stack
Definition: stack.h:147

## stack() [2/2]

template<class Type >

stack< Type >::stack | ( | const stack< Type > & | otherStack | ) |

inlineexplicit

Copy constructor

{
node<Type> *newNode, *current, *last;
/* If stack is no empty, make it empty */
if (stackTop != nullptr) {
stackTop = nullptr;
}
if (otherStack.stackTop == nullptr) {
stackTop = nullptr;
} else {
current = otherStack.stackTop;
stackTop = new node<Type>;
stackTop->data = current->data;
stackTop->next = nullptr;
last = stackTop;
current = current->next;
/* Copy the remaining stack */
while (current != nullptr) {
newNode = new node<Type>;
newNode->data = current->data;
newNode->next = nullptr;
last->next = newNode;
last = newNode;
current = current->next;
}
}
size = otherStack.size;
}
node< Type >

## ~stack()

template<class Type >

stack< Type >::~stack | ( |  | ) |

inline

Destructor

{}

## Member Function Documentation

## clear()

template<class Type >

void stack< Type >::clear | ( |  | ) |

inline

Clear stack

{ stackTop = nullptr; }

## display()

template<class Type >

void stack< Type >::display | ( |  | ) |

inline

Show stack

{
node<Type> *current = stackTop;
std::cout << "Top --> ";
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
std::cout << "Size of stack: " << size << std::endl;
}
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function:

## isEmptyStack()

template<class Type >

bool stack< Type >::isEmptyStack | ( |  | ) |

inline

Determine whether the stack is empty

{ return (stackTop == nullptr); }

## operator=()

template<class Type >

stack<Type>& stack< Type >::operator= | ( | const stack< Type > & | otherStack | ) |

inline

Overload "=" the assignment operator

{
node<Type> *newNode, *current, *last;
/* If stack is no empty, make it empty */
if (stackTop != nullptr) {
stackTop = nullptr;
}
if (otherStack.stackTop == nullptr) {
stackTop = nullptr;
} else {
current = otherStack.stackTop;
stackTop = new node<Type>;
stackTop->data = current->data;
stackTop->next = nullptr;
last = stackTop;
current = current->next;
/* Copy the remaining stack */
while (current != nullptr) {
newNode = new node<Type>;
newNode->data = current->data;
newNode->next = nullptr;
last->next = newNode;
last = newNode;
current = current->next;
}
}
size = otherStack.size;
return *this;
}

## pop()

template<class Type >

void stack< Type >::pop | ( |  | ) |

inline

Remove the top element of the stack

{
node<Type> *temp;
if (!isEmptyStack()) {
temp = stackTop;
stackTop = stackTop->next;
delete temp;
size--;
} else {
std::cout << "Stack is empty !" << std::endl;
}
}
stack::isEmptyStack
bool isEmptyStack()
Definition: stack.h:80
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

{
node<Type> *newNode;
newNode = new node<Type>;
newNode->data = item;
newNode->next = stackTop;
stackTop = newNode;
size++;
}

## top()

template<class Type >

Type stack< Type >::top | ( |  | ) |

inline

Return the top element of the stack

{
assert(stackTop != nullptr);
return stackTop->data;
}

## 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:

- data_structures/stack.h