www.ibooksharing.com
www.csBook-jack.blogspot.com
* programming in C++ , C , Java , HTML , Objective-C , Unix , Javascript , Xcode , iOS , Development , Computer Science and More *
Pages
▼
Tuesday, February 28, 2012
Data Structure and Algorithm - Stack - stackAsArray.h source code
stackAsAraay.h as template
#include <iostream> #include <cassert> #include <string.h> using namespace std;
template<class Type> class stackType { private: int maxStackSize; //variable to store the maximum stack size int stackTop; //variable to point to the top of the stack Type *list; //pointer to the array that holds the //stack elements public: //Function to initialize the stack to an empty state. //Postcondition: stackTop = 0 void initializeStack(){ stackTop = 0; } //Function to determine whether the stack is empty. //Postcondition: Returns true if the stack is empty; // otherwise, returns false. bool isEmptyStack(){ return (stackTop == 0); } //Function to determine whether the stack is full. //Postcondition: Returns true if the stack is full; // otherwise, returns false. bool isFullStack(){ return (stackTop == maxStackSize); } //Function to remove all the elements from the stack. //Postcondition: stackTop = 0 void destroyStack(){ for(int i=0;i<stackTop;i++){ delete list[i]; } stackTop = 0; } //Function to add newItem to the stack. //Precondition: The stack exists and is not full. //Postcondition: The stack is changed and newItem // is added to the top of stack. void push(const Type& newItem){ if(isFullStack()){ cout<<"Stack is Full....."<<endl; }else{ list[stackTop]=newItem; stackTop++; } } //Function to return the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: If the stack is empty, the program // terminates; otherwise, the top element // of the stack is returned. Type top(){ assert(stackTop!=0); // if stackTop = 0 then exit program return list[stackTop-1]; } //Function to remove the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: The stack is changed and the top // element is removed from the stack. void pop(){ if(isEmptyStack()){ cout<<"Stack is Empty..."<<endl; }else{ stackTop--; } } // number of stacks int Nstack(){ return stackTop; } //constructor //Creates an array of the size stackSize to hold the //stack elements. The default stack size is 100. //Postcondition: The variable list contains the base // address of the array, stackTop = 0, and // maxStackSize = stackSize. stackType(int stackSize){ if(stackSize <= 0){ cout<<"Size of stack can't be negative number ..."<<endl; cout<<"Now the program set size = 100 Automatically.."<<endl; stackSize = 100; }else{ maxStackSize = stackSize; } stackTop = 0; list = new Type[maxStackSize]; assert(list!=NULL); // exit program if list is NULL } //destructor //Removes all the elements from the stack. //Postcondition: The array (list) holding the stack // elements is deleted. ~stackType(){ delete[] list; } void print(void); };
No comments:
Post a Comment