next up previous
Next: A Listing for revstring.t Up: APS105F Thursday Quiz 2 Previous: 2 A Simple Spread

3 Towards A Better Spread Sheet

As those of you who have used real spread sheet calculators will know, each element or cell in the spread sheet can hold an equation, a number, or some text. It is also possible to insert and delete rows and columns. This means that we will need to use records and some sort of data structure. A simple start is to have each row be a doubly-linked list of cells.

What to do

Copy the file ``/usr/copy/aps105/linkeda.t'' into your working directory and rename it ``part3.t''. A listing of this file is given in Appendix C. It is unlikely that you will need to understand all of it. Just realize that you are dealing with a simple doubly-linked list. If you do have problems, they will show up in the PrintRow procedure, which is described in more detail below.

Your task is to complete the InsertCell procedure.

Assumptions

Inputs are always in the correct format and sequence.

Restrictions:

You should only write the code for the procedure. Do not change any other part of the program when you submit your solution, though you are free to make modifications while testing. Just make sure that the program you submit has the original code.

More Information

Each cell has the following fields:

        Field                  Use
        -------------------------------------------------
        Left            Pointer to the cell to the left
        Right           Pointer to the cell to the right
        ColumnNum       An int indicating the column that the cell is in
        Value           A real number holding the value of the cell
        HasValue        A boolean that is true when there is a number in Value

The use of the ColumnNum field means that it is not necessary to include a cell in the list if there is nothing in the cell for the corresponding column. The HasValue field is used to indicate whether the Value field has been assigned anything.

The PrintRow procedure prints the row both in the forward and reverse directions to check that all the links are correct. Note that the procedure assumes that the Right link of the cell at the right end of the row and the Left link of the left-end cell are set to nil.

Example Session

An example session is shown below. Note that the program first inputs a number of cells. At this point, it is assumed that the column numbers are entered in increasing order. A ``y'' response to the ``Value?'' prompt is asking whether the cell is to store a value. If you respond with ``y'', then you will be asked for the value. This first part allows for building a list quickly that you can then work on.

You are then asked whether you wish to insert a cell or not. After each insertion, the list is printed again. Remember to check all of the cases!

Enter a bunch of cells, exit with a Column Number < 1
Column? 3
Value? (y/n) n
Column? 9
Value? (y/n) y
Enter value 18
Column? -1

[(3) No Value] --> [(9) 18]

[(9) 18] --> [(3) No Value]

Insert a cell? (y/n) y
Column? 2
Value? (y/n) n

[(2) No Value] --> [(3) No Value] --> [(9) 18]

[(9) 18] --> [(3) No Value] --> [(2) No Value]

Insert a cell? (y/n) n

A Solution

Go here to see a solution. Note that the main program is a bit different. It only uses InsertCell to build the list.


next up previous
Next: A Listing for revstring.t Up: APS105F Thursday Quiz 2 Previous: 2 A Simple Spread

Paul Chow
Sun Nov 24 15:34:31 EST 1996