next up previous
Next: 2 A Graphical Animation Up: APS105S Monday Quiz 2 Previous: Instructions

1 Linked List Manipulation

The program listed below reads, from a file, records describing the name and height of a person. It stores this data in a singly-linked list of records.

It is meant to be part of an interactive database program, in which the user types commands to change the list in some way. One of the functions that the user can request is to move all records with height greater than some value, V, to the front of the linked list. That is, all records that have a person with tex2html_wrap_inline103 should be moved to come before all of the other records in the list. The records that are moved do not otherwise need to be in any particular order.

You are to complete this program by writing the code for the procedure MoveFront, which should implement the move-to-front function described above. MoveFront is called in the main program below.

The other commands, already implemented in the code below, are to display the list (P) and to quit the program (Q).

You should copy the code from the file /usr/copy/aps105/move.t into your file ``part1.t'' and modify it to add the procedure MoveFront. Do not change any of the code from move.t.

Notes

Here is the code from /usr/copy/aps105/move.t:

type LL:
    record
       Name: string
       Height: real
       Link: pointer to LL
    end record

var Head: pointer to LL

procedure GetData(FileName: string)
    var FileNo: int
    var NewRec, Previous: pointer to LL

    open:  FileNo, FileName, get

    Previous := nil
    Head := nil

    loop
        exit when eof(FileNo)
        new NewRec
        get: FileNo, NewRec->Name, NewRec->Height 

        NewRec->Link := nil
        if Previous = nil then
            Head := NewRec
        else
            Previous->Link := NewRec
        end if

        Previous := NewRec
    end loop
end GetData

procedure PrintList
    var Current: pointer to LL

    Current := Head
    loop
        exit when Current = nil
        put Current->Name, " ", Current->Height
        Current := Current->Link
    end loop
end PrintList


% MAIN

var FName: string
var Command: string
var MoveHeight: real

% Get the file name containing linked list data from user
put "Enter File Name: " ..
get FName

% Read Data and create Linked List
GetData(FName)

% Loop to Read Command from user

put "Commands are P (display list) M (Move To Front) and Q (quit) "
loop
    put "Command: " ..
    get Command

    if Command = "P" or Command = "p" then
        PrintList
    elsif Command = "M" or Command = "m" then
       put "Height of Records to be moved to front: " ..
       get MoveHeight

       MoveFront(MoveHeight)

    elsif Command = "Q" or Command = "q" then
       exit
    else
       put "Unknown Command, ", Command
    end if
end loop

A solution



Jonathan Rose
Fri Apr 4 13:36:24 EST 1997