CS 12: Programming Assignment 4

Directory to turnin: as4

Due date: Thursday Nov 2, 2000

Synopsis:

A stack is a data structure in which elements are "popped" off in the reverse order in which they are "pushed" on. (Think of a stack of plates at a cafeteria; you always take plates from the top of the stack, in the reverse order in which they were stacked.)

For example, if the elements 5, 10, 18, and 22 are pushed onto a stack, doing a pop will return 22; doing another pop will return 18. If 50 is then pushed onto the stack, doing another pop will return 50.

You will write a class that implements a stack, using an array.

Details:

This program should consist of 3 source files:

stack.h:

This file contains the definition for the Stack class. Click here to get it. This is all stack.h will contain.

stack.cpp:

This file will contain the implementation of the Stack class.

Stack()
The constructor should simply initialize the currentSize member variable to 0.
push()
This method takes an integer value to push onto the stack as a parameter. The method should first check to see if the array is full (i.e. if currentSize is equal to MAX_SIZE). If the array is full, the method should do nothing except return false.

If the array is not full, the value should be added to the array, increment currentSize, and return true.

pop()
This method takes a reference to an integer as a parameter. The method should first check to see if the array is empty (i.e. if currentSize is equal to 0). If the array is empty, the method should do nothing except return false.

If the array is not empty, it should copy the last integer added to the array (i.e. the integer at index currentSize - 1) to the parameter, decrement currentSize, and return true.

currentSize()
This method should simply return the current size of the stack (i.e. the value of currentSize).
dump()
This method should print all the integers on the stack, starting from the top. Each integer should be separated by a space and a return should be printed after every 10'th integer.

main.cpp:

This file should contain your main function. It should declare a variable of type Stack and use it to perform operations.

A menu of options should be presented to the user:

      P) Push to stack
      O) pOp from stack
      S) Size of stack
      D) Dump stack
      Q) Quit

      Select option:
Depending on which option the user chooses, the appropriate Stack member function should be called and the appropriate result displayed to the user. For example, for the push option, the user should be asked for a value to push and then the push() method should be called. If push() returns true, the user should be informed that the value was added to the stack; if false is returned, the user should be informed that the stack is full.

Put the menu in a loop so that after performing each operation, the menu is shown again, until the user chooses to quit.