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.

Slides:



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

Singly linked lists Doubly linked lists
Lists CS 3358.
Stacks, Queues, and Linked Lists
Linked Lists.
Linear Lists – Linked List Representation
Chapter 9: Data Structures I
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Arrays And ArrayLists - S. Kelly-Bootle
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
C++ Classes and Data Structures Jeffrey S. Childs
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,
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
Data structures Abstract data types Java classes for Data structures and ADTs.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Chapter 5 Linked Lists II
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked Lists Revision Based on: v/
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.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Data Structure & Algorithms
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
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.
Linked Lists CS 367 – Introduction to Data Structures.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Lecture 6 of Computer Science II
Elementary Data Structures
CS2006- Data Structures I Chapter 5 Linked Lists I.
LINKED LISTS CSCD Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
Topic 11 Linked Lists -Joel Spolsky
Arrays and Linked Lists
Linked Lists.
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

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 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.

Attendance Question 1  What is output by the following code? ArrayList a1 = new ArrayList (); ArrayList a2 = new ArrayList (); a1.add(12); a2.add(12); System.out.println( a1 == a2 ); A.No output due to syntax error B.No output due to runtime error C. false D. true CS 307 Fundamentals of Computer ScienceLinked Lists 2

CS 307 Fundamentals of Computer ScienceLinked Lists 3 Dynamic Data Structures  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 like 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

Linked Lists 4 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

Linked Lists 5 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 Jane Jones

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

CS 307 Fundamentals of Computer ScienceLinked Lists 7  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

CS 307 Fundamentals of Computer ScienceLinked Lists 8 Advantages of linked lists  Linked lists are dynamic, they can grow or shrink as necessary  Linked lists can be maintained in sorted order simply by inserting each new element at the proper point in the list. Existing list elements do not need to be moved  Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled from any physical ordering in memory

CS 307 Fundamentals of Computer ScienceLinked Lists 9 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

CS 307 Fundamentals of Computer ScienceLinked Lists 10 A Node Class public class Node { private E myData; private Node myNext; public Node() {myData = null; myNext = null;} public Node(E data, Node next) {myData = data; myNext = next; } public E getData() {return myData;} public Node getNext() {return myNext;} public void setData(Et data) {myData = data;} public void setNext(Node next) {myNext = next;} }

CS 307 Fundamentals of Computer ScienceLinked Lists 11 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

CS 307 Fundamentals of Computer ScienceLinked Lists 12 Interfaces and Standard Java  Finally, an alternate implementation to an ADT  Specify a List interface –Java has this  Implement in multiple ways –ArrayList –LinkedList  Which is better? Cookie

CS 307 Fundamentals of Computer ScienceLinked Lists 13 A Linked List Implementation public class LinkedList implements Ilist private Node head; private Node tail; private int size; public LinkedList(){ head = null; tail = null; size = 0; } LinkedList list = new LinkedList (); LinkedList myHead iMySize myTail null 0

CS 307 Fundamentals of Computer ScienceLinked Lists 14 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!

CS 307 Fundamentals of Computer ScienceLinked Lists 15 add method  add to the end of list  special case if empty  steps on following slides  public void add(Object obj)

CS 307 Fundamentals of Computer ScienceLinked Lists 16 Add Element - List Empty (Before) headtailsize null 0 Object item

CS 307 Fundamentals of Computer ScienceLinked Lists 17 Add Element - List Empty (After) headtailsize 1 String Node myDatamyNext null

CS 307 Fundamentals of Computer ScienceLinked Lists 18 Add Element - List Not Empty (Before) 1 String Node myDatamyNext null headtailsize String item

CS 307 Fundamentals of Computer ScienceLinked Lists 19 Add Element - List Not Empty (After) 2 String Node myDatamyNext headtailsize String Node myDatamyNext null

CS 307 Fundamentals of Computer ScienceLinked Lists 20 Code for default add  public void add(Object obj)

Attendance 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 contains N items. Array basedLinked A.O(1)O(1) B.O(N)O(N) C.O(logN)O(1) D.O(1)O(N) E.O(N)O(1) CS 307 Fundamentals of Computer ScienceLinked Lists 21

CS 307 Fundamentals of Computer ScienceLinked Lists 22 Code for addFront  add to front of list  public void addFront(Object obj)  How does this compare to adding at the front of an array based list?

Attendance Question 3  What is the Big O for adding to the front of an array based list and a linked list? The lists already contains N items. Array basedLinked A.O(1)O(1) B.O(N)O(1) C.O(logN)O(1) D.O(1)O(N) E.O(N)O(N) CS 307 Fundamentals of Computer ScienceLinked Lists 23

CS 307 Fundamentals of Computer ScienceLinked Lists 24 Code for Insert  public void insert(int pos, Object obj)  Must be careful not to break the chain!  Where do we need to go?  Special cases?

Attendance Question 4  What is the Big O for inserting an element into the middle of an array based list and a linked list? The lists contains N items. Array basedLinked A.O(N)O(N) B.O(N)O(1) C.O(logN)O(1) D.O(logN)O(logN)) E.O(1)O(N) CS 307 Fundamentals of Computer ScienceLinked Lists 25

Attendance Question 5  What is the Big O for getting an element based on position from an array based list and a linked list? The lists contain N items. Array basedLinked A.O(1)O(N) B.O(N)O(1) C.O(logN)O(1) D.O(logN)O(N) E.O(N)O(N) CS 307 Fundamentals of Computer ScienceLinked Lists 26

CS 307 Fundamentals of Computer ScienceLinked Lists 27 Code for get  public Object get(int pos)  The downside of Linked Lists

CS 307 Fundamentals of Computer ScienceLinked Lists 28 Code for remove  public Object remove(int pos)

CS 307 Fundamentals of Computer ScienceLinked Lists 29 Why Use Linked List  What operations with a Linked List faster than the version from ArrayList?

CS 307 Fundamentals of Computer ScienceLinked Lists 30 Remove Back Method  public Object removeBack()  Big O?

CS 307 Fundamentals of Computer ScienceLinked Lists 31 Iterators for Linked Lists  What is the Big O of the following code? LinkedList list; list = new LinkedList (); // 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) );

Attendance Question 6  What is the Big O of the code on the previous slide? A.O(N) B.O(2 N ) C.O(NlogN) D.O(N 2 ) E.O(N 3 ) CS 307 Fundamentals of Computer ScienceLinked Lists 32

CS 307 Fundamentals of Computer ScienceLinked Lists 33 Other Possible Features of Linked Lists  Doubly Linked  Circular  Dummy Nodes for first and last node in list public class DLNode { private E myData; private DLNode myNext; private DLNode myPrevious; }

CS 307 Fundamentals of Computer ScienceLinked Lists 34 Dummy Nodes  Use of Dummy Nodes for a Doubly Linked List removes most special cases  Also could make the Double Linked List circular

CS 307 Fundamentals of Computer ScienceLinked Lists 35 Doubly Linked List addFront  public void addFront(Object obj)

CS 307 Fundamentals of Computer ScienceLinked Lists 36 Insert for Doubly Linked List  public void insert(int pos, Object obj)