2020-01-13 08:53:13 +08:00
|
|
|
#include "stack.h"
|
2020-05-30 07:26:30 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <iostream>
|
2020-01-13 08:53:13 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/* Default constructor*/
|
|
|
|
template <class Type>
|
|
|
|
stack<Type>::stack()
|
|
|
|
{
|
|
|
|
stackTop = NULL;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destructor */
|
|
|
|
template <class Type>
|
|
|
|
stack<Type>::~stack()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display for testing */
|
|
|
|
template <class Type>
|
|
|
|
void stack<Type>::display()
|
|
|
|
{
|
|
|
|
node<Type> *current = stackTop;
|
|
|
|
cout << "Top --> ";
|
2020-05-30 07:26:30 +08:00
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
cout << current->data << " ";
|
|
|
|
current = current->next;
|
2020-01-13 08:53:13 +08:00
|
|
|
}
|
2020-05-30 07:26:30 +08:00
|
|
|
cout << endl;
|
2020-01-13 08:53:13 +08:00
|
|
|
cout << "Size of stack: " << size << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine whether the stack is empty */
|
|
|
|
template <class Type>
|
|
|
|
bool stack<Type>::isEmptyStack()
|
|
|
|
{
|
|
|
|
return (stackTop == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear stack */
|
|
|
|
template <class Type>
|
|
|
|
void stack<Type>::clear()
|
|
|
|
{
|
|
|
|
stackTop = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new item to the stack */
|
|
|
|
template <class Type>
|
|
|
|
void stack<Type>::push(Type item)
|
|
|
|
{
|
|
|
|
node<Type> *newNode;
|
|
|
|
newNode = new node<Type>;
|
|
|
|
newNode->data = item;
|
|
|
|
newNode->next = stackTop;
|
|
|
|
stackTop = newNode;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the top element of the stack */
|
|
|
|
template <class Type>
|
|
|
|
Type stack<Type>::top()
|
|
|
|
{
|
|
|
|
assert(stackTop != NULL);
|
|
|
|
return stackTop->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the top element of the stack */
|
|
|
|
template <class Type>
|
|
|
|
void stack<Type>::pop()
|
|
|
|
{
|
|
|
|
node<Type> *temp;
|
2020-05-30 07:26:30 +08:00
|
|
|
if (!isEmptyStack())
|
|
|
|
{
|
2020-01-13 08:53:13 +08:00
|
|
|
temp = stackTop;
|
|
|
|
stackTop = stackTop->next;
|
|
|
|
delete temp;
|
|
|
|
size--;
|
2020-05-30 07:26:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-13 08:53:13 +08:00
|
|
|
cout << "Stack is empty !" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Operator "=" */
|
|
|
|
template <class Type>
|
2020-05-30 07:26:30 +08:00
|
|
|
stack<Type> stack<Type>::operator=(stack<Type> &otherStack)
|
2020-01-13 08:53:13 +08:00
|
|
|
{
|
|
|
|
node<Type> *newNode, *current, *last;
|
|
|
|
|
|
|
|
if (stackTop != NULL) /* If stack is no empty, make it empty */
|
|
|
|
stackTop = NULL;
|
|
|
|
if (otherStack.stackTop == NULL)
|
|
|
|
stackTop = NULL;
|
2020-05-30 07:26:30 +08:00
|
|
|
else
|
|
|
|
{
|
2020-01-13 08:53:13 +08:00
|
|
|
current = otherStack.stackTop;
|
|
|
|
stackTop = new node<Type>;
|
|
|
|
stackTop->data = current->data;
|
|
|
|
stackTop->next = NULL;
|
|
|
|
last = stackTop;
|
2020-05-30 07:26:30 +08:00
|
|
|
current = current->next;
|
2020-01-13 08:53:13 +08:00
|
|
|
/* Copy the remaining stack */
|
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
newNode = new node<Type>;
|
|
|
|
newNode->data = current->data;
|
|
|
|
newNode->next = NULL;
|
|
|
|
last->next = newNode;
|
|
|
|
last = newNode;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = otherStack.size;
|
|
|
|
return *this;
|
|
|
|
}
|