HOMEWORK 6

Due: December 4th before class. NO LATE SUBMISSIONS.

Techniques: Templates

 

Build a template stack class (DO NOT USE THE STANDARD C++ STACK CLASS ) that will support the following functions:

  1. empty() - returns true if the stack is empty and false otherwise
  2. size() - returns the size of the stack ( i.e. the number of elements in the stack )
  3. pop() – removes, but does not return, the the top element of the stack
  4. top() - Returns, but does not remove, the top element of the stack
  5. push() - Places a new top element on the stack.

 

A current image of an integer stack maybe :

2 ß top

3

4

7

8

3

1 ß bottom

 

empty() will return false

size() should return 7

pop() will give you

3 ß top

4

7

8

3

  1. ß bottom

top will return 3

 

 

 

 

 

 

push(10) will return the following stack:

10 ß top

3

4

7

8

3

1 ß bottom

top() followed by a pop() will return AND remove the top most element of the stack.

Output: In your main(), create at least two kinds of stacks ( say a stack that contains integers and strings. Note that C++ now has a built in string class ).

Populate your stack class with some values by using push(). Print out the stack. Then perform some pop(), top() and push functions randomly, printing the stack after each such operation. Remember to take care of popping and empty stack. Your program should flag an error in such a case.

Also the size of the array should be entered via a constructor. A call to push() should check if the size of the stack is getting violated or not.

Also remember to write a destructor which will delete the objects if you are using dynamic arrays in the template class.