18+-+Linked+Lists - Linked Lists Introduction EECS 280...

Info icon This 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 circle5 Expandable arrays are only one way to implement storage that can grow and shrink over time. circle5 Another way is to use a "linked structure". circle5 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 circle5 Expandable arrays are only one way to implement storage that can grow and shrink over time. circle5 Another way is to use a "linked structure". circle5 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 circle5 Suppose we wanted to implement an abstract data type representing a mutable list of integers, represented as a linked structure. circle5 This ADT will be similar to the list_t type from project one, except that those lists were immutable: circle5 Once a list_t object was created, no operations on that list would ever change it. circle5 A valid list of integers is either: circle5 The empty list circle5 An integer followed by a valid list. Linked Lists Introduction 5 circle5 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 circle5 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 circle5 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
Image of page 1

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

View Full Document Right Arrow Icon
11/22/2009 2 Linked Lists Implementation 7 circle5 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; }; circle5 The invariants on these fields are: circle5 The value field holds the integer value of this element of the list.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern