19-lists.student - Last time Dynamic storage recap...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Last time: * Dynamic storage recap * Expandable arrays * Asymptotic analysis Today: +++++++++++++++++++++++++++++++++++++++++++++++++++++ Expandable arrays are only one way to implement storage that can grow and shrink over time. Another way is to use a "linked structure". A linked structure is one with a series of zero or more data containers, connected by pointers from one to another, like so: +---+ +---+ +---+ +---+ list-----> | -----> | -----> | -----> | -----\ +---+ +---+ +---+ +---+ \ --- - A linked structure is sort of like a freight train. If you need to carry more freight, you get a new boxcar, connect it to the train, and fill it. When you don't need it any more, you can remove that boxcar from the train. Suppose we wanted to implement an abstract data type representing a mutable list of integers, represented as a linked structure. This ADT is similar to the list_t type from project one, except that those lists were immutable: once a list_t object was created, no operations on that list would ever change it. A valid list of integers is either: the empty list, or an integer followed by a valid list. There are three operations that the list must support: void insert(int v); // MODIFIES: this // EFFECTS: inserts v into the front of the list bool isEmpty(); // EFFECTS: returns true if list is empty, false otherwise class listIsEmpty {}; // An exception class int remove(); // MODIFIES: this // EFFECTS: if list is empty, throw listIsEmpty. // Otherwise, remove and return the first element of the list For example, if the list is (1 2 3), and you remove(), the list will
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
be changed to (2 3), and remove returns 1. If you then insert(4), the list changes to (4 2 3). To implement this, we need to pick a concrete representation. We'll store a list as a dynamically created list of "nodes". A node looks like this: struct node { node *next; int value; }; The invariants on these fields are: The value field holds the integer value of this element of the list. The "next" field points to the next node in the list, or NULL if no such node exists. NULL is the universal pointer value that means "points at nothing". The value of the NULL pointer is "0", and it is written as: | | ----- --- - Or the symbol for "ground" in a circuit. The basic idea is that each time an int is inserted into the list,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

19-lists.student - Last time Dynamic storage recap...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online