Topic 11 Linked Lists -Joel Spolsky

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
Linear Lists – Linked List Representation
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
M180: Data Structures & Algorithms in Java
Topic 11 Linked Lists -Joel Spolsky
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Summary of lectures (1 to 11)
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Chapter 3: Arrays, Linked Lists, and Recursion
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Lecture 6 of Computer Science II
Elementary Data Structures
Unit – I Lists.
Review Array Array Elements Accessing array elements
COSC160: Data Structures Linked Lists
Chapter 4 Linked Structures.
Data Structure By Amee Trivedi.
Program based on queue & their operations for an application
UNIT – I Linked Lists.
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Big-O notation Linked lists
CS2006- Data Structures I Chapter 5 Linked Lists I.
John Hurley Cal State LA
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Chapter 12: Data Structures
Revision Based on: Linked Lists Revision Based on:
Queues Queues Queues.
Programming Abstractions
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCI 3333 Data Structures Linked Lists.
LINKED LISTS CSCD Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Arrays and Linked Lists
Chapter 13 Collections.
Lecture 20 Linked Lists Richard Gesick.
Linked Lists.
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Chapter 17: Linked Lists.
Lecture 14 Linked Lists CSE /26/2018.
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
CS210- Lecture 6 Jun 13, 2005 Announcements
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
BY PROF. IRSHAD AHMAD LONE.
Topic 11 Linked Lists -Joel Spolsky
LINEAR DATA STRUCTURES
Problem Understanding
Presentation transcript:

Topic 11 Linked Lists -Joel Spolsky "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101, a data structures course, and when they hit the pointers business their brains would just totally explode, and the next thing you knew, they were majoring in Political Science because law school seemed like a better idea." -Joel Spolsky Thanks to Don Slater of CMU for use of his slides.

Clicker Question 1 What is output by the following code? ArrayList<Integer> a1 = new ArrayList<Integer>(); ArrayList<Integer> a2 = new ArrayList<Integer>(); a1.add(12); a2.add(12); System.out.println( a1 == a2 ); No output due to syntax error No output due to runtime error false true CS314 Linked Lists

Dynamic Data Structures They grow and shrink one element at a time, normally without some of the inefficiencies of arrays as opposed to a static container such as an array Big O of Array Manipulations Access the kth element Add or delete an element in the middle of the array while maintaining relative order adding element at the end of array? space avail? no space avail? add element at beginning of an array CS314 Linked Lists

Object References Recall that an object reference is a variable that stores the address of an object A reference can also be called a pointer They are often depicted graphically: student John Smith 40725 3.57 CS314 Linked Lists

References as Links Object references can be used to create links between objects Suppose a Student class contained a reference to another Student object John Smith 40725 3.57 Jane Jones 58821 3.72 CS314 Linked Lists

References as Links References can be used to create a variety of linked structures, such as a linked list: studentList CS314 Linked Lists

Linked Lists A linear collection of self-referential objects, called nodes, connected by other links linear: for every node in the list, there is one and only one node that precedes it (except for possibly the first node, which may have no predecessor,) and there is one and only one node that succeeds it, (except for possibly the last node, which may have no successor) self-referential: a node that has the ability to refer to another node of the same type, or even to refer to itself node: contains data of any type, including a reference to another node of the same data type, or to nodes of different data types Usually a list will have a beginning and an end; the first element in the list is accessed by a reference to that class, and the last node in the list will have a reference that is set to null CS314 Linked Lists

Advantages of linked lists Linked lists are dynamic, they can grow or shrink as necessary Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled from any physical ordering in memory CS314 Linked Lists

Nodes and Lists A different way of implementing a list Each element of a Linked List is a separate Node object. Each Node tracks a single piece of data plus a reference (pointer) to the next Create a new Node very time we add something to the List Remove nodes when item removed from list and allow garbage collector to reclaim that memory CS314 Linked Lists

