Lecture #17 (19 March 2002)

Data Structures, Linked Lists

Overall Reading
Brookshear: p. 321, Ch. 7.1-7.2
(skip second half of p. 326 and all of p. 331)

Outline:

  • Overview (p. 321 [Br])

  • Arrays (Ch. 7.1 [Br])
  • One-dimensional arrays
  • Two-dimensional arrays
  • Lists (Ch. 7.2  skip second half of p. 326 and all of p. 331[Br])
  • Pointers
  • Linked Lists

  • Overview (p. 321 [Br])

    Recall that main memory is organized as individual cells with consecutive addresses. However in is often convenient to associate larger data structures among these cells. Higher-level programming languages will often support such views of data structures.

    Arrays (Ch. 7.1 [Br])

  • One-dimensional arrays
    Imagine that we want a program which gathers a series of numbers for processing, perhaps from a scientific experiment. We could think about each cell of memory separately but better to think about them as a group or "array" of cells.

    For example, a program may refer to the array as Readings and then individual cells of the array as Readings[1], Readings[2] and so on.

    Converting this conceptual structure to the machine's memory is straightforward.

  • Two-dimensional arrays
    Often, spreadsheets keep information in a two-dimensional table, such as the following:

    10 28 13 22
    40 38 11 14
    33 18 25 19

    Though conceptually, this information is two-dimensional, we can store it using a one-dimensional piece of memory.

    row major order - Store all entries of the first row, followed by entries in the second row, followed by the third row, etc. On the above example,

    10 28 13 22 40 38 11 14 33 18 25 19

    Based on the number of rows and columns of the table, it is easy to calculate where an entry in the two-dimensional table will be stored in the one-dimensional array.

  • If our table has c columns, then we know that the entry in the
    ith row and jth column will be stored in the
    c*(i-1)+jth location of the one-dimensional array.
  • Fortunately, a high-level programming language can take care of this conversion, allowing programmers to think about their information as a two-dimensional array, using notation such as Readings[2][4].

    Notes:

  • With additional memory, it is easy to add additional rows at the bottom of a table in row major order. However, there is no convenient way to add or remove columns using this representation.

  • you can also use column major order in simulating a two-dimensional array.

    10 40 33 28 38 18 13 11 25 22 14 19

  • You can even represent higher-dimensional tables using similar techniques.

  • Lists (Ch. 7.2 , skip second half of p. 326 and all of p. 331 [Br])

    The biggest problem with the use of arrays is that you need to essentially decide on the size of the complete array before you start using it. This is necessary so that you can reserve the necessary number of contiguous memory locations.

    If you are in a setting where you do not have advanced knowledge of the amount of data you will be storing, one of two things might happen.

  • You may have reserved too large of an original array, thereby wasting a lot of unused memory.
  • Worse yet, you may have reserved too small of an original array, which does not have sufficient room for all of the data.
  • Therefore, a goal will be to develop data structures which can grow and shrink as necessary, so that the memory usage is proportional to the true number of items currently being stored.
  • Pointers (a.k.a. references)
  • Telephone numbers
  • Postal addresses tell where mail should be delivered
    (and mail forwarding can be used if you move)
  • URL's on the Web link you to new pages.
  • CPU instructions (Program Counter, JMP instructions)
  • Linked List
    Have an individual block of memory which stores a piece of data, as well as a pointer to the place where the next piece of data can be found.
  • Make sure you keep a reference to the first item of the list (the "head")
  • The last item on the list will have to have a way to express that it is indeed the last item (a "NIL" pointer).
  • Let's look at an example, such as that of Figure 7.5 (p. 329 of [Br]), however looking at how it might be laid out in memory. For this example, we are told that the "head" of the list begins at cell 11. By conventions, we will designate the end of the list with a pointer value of zero.

    Memory Contents
    (in decimal)
    Cell 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Value Z 15 X 11 C 13 V 1 B 0 N 5 M 7 L 9

    We can walk through the list, reading the data as follows:
    N -> C -> M -> V -> Z -> L -> B
    Note: The information currently stored in Cells 3 and 4 is not part of this list.

  • Deleting an existing item from a list: Figure 7.6 (p. 330 of [Br])
    If we want to delete the entry storing character "V" from the list, we simply change one pointer. Specifically, the pointer that used to point to V should be updated so that it points to the entry following the deleted item. In this example, Cell 14 originally contained a pointer to Cell 7, but should be updated to point to Cell 1. The resulting list is represented as:

    Memory Contents
    (in decimal)
    Cell 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Value Z 15 X 11 C 13 V 1 B 0 N 5 M 1 L 9
    Note: the information in Cells 7 and 8 is no longer part of the list. We might choose to reset the values of those cells accordingly, though it is not necessary.

  • Adding a new item into a list: Figure 7.7 (p. 330 of [Br])
    A new item can be placed at any given point in the linked list. The information can be stored in any two memory cells which are currently available. What remains is to update one pointer to point to the newly inserted item, and to adjust the new pointer to point to the item which should follow it.

    In our example, lets add "K" to the list so that it is placed between L and B. For storage, we will note that Cells 3 and 4 are available for use. The two pointers we update are as follows. Cell 4 is set to value "9" since the new item "K" will be followed by "B" which is stored in Cell 15. Also, Cell 16 will be set to value "3" so that the entry L in the list is now followed by the new entry "K". The updated list is represented as follows:

    Memory Contents
    (in decimal)
    Cell 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Value Z 15 K 9 C 13 V 1 B 0 N 5 M 1 L 3

  • A Linked List demo.


    comp150 Class Page
    mhg@cs.luc.edu
    Last modified: 19 March 2002