Algorithms and OOD (CSC 207 2014S) : Assignments

# Assignment 6: Linear Structures

This assignment is currently in draft form.

Due: 10:30 p.m., Wednesday, 9 April 2014

Summary: In this assignment, you will experiment with recent data structures and algorithms that we have been exploring.

Purposes: To give you more experience thinking about the implementation and use of linear structures. To give you experience writing interactive programs.

Collaboration: I encourage you to work in groups of size two to four, although you may work alone. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.

Submitting: Please put all of your work in a GitHub repository named `csc207-hw6`. Email your grader the address of that repository. Please title your email “CSC207 2014S Assignment 6 (Your Names)”.

Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

## Assignment

### Problem 1: Improving Array-Based Queues

The standard array-based implementation of queues uses two pointers: one to keep track of where to put the next value (we'll call that `back`) and one to keep track of where to get the least recently added value (we'll call that `front`). For example, after adding the strings `"alpha"`, `"beta"`, and `"gamma"` to an array-based queue, we might end up with the following.

```front            back
|               |
|       +-------+
|       |
v       v
+---+---+---+---+---+
| * | * | * | / | / |
+-|-+-|-+-|-+---+---+
|   |   +---------------------------+
|   |                               |
|   +--------------+                |
|                  |                |
v                  v                v
a l p h a \0 . . . b e t a \0 . . . g a m m a \0 . . .
```

That's a bit cumbersome to draw, so we'll use the usual shorthand of putting the values “in” the array.

```front            back
|               |
|               |
|               |
v               v
+-------+-------+-------+-------+-------+
| alpha | beta  | gamma |       |       |
+-------+-------+-------+-------+-------+
```

After we add and remove a few elements (say we add another gamma and an alpha, and then remove the initial alpha and beta), we'll have “empty space” at the beginning of the array

```front            back
|               +---------------+
|                               |
+---------------+               |
v               v
+-------+-------+-------+-------+-------+
|       |       | gamma | gamma | alpha |
+-------+-------+-------+-------+-------+
```

What happens if we add another element, say "delta"? In the naive implementation of array-based queues, we've run out of space. But we should be able to wrap around to the front of the array.

```front            back
|  +------------+
|  |
+--|------------+
v--+            v
+-------+-------+-------+-------+-------+
| delta |       | gamma | gamma | alpha |
+-------+-------+-------+-------+-------+
```

Implement and test array-based queues with wrapping. You may find it useful to start with the `ArrayBasedQueue` class from the Tao of Java's linear structures repository.

### Problem 2. Matching Parentheses and Other Symbols

Write a procedure that takes a string as input and prints out a picture that shows the nesting. For each pair of matched symbols, you should draw a line underneath, as in the following picture.

```{oh [boy] (I am having) (<so> much) fun matching `symbols'}
[---]
(-----------)
<-->
(---------)
`-------'
{---------------------------------------------------------}
```

You should support parentheses, square brackets, curley braces, angle brackets, and matched single quotation marks. You may also choose to support double-quotation marks, although they can be a bit troublesome.

If the symbols do not match, you can do whatever you think is appropriate to indicate the failure to match. For examlpe

```(Hello (world)
(-----)
( < - UNMATCHED
```
```(Hello (world))}  ]
(-----)
(-------------)
} <- UNMATCHED
] <- UNMATCHED
```

To solve this problem, you will need to store the indices of the matching symbols on the stack. That is, you will need to push both symbol and index. How can you store two kinds of values in stack? One option is to make it a stack of general Objects, and alternately push Character and Integer objects. Another option is to create a simple class that groups a character and an integer.

### Problem 3. A Reverse Polish Notation Calculator

Implement a calculator that uses Reverse Polish Notation (RPN). You should support input of real numbers and the following operations:

• +, -, *, / - the standard arithmetic operators;
• p - print the top value on the stack;
• s - print the whole stack;
• c - clear the stack; and
• one other interesting operation of your choice.

Although it doesn't have exactly the same interface, you can experiment with the command-line RPN calculator `dc` to help understand these operations. (I don't think `dc` provides `s`.)

In implementing your calculator, do your best to use the model-view-controller (MVC) pattern. You may need to Google that pattern.

## Citations

I've assigned problem one before, but I don't recall when.

Problem two is adapted from the extra problems in the lab on stacks. I've modified the text slightly in combining the problems.

Problem three comes from an assignment from a previous semester. I've taken the text mostly verbatim.

Copyright (c) 2013-14 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit `http://creativecommons.org/licenses/by/3.0/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.