A Node Class public class Node<E> { private E myData; private Node<E> myNext; public Node() { myData = null; myNext = null; } public Node(E data, Node<E> next) { myData = data; myNext = next; } public E getData() { return myData; } public Node<E> getNext() { return myNext; } public void setData(E data) { myData = data; } public void setNext(Node<E> next) { myNext = next; } } CS314 Linked Lists

One Implementation of a Linked List The Nodes show on the previous slide are singly linked a node refers only to the next node in the structure it is also possible to have doubly linked nodes. The node has a reference to the next node in the structure and the previous node in the structure as well How is the end of the list indicated myNext = null for last node a separate dummy node class / object CS314 Linked Lists

A Linked List Implementation public class LinkedList<E> implements IList<E> private Node<E> head; private Node<E> tail; private int size; public LinkedList(){ head = null; tail = null; size = 0; } LinkedList<String> list = new LinkedList<String>(); LinkedList myHead iMySize myTail null null CS314 Linked Lists

Writing Methods When trying to code methods for Linked Lists draw pictures! If you don't draw pictures of what you are trying to do it is very easy to make mistakes! CS314 Linked Lists

add method add to the end of list special case if empty steps on following slides public void add(E obj) CS314 Linked Lists

Add Element - List Empty (Before) head tail size null null Object item CS314 Linked Lists

Add Element - List Empty (After) head tail size 1 Node myData myNext null String CS314 Linked Lists

Add Element - List Not Empty (Before) head tail size 1 Node myData myNext null String String item CS314 Linked Lists

Add Element - List Not Empty (After) head tail size 2 Node myData myNext Node myData myNext null String String CS314 Linked Lists

Code for default add public void add(E obj) CS314 Linked Lists

Clicker Question 2 What is the worst case Big O for adding to the end of an array based list and a linked list? The lists already contain N items. Array based Linked O(1) O(1) O(N) O(N) O(logN) O(1) O(1) O(N) O(N) O(1) CS314 Linked Lists

Code for addFront add to front of list public void addFront(E obj) How does this compare to adding at the front of an array based list? CS314 Linked Lists

Clicker Question 3 What is the Big O for adding to the front of an array based list and a linked list? The lists already contain N items. Array based Linked O(1) O(1) O(N) O(1) O(logN) O(1) O(1) O(N) O(N) O(N) CS314 Linked Lists

Code for Insert public void insert(int pos, E obj) Must be careful not to break the chain! Where do we need to go? Special cases? CS314 Linked Lists

Clicker Question 4 What is the Big O for inserting an element into the middle of an array based list and into the middle of a linked list? Each list already contains N items. Array based Linked O(1) O(1) O(1) O(N) O(N) O(1) O(N) O(N) O(N) O(logN) CS314 Linked Lists

Clicker Question 5 What is the Big O for getting an element based on position from an array based list and from a linked list? Each list contains N items. In other words E get(int pos) Array based Linked O(1) O(1) O(1) O(N) O(N) O(1) O(logN ) O(N) O(N) O(N) CS314 Linked Lists

Code for get public E get(int pos) The downside of Linked Lists CS314

Code for remove public E remove(int pos) CS314 Linked Lists

Why Use Linked List What operations with a Linked List faster than the version from ArrayList? CS314 Linked Lists

Getting All Elements in Order From a Linked Lists What is the Order (Big O) of the following code? LinkedList<Integer> list; list = new LinkedList<Integer>(); // code to fill list with N elements //Big O of following code? for(int i = 0; i < list.size(); i++) System.out.println( list.get(i) ); O(N) B. O(2N) C. O(NlogN) D. O(N2) E. O(N3) CS314 Linked Lists

Other Possible Features of Linked Lists Doubly Linked Circular Dummy Nodes for first and last node in list public class DLNode<E> { private E myData; private DLNode<E> myNext; private DLNode<E> myPrevious; } CS314 Linked Lists

Dummy Nodes Use of Dummy Nodes for a Doubly Linked List removes most special cases Also could make the Double Linked List circular CS314 Linked Lists

Doubly Linked List add public void add(E obj) CS314 Linked Lists

Insert for Doubly Linked List public void insert(int pos, E obj) CS314 Linked Lists