20+-+Linked+Lists - 11/16/10 Linked Lists Introduction...

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

View Full Document Right Arrow Icon
11/16/10 1 Linked Lists EECS 280 Programming and Introductory Data Structures 1 Linked Lists Introduction 2 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 Linked Lists Introduction 3 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. Linked Lists Introduction 4 Suppose we wanted to implement an abstract data type representing a mutable list of integers, represented as a linked structure. This ADT will be 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 An integer followed by a valid list. Linked Lists Introduction 5 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 Linked Lists Introduction 6 For example, if the list is (1 2 3), and you remove() , the list will be changed to (2 3), and remove returns 1. int remove(); // MODIFIES: this // EFFECTS: if list is empty, throw listIsEmpty. // Otherwise, remove and return the // first element of the list If you then insert(4) , the list changes to (4 2 3). void insert(int v); // MODIFIES: this // EFFECTS: inserts v into the front of the list
Background image of page 1

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

View Full DocumentRight Arrow Icon
11/16/10 2 Linked Lists Implementation 7 To implement this, we need to pick a concrete representation that stores a list as a dynamically created list of "nodes". 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.
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.

Page1 / 6

20+-+Linked+Lists - 11/16/10 Linked Lists Introduction...

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