Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
stack< Type > Class Template Reference
Collaboration diagram for stack< Type >:
[legend]

Public Member Functions

void display ()
 
 stack ()
 
 ~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
 

Constructor & Destructor Documentation

◆ stack()

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

Default constructor

32  {
33  stackTop = NULL;
34  size = 0;
35  }

◆ ~stack()

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

Destructor

38 {}

Member Function Documentation

◆ clear()

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

Clear stack

73 { stackTop = NULL; }

◆ display()

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

Show stack

20  {
21  node<Type> *current = stackTop;
22  std::cout << "Top --> ";
23  while (current != NULL) {
24  std::cout << current->data << " ";
25  current = current->next;
26  }
28  std::cout << "Size of stack: " << size << std::endl;
29  }
Here is the call graph for this function:

◆ isEmptyStack()

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

Determine whether the stack is empty

41 { return (stackTop == NULL); }

◆ operator=()

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

Overload "=" the assignment operator

76  {
77  node<Type> *newNode, *current, *last;
78 
79  /* If stack is no empty, make it empty */
80  if (stackTop != NULL) {
81  stackTop = NULL;
82  }
83  if (otherStack.stackTop == NULL) {
84  stackTop = NULL;
85  } else {
86  current = otherStack.stackTop;
87  stackTop = new node<Type>;
88  stackTop->data = current->data;
89  stackTop->next = NULL;
90  last = stackTop;
91  current = current->next;
92  /* Copy the remaining stack */
93  while (current != NULL) {
94  newNode = new node<Type>;
95  newNode->data = current->data;
96  newNode->next = NULL;
97  last->next = newNode;
98  last = newNode;
99  current = current->next;
100  }
101  }
102  size = otherStack.size;
103  return *this;
104  }

◆ pop()

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

Remove the top element of the stack

60  {
61  node<Type> *temp;
62  if (!isEmptyStack()) {
63  temp = stackTop;
64  stackTop = stackTop->next;
65  delete temp;
66  size--;
67  } else {
68  std::cout << "Stack is empty !" << std::endl;
69  }
70  }
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

44  {
45  node<Type> *newNode;
46  newNode = new node<Type>;
47  newNode->data = item;
48  newNode->next = stackTop;
49  stackTop = newNode;
50  size++;
51  }

◆ top()

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

Return the top element of the stack

54  {
55  assert(stackTop != NULL);
56  return stackTop->data;
57  }

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
std::endl
T endl(T... args)
stack::isEmptyStack
bool isEmptyStack()
Definition: stack.h:41
stack::stackTop
node< Type > * stackTop
Definition: stack.h:107