/**************************************************************************************** * Program Name: stackType.h * Author: Zhixiang Chen * Course: CSCI/CMPE 3333, Fall 2014 * Lab 9: Header file for Lab 9 * - deriving the array based stack from the array based list * Date: 08/2014 * Description: This file contains the prototype of the class arrayStackType *****************************************************************************************/ #ifndef H_ArrayStackType #define H_ArrayStackType #include #include #include #include "arrayBasedListType.h" using namespace std; //derive arrayStackType from arrayListType template class arrayStackType: public arrayListType { //overload insertion operator << friend ostream& operator<<(ostream& os, const arrayStackType& x); public: const arrayStackType & operator= // overloading assignment operator (const arrayStackType&); Type & operator[] (int index); // index operator overloading //two major stack operation Type pop(); //pop the top element void push(Type & item); //push an item upto stack arrayStackType():arrayListType() // default constructor { top = -1; } arrayStackType(int n):arrayListType(n) // another constructor { top = -1; } arrayStackType(const arrayStackType&); // copy constructor ~arrayStackType(){}; // the destructor private: int top; void arrayStackType::copyStack(const arrayStackType & rhs); //copy stack method }; //pop template Type arrayStackType::pop() //pop the top element { assert(!isEmpty()); //make sure the stack is not empty if (!isEmpty()) { Type tmp = list[top--]; deleteItem(tmp); return tmp; } else { cout<<"No pop, the stack is empty." < void arrayStackType:: push(Type & item) //push an item upto stack { assert(!isFull()); //make sure no full if(!isFull()) { insertLast(item); top++; } else { cout<<"No push, the stack is full."< ostream& operator<<<>(ostream& os, const arrayStackType& x) { int flag = 1; for (int i = 0; i < x.length(); i++) { os << x[i] << " "; if (flag %5 == 0) os << endl; flag++; } return os; } // index operator overloading template Type & arrayStackType::operator[] (int index) { assert(0<= index && index < count); //check the range of the index return list[index]; } // overloading assignment operator template const arrayStackType & arrayStackType::operator= (const arrayStackType& rhs) { if (this != & rhs) { copyStack(rhs); } return *this; } //copy stack template void arrayStackType::copyStack(const arrayStackType & rhs) { count = rhs.count; maxSize = rhs.maxSize; delete[] list; if (rhs.isEmpty()) list = NULL; else { list = new Type[maxSize]; for (int i = 0; i arrayStackType::arrayStackType(const arrayStackType& rhs) // copy constructor { top = rhs.top; count = rhs.count; maxSize = rhs.maxSize; delete[] list; if (rhs.isEmpty()) list = NULL; else { list = new Type[maxSize]; for (int i = 0; i