All pastes #639539 Raw Edit

Untitled

public text v1 · immutable
#639539 ·published 2007-07-30 17:19 UTC
rendered paste body
A stack is a first-in-last-out data structure where we always remove the item which 
has most recently been added to the stack, this is comparable to a pile of books. You 
place books on top and remove from the top. 
Consider the operations on a stack:    push - adds an element to the top of the stack    pop - removes the element from the top of the stack    top - returns the value of the element at the top of the stack without actually removing it    empty - returns true if the stack is empty and false otherwise 
Reference: page 744 of Savitch for more on stacks. 
Study the class below and answer the questions that follow: 
class Stack 
{ 
    public: 
        Stack(); 
        bool empty() const; 
        int top() const; 
        int pop();  
        void push(int element); 
    private: 
        vector<int> pool; 
}; 
 
//constructor 
Stack::Stack(){ 
    pool.reserve(10);  
 //reserve member function such as vector reserve
} 
(a) Write code for a template version of the Stack class.  
 
(b) Use the following main()function to test your Stack class. 
  
 int main() { 
 Stack<double> dStack; 
  Stack<string> sStack; 
        
  cout<<"Pushing 3.4, 5.5, 2.2, 6.7  -> on to dStack"<<endl; 
  dStack.push(3.4); 
  dStack.push(5.5); 
  dStack.push(2.2); 
  dStack.push(6.7); 
  cout<<endl; 
   
  cout<<"Pushing Sawubona, Molo,Lotjha,Dumela, Hi,Hallo,Ndi  
 Masiari,Avuxeni -> on to sStack"<<endl; 
  sStack.push("Sawubona"); 
  sStack.push("Molo"); 
  sStack.push("Lotjha"); 
  sStack.push("Dumela"); 
  sStack.push("Hi"); 
  sStack.push("Hallo"); 
  sStack.push("Ndi Masiari"); 
  sStack.push("Avuxeni"); 
     
  cout<<endl<<"dStack:"<<endl; 
  Output(dStack); 
  cout<<endl<<"sStack:"<<endl; 
  Output(sStack);  
  return 0; 
} 
 
Note: You will have to write code for the template function Output() which outputs the Stack. 
 
(c) Submit the output produced by the above main( ) function. 
 
Caution:    C++ does include a container class called stack (see page 945 of Savitch) but using those member function 
would be considered cheating. We want you to write your own version of the Stack class.    The Stack class given in Savitch (pages 745 - 746) is different from the class that you are expected to code.