2020-06-20 00:04:56 +08:00
|
|
|
/* This class specifies the basic operation on a stack as a linked list */
|
|
|
|
#ifndef DATA_STRUCTURES_STACK_H_
|
|
|
|
#define DATA_STRUCTURES_STACK_H_
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
/* Definition of the node */
|
|
|
|
template <class Type>
|
|
|
|
struct node {
|
|
|
|
Type data;
|
|
|
|
node<Type> *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Definition of the stack class */
|
|
|
|
template <class Type>
|
|
|
|
class stack {
|
|
|
|
public:
|
|
|
|
/** Show stack */
|
|
|
|
void display() {
|
|
|
|
node<Type> *current = stackTop;
|
|
|
|
std::cout << "Top --> ";
|
2020-06-24 22:27:23 +08:00
|
|
|
while (current != nullptr) {
|
2020-06-20 00:04:56 +08:00
|
|
|
std::cout << current->data << " ";
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "Size of stack: " << size << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Default constructor*/
|
|
|
|
stack() {
|
2020-06-24 22:27:23 +08:00
|
|
|
stackTop = nullptr;
|
2020-06-20 00:04:56 +08:00
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-24 22:26:55 +08:00
|
|
|
/** Copy constructor*/
|
|
|
|
explicit stack(const stack &other) {
|
|
|
|
node<Type> *newNode, *current, *last;
|
|
|
|
|
|
|
|
/* If stack is no empty, make it empty */
|
2020-06-24 22:27:23 +08:00
|
|
|
if (stackTop != nullptr) {
|
|
|
|
stackTop = nullptr;
|
2020-06-24 22:26:55 +08:00
|
|
|
}
|
2020-06-24 22:27:23 +08:00
|
|
|
if (otherStack.stackTop == nullptr) {
|
|
|
|
stackTop = nullptr;
|
2020-06-24 22:26:55 +08:00
|
|
|
} else {
|
|
|
|
current = otherStack.stackTop;
|
|
|
|
stackTop = new node<Type>;
|
|
|
|
stackTop->data = current->data;
|
2020-06-24 22:27:23 +08:00
|
|
|
stackTop->next = nullptr;
|
2020-06-24 22:26:55 +08:00
|
|
|
last = stackTop;
|
|
|
|
current = current->next;
|
|
|
|
/* Copy the remaining stack */
|
2020-06-24 22:27:23 +08:00
|
|
|
while (current != nullptr) {
|
2020-06-24 22:26:55 +08:00
|
|
|
newNode = new node<Type>;
|
|
|
|
newNode->data = current->data;
|
2020-06-24 22:27:23 +08:00
|
|
|
newNode->next = nullptr;
|
2020-06-24 22:26:55 +08:00
|
|
|
last->next = newNode;
|
|
|
|
last = newNode;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = otherStack.size;
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:04:56 +08:00
|
|
|
/** Destructor */
|
|
|
|
~stack() {}
|
|
|
|
|
|
|
|
/** Determine whether the stack is empty */
|
2020-06-24 22:27:23 +08:00
|
|
|
bool isEmptyStack() { return (stackTop == nullptr); }
|
2020-06-20 00:04:56 +08:00
|
|
|
|
|
|
|
/** Add new item to the stack */
|
|
|
|
void 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 */
|
|
|
|
Type top() {
|
2020-06-24 22:27:23 +08:00
|
|
|
assert(stackTop != nullptr);
|
2020-06-20 00:04:56 +08:00
|
|
|
return stackTop->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove the top element of the stack */
|
|
|
|
void pop() {
|
|
|
|
node<Type> *temp;
|
|
|
|
if (!isEmptyStack()) {
|
|
|
|
temp = stackTop;
|
|
|
|
stackTop = stackTop->next;
|
|
|
|
delete temp;
|
|
|
|
size--;
|
|
|
|
} else {
|
|
|
|
std::cout << "Stack is empty !" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear stack */
|
2020-06-24 22:27:23 +08:00
|
|
|
void clear() { stackTop = nullptr; }
|
2020-06-20 00:04:56 +08:00
|
|
|
|
|
|
|
/** Overload "=" the assignment operator */
|
|
|
|
stack<Type> &operator=(const stack<Type> &otherStack) {
|
|
|
|
node<Type> *newNode, *current, *last;
|
|
|
|
|
|
|
|
/* If stack is no empty, make it empty */
|
2020-06-24 22:27:23 +08:00
|
|
|
if (stackTop != nullptr) {
|
|
|
|
stackTop = nullptr;
|
2020-06-20 00:04:56 +08:00
|
|
|
}
|
2020-06-24 22:27:23 +08:00
|
|
|
if (otherStack.stackTop == nullptr) {
|
|
|
|
stackTop = nullptr;
|
2020-06-20 00:04:56 +08:00
|
|
|
} else {
|
|
|
|
current = otherStack.stackTop;
|
|
|
|
stackTop = new node<Type>;
|
|
|
|
stackTop->data = current->data;
|
2020-06-24 22:27:23 +08:00
|
|
|
stackTop->next = nullptr;
|
2020-06-20 00:04:56 +08:00
|
|
|
last = stackTop;
|
|
|
|
current = current->next;
|
|
|
|
/* Copy the remaining stack */
|
2020-06-24 22:27:23 +08:00
|
|
|
while (current != nullptr) {
|
2020-06-20 00:04:56 +08:00
|
|
|
newNode = new node<Type>;
|
|
|
|
newNode->data = current->data;
|
2020-06-24 22:27:23 +08:00
|
|
|
newNode->next = nullptr;
|
2020-06-20 00:04:56 +08:00
|
|
|
last->next = newNode;
|
|
|
|
last = newNode;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = otherStack.size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
node<Type> *stackTop; /**< Pointer to the stack */
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DATA_STRUCTURES_STACK_H_
|