Assignment #7: Pointers
Fall 1996
(To be completed on Nov. 14/15)
Objective: To gain practice in working with pointers.
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.
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:
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.