Stack Demonstration

Dusty Phillips
Athabasca University
May 19, 2004
Last updated Jun 3, 2004

This applet visually demonstrates the use of Stacks in programming. View the applet in the Applet section below. The usage of the applet is described in the following Usage section. For those interested, the Display and design of the applet are also described.

Applet

Usage

This applet visually demonstrates the programming behind an array-based stack. It assumes that you have some basic knowledge of stacks. The purpose is to solidify and expand this knowledge.

There are essentially two operations that can be performed on a stack. The first is to Push an object onto the stack, the second is to Pop an object from the stack. (In some cases, you can also Peek at the top object's value, but this is not modelled here as it is just a constrained form of Poping and the stack can be visualized at all times anyway.)

To begin with, select one of the available buttons ( Push or Pop). Doing so will display the pseudocode method related to the specified procedure. At this point, the Push and Pop buttons will become unavailable until you finish stepping through the code or reset the applet. Since Pushing requires an object parameter, you will be prompted to enter a string to use as the parameter upon entering the method.

After you have started stepping through the Pseudocode, the Step and Autostep buttons become available. These allow you to step one line at a time, or to have the computer do it automatically on a half-second intervals. As the application steps through the code, the visual display will be updated to show the new contents of the array.

Once the method has completed, you will once again be presented with the options to Push and Pop the stack.

Display

The display is divided up into input, pseudocode, and output areas. The input area has buttons for Pushing and Poping the stack, as well as buttons to Step, and Autostep through the Pseudocode for these functions.

Below this to the left is a panel with pseudocode signatures for a class based on a stack. Below this panel is another panel where the pseudocode methods will be executed to demonstrate how the code works. To the right is an animated display of the stack array.

The animated display is somewhat complex. When an item is pushed onto the stack, the next empty slot is highlighted. Then as the code is stepped through, the slot is filled by a box falling into it. When an item is popped, the level of the next variable is highlighted. When this is decremented, the next one down is highlighted. Then when the item is actually removed, it bounces off the stack.

Design

The design for this stack demonstration is somewhat complex. A StructuredPanel will contain the various areas of the system. The InputPanel will have two buttons added to it for the Push and Pop commands. Each of these will be associated with a separate class derived from Stepper. When either button is clicked, both will become unavailable, and the Stepper will be associated with the StepPanel. The StepPanel will take care of enabling the step buttons; stepping will be nearly autonomous.

The applet will implement StepListener to listen to the relevant Steppers. When they have completed, it will remove the Stepper from the StepPanel, which will disable the buttons for starting/stepping through the Pseudocode, will remove the Pseudocode function object and will reenable the Push and Pop buttons.

The Steppers associated with Pushing and Popping will have a reference to a custom output panel that will take care of animating the stack procedures.

The main Pseudocode for this demonstration will look like this: class Stack { int maxsize = 6 Object[] stack = new Object[size] int next = 0 public void push(Object obj); public Object pop(); }

The PseudocodeChild for pushing will look like this: public void push(Object obj) { if (next == maxsize) { throw FullStackException } stack[next] = obj next++ }

Finally, the PseudocodeChild for popping will look like this: public Object pop() { if (next == 0) throw EmptyStackException } next-- return stack[next] }