Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Matt Short
- Assignment 4
- Purpose: create a template stack ADT class
- CPSC 131*/
- //SPECIFICATION FILE
- template<class dataType>
- class stackADT
- {
- public:
- stackADT(); //constructor
- bool isEmpty() const;
- /*Purpose: Determines wheather the stack is empty.
- Precondition: Stack has been initialized.
- Postcondition: Function value = (stack is empty).*/
- bool isFull() const;
- /*Purpose: Determines wheather the stack is full.
- Precondition: Stack has been initialized.
- Postcondition: Function calue = (stack is full).*/
- void Push(dataType newItem);
- /*Purpose: Adds newItem to the top of the stack
- Precondition: Stack has been initialized
- Postcondition: If (stack is full), exception FullStack is thrown, else newItem is at the top of the stack.*/
- void Pop();
- /*Purpose: Removes top item from the stack.
- Precondition: Stack has been initialized.
- Postcondition: If (stack is full), exception FullStack is thrown, else newItem is at the top of the stack.*/
- dataType Top() const;
- /*Purpose: Returns a copy of the top item on the stack.
- Precondition: Stack has been initialized.
- Postconditions: If (stack is empty), exception EmptyStack is thrown, else a copy of the top element is returned.*/
- ~stackADT(); //deconstructor
- private:
- int *link;
- dataType info;
- int top;
- dataType *items;
- };
- //IMPLEMENTATION FILE
- #include<iostream>
- using namespace std;
- template<class dataType>
- stackADT<dataType>::stackADT()
- {
- max=200;
- items=new dataType[max];
- top=-1;
- }
- template<class dataType>
- bool stackADT<dataType>::isEmpty() const
- {
- if(top==-1)
- return true;
- else
- return false;
- }
- template<class dataType>
- bool stackADT<dataType>::isFull() const
- {
- if(top==max-1)
- return true;
- else
- return false;
- }
- template<class dataType>
- void stackADT<dataType>::Push(dataType newItem)
- {
- if(isFull())
- {
- cout<<"Stack is full."<<endl;
- return;
- }
- else
- {
- top++;
- items[top]=newItem;
- }
- }
- template<class dataType>
- void stackADT<dataType>::Pop()
- {
- if(isEmpty())
- {
- cout<<"Stack is Empty."<<endl;
- return;
- }
- else
- {
- temp = new
- temp->link=top->link;
- top->link=top->link;
- delete temp;
- top--;
- }
- }
- template<class dataType>
- dataType stackADT<dataType>::Top() const
- {
- if(!isEmpty())
- return top->info;
- }
- template<class dataType>
- stackADT<dataType>::~stackADT()
- {
- while(!isEmpty)
- {
- Pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement