#ifndef __DYNINTSTACK_HPP__
#define __DYNINTSTACK_HPP__
#include <iostream>
template <class T>
class DynIntStack
{
private:
//Structure for stack nodes
struct StackNode
{
T value; //Value in the node
StackNode *next; //Pointer to the next node
};
StackNode *top; //Pointer to the stack top
public:
//Constructor
DynIntStack()
{
top = NULL;
}
//Destructor
~DynIntStack();
//Stack Operations
void push(T);
void pop(T &);
bool isEmpty();
// void reversePrint(); //Implement this function
void topOfStack();
void bottomOfStack();
void size();
void sumOfStack();
void displayList() const;
};
//Destructor
//This function deletes every node in the list
template <class T>
DynIntStack<T>::~DynIntStack()
{
StackNode *nodePtr;
StackNode *nextNode;
//Position nodePtr at the top of the stack
nodePtr = top;
//Traverse the list deleting each node
while(nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//Member function push pushes the argument onto the stack
template <class T>
void DynIntStack<T>::push(T num)
{
StackNode *newNode; //Pointer to a new node
//Allocate a new node and store num there
newNode = new StackNode;
newNode->value = num;
//If there are no nodes in the list
//make newNode the first node
if(isEmpty())
{
top =newNode;
newNode->next = NULL;
}
else //otherwise insert NewNode before the top
{
newNode->next = top;
top = newNode;
}
}
// Member function pop, pops the value at the top of the stack
// off, and copies it into the variable passed as an argument
template <class T>
void DynIntStack<T>::pop(T &num)
{
StackNode *temp; //Temporary pointer
//First make sure the stack isn't empty
if(isEmpty())
{
std::cout << "The stack is empty. \n";
}
else //pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
// Member function is empty and returns true if the stack is
// empty, or false otherwise
template <class T>
bool DynIntStack<T>::isEmpty()
{
bool status;
if(!top)
{
status = true;
}
else
{
status = false;
}
return status;
}
template <class T>
void DynIntStack<T>::topOfStack()
{
if(isEmpty()) // If there are no nodes in the list
{
std::cout<<"The Stack is empty\n";
}
else
{
std::cout<<"The value at the top of the stack is: "<< top->value <<"\n";
}
return;
}
template <class T>
void DynIntStack<T>::bottomOfStack()
{
StackNode *currentNode; //Pointer for current value in the stack
if(isEmpty()) // If there are no nodes in the list
{
std::cout<<"The Stack is empty \n";
}
else //otherwise find the last node in the stack
{
currentNode = top;
while(currentNode->next != NULL)
{
currentNode = currentNode->next;
}
std::cout<<"The value at the bottom of the stack is: "<<currentNode->value<<"\n";
return;
}
}
template <class T>
void DynIntStack<T>::size()
{
StackNode *currentNode; // Pointer for current value in the stack
StackNode *nextNode; // Pointer for the next value in the stack
int stackSize =0;
currentNode = top;
while(currentNode!= NULL)
{
currentNode = currentNode->next;
stackSize++;
}
std::cout<<"The Stack size is: "<< stackSize<<"\n";
}
template <class T>
void DynIntStack<T>::sumOfStack()
{
StackNode *currentNode; //Pointer for current value in the stack
StackNode *nextNode; // Pointer for the next value in the stack
int stackSum =0; //variable to store the sum of the stack
// If there are no nodes in the list, distinguish between
// an empty stack and a stack which may have a sum of zero
if(isEmpty())
{
std::cout<<"The Stack is empty \n";
}
else
{
currentNode = top;
while(currentNode!= NULL)
{
stackSum = stackSum + currentNode->value;
currentNode = currentNode->next;
}
std::cout<<"The sum of the numbers in the Stack is: "<< stackSum <<"\n";
}
}
template <class T>
void DynIntStack<T>::displayList() const
{
StackNode *nodePtr; //To move through the list
nodePtr = top; //Position nodePtr at the head of the list
while (nodePtr) //while nodePtr points to a node, traverse the list
{
//Display the value in this node
std::cout<< nodePtr->value<< " ";
//Move to the next node
nodePtr = nodePtr -> next;
}
std::cout<<std::endl;
}
#endif
// ***** DRIVER.CPP ***** //
#include <iostream>
#include <string>
#include <stack>
int main()
{
//stack for integers
DynIntStack<int> stack;
stack.push(2);
stack.push(3);
stack.push(4);
int top { };
std::cout << "--->Integer stack<---\n";
std::cout << "Integer stack\n";
std::cout << "---------------\n";
// stack.print(); // not implemented, can't call
std::cout << "Popping integers\n";
std::cout << "---------------\n";
stack.pop(top);
std::cout << top << '\n';
stack.pop(top);
std::cout << top << '\n';
stack.pop(top);
std::cout << top << '\n';
//stack for characters
DynIntStack<char> charstack;
charstack.push('a');
charstack.push('b');
charstack.push('c');
char ch { };
std::cout << "--->Character stack<---\n";
std::cout << "Character stack\n";
std::cout << "---------------\n";
// charstack.print();
std::cout << "Popping character elements\n";
std::cout << "---------------\n";
charstack.pop(ch);
std::cout << ch << '\n';
charstack.pop(ch);
std::cout << ch << '\n';
charstack.pop(ch);
std::cout << ch << '\n';
//stack for string
DynIntStack<std::string> stringStack;
stringStack.push("windows");
stringStack.push("apple");
stringStack.push("unix");
std::string os { };
std::cout << "--->String stack<---\n";
std::cout << "String stack\n";
std::cout << "---------------\n";
// stringStack.print();
std::cout << "Popping strings elements\n";
std::cout << "---------------\n";
stringStack.pop(os);
std::cout << os << '\n';
stringStack.pop(os);
std::cout << os << '\n';
stringStack.pop(os);
std::cout << os << '\n';
}