4. Linked Lists.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Double-Linked Lists and Circular Lists
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Week 4 - Monday CS221.
CSE 373 Implementing a Stack/Queue as a Linked List
Single-Linked Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Lecture 04: Sequences structures
Marcus Biel, Software Craftsman
3. Array-Lists.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Building Java Programs
UNIT-3 LINKED LIST.
Programming Abstractions
Java collections library
LinkedList Class.
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
List Data Structure.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Arrays and Collections
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Dynamic Data Structures and Generics
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
Collections Framework
Dynamic Data Structures and Generics
Linked Lists.
Linked Lists Chapter 5 (continued)
CSC 143 Java Linked Lists.
Recursive Objects Singly Linked Lists.
Building Java Programs
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists.
Lecture 7: Linked List Basics reading: 16.2
slides created by Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists.
Dynamic Array: Implementation of Stack
Linked Lists Chapter 5 (continued)
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

4. Linked Lists

Why they’re cool ArrayLists: LinkedLists (just the reverse) +: Infrastructure is already there (arrays) -: performance hit when capacity is reached (resize op) +: Lean memory usage: Array itself: 4/8 bytes per slot (a reference [32-bit = 4bytes, 64-bit = 8 bytes]) The actual data we’re storing Other items: A used-count (4 bytes) (static) capacity-increase amount (4 bytes) -: Require contiguous memory for the array. LinkedLists (just the reverse) We’ll explore the memory usage shortly.

Review: Nested Classes public class Outer { protected class Inner } Inner mSampleInst; A class within a class! We actually saw it with ArrayList.ArrayListItearator Why? Data Hiding (note the protected) The user of Outer need not even know about Inner! Encapsulation Data Hiding. Also implies we only need worry about it in Outer. Most LinkedList implementations have a hidden Node class. Contains the data… …and references to the neighboring Nodes.

Code View: Basic Structure class LinkedList<E> { protected class Node E mData; // Note how the generic was “inherited” Node mNext;// Reference to the following node // (or null if there is none) Node mPrev;// Similar for preceding. This makes // our Llist a doubly-linked list. } protected Node mStart; // Sometimes called the head protected Node mEnd; // Sometimes called the tail protected int mSize; // Number of nodes public void add(E new_val) { … } public void remove(E val) { … } // additional top-level methods. LinkedList<String> LL = new LinkedList<String>(); LL.addLast(“Sue”); LL.addLast(“Bob”); LL.addLast(“Tom”);

Logical View Empty List List with 3 elements mStart: (null) mEnd: mSize: 0 mStart: mEnd: mSize: 3 mData: “Bob” mNext: mPrev: mData: “Sue” mData: “Tom”

Memory View Stack Heap “Sue”’s node “Tom”’s node “Bob”’s node addr var contents Notes LL 40000 (in main program) … 50000 mStart field of list pointed to by LL 40004 55000 mEnd field of llist pointed to by LL 40008 3 mSize field llist pointed to by LL 72000 mData field 50004 60000 mNext field 50008 0 (null) mPrev field 71000 55004 55008 70000 60004 60008 “Bob” mData of Bob-node “Tom” mData of Tom-node “Sue” mData of Sue-node Memory View Stack Heap “Sue”’s node “Tom”’s node “Bob”’s node Note how the nodes are not in contiguous order (nor in the order they were allocated)! Also note the mData field is also a reference to data elsewhere on the heap!

Iteration Not an iterator (yet) A way to “visit” each node in a linked-list Used in most operations (internally!) Forward-iteration Backwards-iteration (doubly-linked lists only) You figure it out! Node cur = mStart; while (cur != null) { // Do something to cur.mData // (and / or mNext, mPrev) // Advance iteration cur = cur.mNext; }

Major methods java.util.linkedList void addFirst(E val); // (to end) void addLast(E val); void add(int index, E val); // insert at that pos. // 0: same as addFirst // size: same as addLast void clear(); int indexOf(E val); // -1 if not found. E get(int index); void remove(int index); // make both get and remove use a protected // method called getNodeByPosition(int index). int size(); Object[] toArray(); String toString(); // I want “<[first][second]…>” // or “<empty>” Iterator<E> iterator();

Freebie: Add (at index) void add(int index, E val); First: determine all cases case1: list is empty => call addFirst case2: list is non-empty and index = 0 => call addFirst case3: list is non-empty and index = size => call addLast case4: list is non-empty and 0 < index < size => work! case5: invalid index – raise an Exception Algorithm (for case4): Allocate a new node (disconnected) Iterate until cur is equal to index-1 Re-route references (see logical view, next slide) Add one to size Victory!

Add @ Index, cont. Logical view new_node: mData: <whatev> mNext: mPrev: … cur: mData: <whatev> mNext: mPrev: after traversal: cur is at index

code Implementation // Note: this is in the LinkedList class (but outside // the nested Node class) void add(int index, E val) throws IndexOutOfBoundesException { if (position < 0 || position > mSize) // Covers case 5 -- fast-fail! throw new IndexOutOfBoundsException("Invalid index: " + position); if (position == 0 || mSize == 0) addFirst(value); // Covers case 1 & 2 else if (position == mSize) addLast(value); // Covers case 3 else // Everything else falls under case 4 -- we're somewhere in the // middle (continued on next slide)

code Implementation, cont. // ... Create the new node (this is done in addFirst and addLast too... Node node_node = new Node(value); // ... traverse to the correct spot (so cur is at desired location) Node cur = mStart; int curPos = 0; while (curPos != position && cur != null) { cur = cur.mNext; curPos++; } // ... re-route the pointers node_node.mNext = cur; node_node.mPrev = cur.mPrev; cur.mPrev.mNext = node_node; cur.mPrev = node_node; // We now have one more node. Note: I put it in the else b/c // addFirst and addLast do it themselves – we don't want to do it twice! mSize++; } // end of else from last slide } // end of add function body

Lab 4 Make test program together You finish implementing the methods Like we did in Lab3 Ask questions – I’m more forthcoming in labs like this!