Fundamentals of Computer Science II (CSC-152 2000S)


Class 38: Implementing Stacks and Queues

Back to Introduction to Linear Structures. On to Automated Problem Solving with Linear Structures.

Held Tuesday, April 11, 2000

Overview

Today we experiment with two implementations of stacks. Similar implementations will be appropriate for queues (although not for priority queues).

Notes

Contents

Summary


Preparation

In order to do this lab, you will need the following files.

  1. Create a new directory to use for the lab. You can use the file manager or the Unix mkdir command.
  2. Copy all of the files to that directory.
  3. Scan the files so that you can understand their purpose. It might help if you took notes on the purpose of each.

Preliminary Questions

You need not turn in answers to these questions. However, you should be prepared to answer them in class and on exams. Feel free to discuss the questions with your colleagues. We'll reflect on them after a few minutes of discussion, perhaps ten minutes after the start of lab.

Test Plan

Design a simple test plan that you might use to test an implementation of stacks. You might consider what the implementation should do when the same element is added multiple times, when the last element is deleted, and so on and so forth. Summarize your plan and the expected output.

We'll do this as a class.

Executing The Test Plan

  1. Compile the files.
  2. Run the tester (NBSTester). Does the NodeBasedStack class seem to work the way you expect?

Improving Output

It might be easier to test the class if you could see the stack being built. Write a toString method that generates a human-readable representation of the contents of the stack. Here's a possible strategy:

create tmp, a reference to the top of the stack
create a result string
while tmp does not refer to the top of the stack
  add the contents of tmp to the result string
  set tmp to the next element

Once you've written this method, make sure to test it (perhaps by running your test plan).

Updating Linear Structures

Note that this is a puzzle about object-oriented design, and not necessarily about stacks.

Both add and get in the Linear class have a precondition having to do with the number of elements in the structure. It might be helpful if the methods threw an exception in cases in which the structure has no elements (get) or no room (add).

  1. Update the method declaration in Linear.java so that it's clear that get throws an exception.
  2. Do you think you have to change any other classes? Why or why not?
  3. Try recompiling Linear.java, Stack.java, and StackTester.java. Does everything continue to work successfully?
  4. Update the method declaration in Linear.java so that it's clear that add throws an exception.
  5. Do you think you have to change any other classes? Why or why not?
  6. Try recompiling Linear.java, Stack.java, and StackTester.java. Does everything continue to work successfully?
  7. Update NodeBasedStack so that it throws exceptions in add and get.
  8. Do you expect it to compile? Why or why not?
  9. Update Stack so that it throws exceptions in get.

Preparing to Work With Array-Based Stacks

  1. Scan the code for ArrayBasedStack and ensure that it compiles.
  2. Build an ABSTester class that can be used to test array-based stacks. You may want to refer to NBSTester for assistance.
  3. Which stub methods work ``correctly''? Which do not?

Implementing Array-Based Stacks

  1. Implement the isEmpty method in ArrayBasedSTack.
  2. Implement the add method in ArrayBasedStack. This method should add the object at the end of the array and update the size.
  3. Test it. Note that you need not have get implemented to test the method because I've already implemented toString.
  4. Implement the peek method in ArrayBasedStack. This method should return the ``last'' element in the array.
  5. Implement the get method in ArrayBasedStack. This method should decrease the size of the stack and return the old last element.

History

Tuesday, 18 January 2000

Tuesday, 11 April 2000

Back to Introduction to Linear Structures. On to Automated Problem Solving with Linear Structures.


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/2000S/Outlines/outline.38.html

Source text last modified Tue Apr 11 09:39:25 2000.

This page generated on Tue Apr 11 09:41:18 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu