Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: venix cador
- // File: stack.cpp
- // Date: 3/18/2013
- #ifndef VENIX_STACK_H_
- #define VENIX_STACK_H_
- #include <iostream>
- #include <cassert>
- template <typename T>
- class node
- {
- public:
- //default constructor for node
- node(): data(), next(0) {};
- //Node constructor for element wiht data
- node(const T& item): data(item), next(0) {};
- T data;
- node<T> *next;
- };
- template <typename T>
- class Stack{
- public:
- Stack(): Tos(0) {};
- ~Stack();
- Stack(const Stack<T>&);
- void swap(Stack<T>&);
- Stack<T>& operator= (Stack<T>);
- void push(const T&);
- T pop();
- T top()const;
- bool isEmpty(){return Tos == 0;};
- bool isFull() const;
- void printAll()const;
- private:
- //pointer to top of stack
- node<T> *Tos;
- };
- //Outputs all the elements of the Stack
- // Ex: out << example_stack;
- template <typename T>
- std::ostream& operator << (std::ostream& out, const Stack<T>& rhs)
- {
- for (Stack<T> temp(rhs); !temp.isEmpty();) {// making a copy of the rhs of the stack and puting it in the ostream and return it back to out because it was call by out
- out << temp.pop();
- }
- return out;
- }
- //determine the current stack is full
- // Ex: full_stack.isfull()
- template<typename T>
- bool Stack<T>::isFull() const
- {
- //try to create a new node in memory
- node<T> *temp = new(std::nothrow) node<T>();
- //if we cannot do that, we are out of memory
- if ( temp == 0){
- return true;
- }
- delete temp;
- return false;
- }
- //destructor
- //
- template <typename T>
- Stack<T>::~Stack()
- {
- while (Tos != 0) {
- node<T> *temp = Tos;
- Tos = Tos->next;
- delete temp;
- }
- }
- //Prints all items in the stack
- //EX: example_stack.printAll();
- template<typename T>
- void Stack<T>::printAll()const{
- //make copy of temp for output
- Stack<T> temp(*this);
- //Pop element one at a time for output
- while (!temp.isEmpty()){
- std::cout << temp.pop() <<' ' << std::endl;
- }
- std::cout << std::endl;
- }
- //swap
- template<typename T>
- void Stack<T>::swap(Stack<T>& rhs){
- //create temp node and copy tos into it
- node<T> *temp = Tos;
- //set tos to what you pass in
- Tos = rhs.Tos;
- //set what you passed in to temp
- rhs.Tos = temp;
- }
- // the operator equal
- template<typename T>
- Stack<T>& Stack<T>::operator =(Stack<T>rhs){
- swap(rhs);
- return *this;
- }
- // stack's copy constructor
- //ex: stack<int> new_stack(old_stack);
- //
- template <typename T>
- Stack<T>::Stack(const Stack<T>& actual){
- node<T> *bottom, *tempTos, *tempnode;
- tempTos = actual.Tos;
- Tos = 0;
- while(tempTos != 0){
- tempnode = new node<T>(tempTos->data);
- if(Tos == 0){
- Tos = tempnode;
- }else{
- bottom->next = tempnode;
- }
- bottom = tempnode;
- tempTos = tempTos->next;
- }
- }
- //pushes an item to the top of the stack
- //ex: example_stack.push(1);
- template <typename T>
- void Stack<T>::push(const T& item)
- {
- //if stack is full, do nothing
- assert(!isFull());
- //create temporary node with passed in data
- node<T> *temp = new node<T>(item);
- //take existing data and appen it to temp
- temp->next = Tos; //Can be (*temp).next, and it is now NULL.
- Tos = temp;
- }
- template <typename T>
- T Stack<T>::pop()
- {
- assert(Tos != 0);
- node<T> *temp = Tos;
- T result = Tos->data;
- Tos = Tos->next;
- delete temp; //Deallocating temp.
- return result;
- } //Now we have a clean stack.
- #endif
Advertisement
Add Comment
Please, Sign In to add comment