Programming Project #1     COSC 311     WI2013

Paren matching
Distributed: 10 January 2013       Due: 22 January 2013 29 January 2013

The algorithm

A stack is a useful data structure for checking for properly nested grouping symbols: (), [], {}, <>.

The algorithm, using a stack, to check for matched grouping symbols works as follows:

 
  while (more to do) {
  	get ch;
  	if (ch is not grouping symbol) break;
  	
  	if (ch is in left group) push it to stack;
  	
  	if (ch is in right group) {
  		if (stack is empty) exit with error notification;
  		
  		left = pop stack;
  		
  		if (left and ch do not match up) exit with error notification;
  		}
  	}				// end of while
  	
  if (stack is not empty) exit with error notification;
   

When checking for matched grouping symbols, there are four possible conclusions:

  1. symbols match
  2. symbols do not match up
    1. because of attempt to pop empty stack: unmatched right symbol.
    2. because left and right do not match: error is "invalid grouping"
    3. because stack is not empty when input is exhausted: error is "unmatched left symbol"

The problem

Write two separate programs:

You must implement your own stack, including push(), pop(), and isEmpty(). The stack is implemented using a linked list (not a 1D array). You must handle popping an empty stack with grace.

IMPORTANT! You may NOT use the Java Stack class that is a part of Java 1.6 (in java.util.Stack) EXCEPT for development and debugging purposes.. You certainly can use the specification for the Java Stack in order to write your own Stack class.


Development advice

Expected line counts (not including comments and white space):
Program #1: 100 - 250
Program #2: 300 - 400
  1. Write program #1 and test thoroughly. You will need input files of each type of result. Keep these input files fairly short so that it is easy to confirm that your program is working correctly.
  2. Write program #2 -- test it using program #1. Start with a small number of groups.
Warning: Your program #2 should be able to generate any random file, given the constraints supplied by the user.

Deliverables


Grading

Code that does not compile is not accepted.

Cannibal?

You can use code published elsewhere. Do NOT use code from another individual in this class. Supply the source of your code in the comments at the beginning.