next up previous
Up: aps105F Home

APS 105S - Computer Fundamentals

Assignment #7:  Pointers

Fall 1996

(To be completed on Nov. 14/15)

 

Objective:   To gain practice in working with pointers.

1 A Train-Modeling Program: Turing

You are required to complete a skeletal program provided for you in the file /usr/copy/aps105/a7.t

This program maintains a list representing rail cars making up a train. Cars can be inserted or deleted from the train. Cars are identified by a number, which determines their order in the train, and also by a type (e.g. engine, tank, gondola, boxcar). The program allows the user to count the number of cars of a given type, and to determine the neighbors of any given car.

The program at first glance may seem daunting. It is 178 lines long, which is not a very big program. Most of this code can safely be ignored by you, except that there is always a benefit to (mainly new) programmers when they try to read and understand existing code. You need only add code to complete the body of three modules: the function lookupCar, the function carCount, and the procedure neighbors. Each needs no more than 10 lines of code added to it. You can add the necessary code to complete each module in turn, testing these modules one at a time as you finish them.

2 Optional Section -Multiple Trains And Merging: Turing

Modify the program from the previous section so that it could maintain several trains in multiple linked lists. The following changes will have to be made to the program:

  1. Instead of a single head train pointer variable, declare an array of train pointer variables each of which points to the beginning of a unique train.
  2. For the modules showTrain, insert, delete, lookupCar, carCount, and neighbors, the appropriate head pointer will have to be passed as a module argument.
  3. The main program will have to prompt the user for the train number each time a selection is made from the main menu.
  4. At the end of each action, display all the trains in memory.

As an additional exercise, construct a module that will merge two trains together. This module will accept two train pointers as parameters. These will be pointing to the beginning of the two trains that are to be merged. The module is to construct one linked list structure from the two trains supplied, keeping the cars in numerical order. At the end of the module, have the first of the two pointers passed point to the final train, and set the second pointer to nil.


next up previous
Up: aps105F Home

Paul Chow
Mon Nov 4 08:22:00 EST 1996