Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <string>
- #include <iostream>
- #include "Node.h"
- #include "List.h"
- template <class T>
- class Stack : public List<T>
- {
- private:
- //const int SIZEMAX = 5;
- int size; //size of stack
- Node<T> *baseNode; //a base dummy node to attach other nodes to.
- List<T> *list; //base list to put nodes into.
- public:
- Stack() : list(new List<T>), size(0), baseNode(new Node<T>) //initializeing size to 0, and makeing list and basenode in the heap
- {
- list->setFirst(baseNode); //setting the very first node in the list to be the base node of the class
- };
- void push(Node <T> *ptr);
- void pop();
- void emptystack();
- bool isempty();
- void deleteStack();
- T getTop();
- };
- template <class T>
- T Stack<T>::getTop()
- {
- return list->getData(1); //gets the head nodes data
- }
- template<class T>
- inline void Stack<T>::push(Node <T> *ptr)
- {
- list->insertFirst(ptr);
- size++;
- }
- template<class T>
- inline void Stack<T>::pop()
- {
- size--;
- if (size >= 0)
- {
- list->deleteFirst(); //pops out node at the head of the list
- }
- if (size < 0)
- {
- size++; //if the size is less than zero and would overflow, throw a signal
- throw (size - 1);
- }
- }
- template<class T>
- inline void Stack<T>::emptystack()
- {
- try {
- while (size > 0)
- {
- pop(); //calls pop over and over till the stack is empty
- }
- }
- catch (int er)
- {
- cout << "MAJOR UNDERFLOW ERROR, STACK SIZE: " << size << endl; //if the empty function emptys more than it should that it underflows throws a signal and gets caught
- }
- }
- template<class T>
- inline bool Stack<T>::isempty()
- {
- if (size == 0)
- {
- return true; //checks if the stack is now empty
- }
- /*if (size > SIZEMAX)
- {
- cout << "WARNING OVERFLOWING STACK" << endl; //if the stack is overflowing sends a signal
- throw size;
- }*/
- else if (size < 0)
- {
- cout << "WARNING UNDERFLOWING STACK" << endl; //if the stack is underflowing sends a signal
- throw size;
- }
- else if (size > 0 )
- {
- return false; //if size is not overflowing and not underflowing but still has nodes inside it will return false
- }
- }
- template<class T>
- void Stack<T>::deleteStack()
- {
- list->emptyList(); //deletes all nodes in the list
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement