18+-+Linked+Lists - 11/22/2009 Linked Lists Introduction...

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

View Full Document Right Arrow Icon
11/22/2009 1 Linked Lists EECS 280 Programming and Introductory Data Structures 1 Linked Lists Introduction 2 c Expandable arrays are only one way to implement storage that can grow and shrink over time. c Another way is to use a "linked structure". c 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 c Expandable arrays are only one way to implement storage that can grow and shrink over time. c Another way is to use a "linked structure". c 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 c Suppose we wanted to implement an abstract data type representing a mutable list of integers, represented as a linked structure. c This ADT will be similar to the list_t type from project one, except that those lists were immutable: c Once a list_t object was created, no operations on that list would ever change it. c A valid list of integers is either: c The empty list c An integer followed by a valid list. Linked Lists Introduction 5 c 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 c 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 c 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/22/2009 2 Linked Lists Implementation 7 c 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; }; c The invariants on these fields are: c The value field holds the integer value of this element of the list. c 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

18+-+Linked+Lists - 11/22/2009 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