CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.

Slides:



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

Stacks, Queues, and Linked Lists
Linked Lists.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
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 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 307 Fundamentals of Computer Science 1 Lists and ArrayList many slides taken from Mike Scott, UT Austin.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Summary of lectures (1 to 11)
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
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,
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Scott Grissom, copyright 2004 Ch 6 Data Structures Slide 1 Linear Data Structures Chapter 6 focuses on: Lists Stacks Queues You can skip the Array implementation.
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.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
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.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 5 Linked Lists II
Linked Structures, LinkedStack
CS2006- Data Structures I Chapter 5 Linked Lists III.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
List Interface and Linked List Mrs. Furman March 25, 2010.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Linked Lists CS 367 – Introduction to Data Structures.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
An Array-Based Implementation of the ADT List
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
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists I.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
CSE 373: Data Structures and Algorithms
public class StrangeObject { String name; StrangeObject other; }
Top Ten Words that Almost Rhyme with “Peas”
Topic 11 Linked Lists -Joel Spolsky
Arrays and Linked Lists
Linked Lists: Implementation of Queue & Deque
Linked Lists [AJ 15].
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin

CS 307 Fundamentals of Computer Science 2 Recursive Data Structures  Linked Lists are dynamic data structures –They grow and shrink one element at a time, normally without some of the inefficiencies of arrays  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  If accesses are all at beginning or end of list, a linked structure offers improvements  Linked Lists could be used as the underlying storage container for higher level ADTs (stack, queue)

CS 307 Fundamentals of Computer Science 3 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 every 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 Science 4 Elements of Linked Lists public class Node {private Object myData; private Node myNext; public Node() {myData = null; myNext = null;} public Node(Object data, Node next) {myData = data;myNext = next; } public Object getData() {return myData;} public Node getNext() {return myNext;} public void setData(Object data) {myData = data;} public void setNext(Node next) {myNext = next;} }

CS 307 Fundamentals of Computer Science 5 One Implementation of a Linked List  The Nodes shown 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 Science 6 A Simple List Interface public interface IList { void add(Object item); void add(Object item, int pos); Object set(Object item, int pos); void add(List other); Object get(int pos); Object remove(int pos); int size(); }

CS 307 Fundamentals of Computer Science 7 A Linked List Implementation public class LinkedList implements Ilist {private Node myHead; private Node myTail; private int iMySize; public LinkedList() {myHead = null; myTail = null; iMySize = 0; } LinkedList list = new LinkedList(); LinkedList myHead iMySize myTail null 0

CS 307 Fundamentals of Computer Science 8 Add Element - List Empty (Before) myHeadmyTailiMySize null 0 Object item

CS 307 Fundamentals of Computer Science 9 Add Element - List Empty (After) myHeadmyTailiMySize 1 Object Node myDatamyNext null

CS 307 Fundamentals of Computer Science 10 Add Element - List Not Empty (Before) 1 Object Node myDatamyNext null myHeadmyTailiMySize Object item

CS 307 Fundamentals of Computer Science 11 Add Element - List Not Empty (After) 2 Object Node myDatamyNext myHeadmyTailiMySize Object Node myDatamyNext null

CS 307 Fundamentals of Computer Science 12 Code for default add  public void add(Object item)

CS 307 Fundamentals of Computer Science 13 Code for arbitrary add  public void add(Object item, int pos)

CS 307 Fundamentals of Computer Science 14 Code for get  public Object get(int pos)

CS 307 Fundamentals of Computer Science 15 Code for remove  public Object remove(int pos)

CS 307 Fundamentals of Computer Science 16 Why Linked List  Are any operations with a Linked List faster than the version from ArrayList?  What about this:  public void addFront(Object item)  Big O? Array version Big O?  Fast performance for removeFront as well?

CS 307 Fundamentals of Computer Science 17 Remove Back Method  public Object removeBack()  Big O?

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

CS 307 Fundamentals of Computer Science 19 Default add for Doubly Linked List  public void add(Object item)

CS 307 Fundamentals of Computer Science 20 Code for arbitrary add - Doubly Linked List  public void remove(Object item, int pos)

CS 307 Fundamentals of Computer Science 21 Code for remove Doubly Linked List  public Object remove(int pos)