CS2013 Lecture 4 John Hurley Cal State LA.

Slides:



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

Singly linked lists Doubly linked lists
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
John Hurley Cal State LA
M180: Data Structures & Algorithms in Java
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
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,
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
List Interface and Linked List Mrs. Furman March 25, 2010.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked List, Stacks Queues
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Unit – I Lists.
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Lectures linked lists Chapter 6 of textbook
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Data structures and algorithms
Lecture - 6 On Data Structures
John Hurley Cal State LA
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Topic 11 Linked Lists -Joel Spolsky
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Sequences 11/22/2018 3:25 AM Doubly-Linked Lists Doubly-Linked Lists.
List Data Structure.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
CS212D: Data Structures Week 5-6 Linked List.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Linked Lists Chapter 4.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
CS210- Lecture 6 Jun 13, 2005 Announcements
BY PROF. IRSHAD AHMAD LONE.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Topic 11 Linked Lists -Joel Spolsky
Problem Understanding
Presentation transcript:

CS2013 Lecture 4 John Hurley Cal State LA

2 The List Interface

Lower-Level Implementation of Lists 3 Lower-Level Implementation of Lists You will probably never need to code a List class yourself after you graduate from school. Java and most other programming languages have built in ones that are good enough for nearly all cases. However, the next lab will involve coding methods with a singly linked list, and later in the course we will code parts or all of many more-complex data structures. The course web page contains a link to a code package with a partial implementation of a linked list.

4 ArrayList Like an array, a list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index. Recall that an array has a fixed length. An array is suitable if your application does not require the data structure to change size.. A list can grow or shrink dynamically. ArrayList and LinkedList are the most common concrete implementations of the List interface. ArrayList is implemented using an underlying array that begins with a default size. If the list outgrows the array, a new, larger array must be created and the contents of the old one copied. For random access using indices, ArrayLists are very efficient, because these operations are very efficiently implemented for arrays. However, inserting or deleting (as distinct from initializing or changing) a value at an arbitrary spot in an array list involves moving all the subsequent elements in the array. If the list never reaches the original size of the array, some of the array is wasted. Therefore, ArrayList is not efficient in memory usage, which is often the bottleneck in present-day applications.

5 java.util.ArrayList

6 Linked Lists A linked list is a data structure consisting of a group of nodes which represent a sequence of values of the parameterizing type. In the simplest form of linked list, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence. This is called a singly linked list. We will learn more about how linked lists are implemented later; here we will use the Java Collections Framework LinkedList<E> class.

7 Linked Lists In a doubly linked list, each node contains a reference to the next node and a reference to the previous node. Either a singly or double linked list may be circular; that is, last node points to the first, and, in a circular doubly-linked list, the first node has a reference to the last Since the items may not be stored in contiguous memory, the JVM traverses the list by following the links. The additional links in these types of linked lists make this process more efficient. Diagrams from Wikipedia: http://en.wikipedia.org/wiki/Linked_list

8 java.util.LinkedList Java’s built-in LinkedList class implements a doubly-linked list. It implements the List interface, but implements some of the methods differently than ArrayList does.

Node as an Inner Class Singly Linked Lists 9

Accessor Methods Singly Linked Lists 10

11 Linked Lists Adding and removing elements from any spot in a singly linked list involves changing, at most, one reference in each of two existing elements and setting, at most, two references in a new element. Compare this to the expense of moving all the elements in an array after the insertion point of the new value or of copying all the elements from an old array to a new one. Add a node to a singly-linked list: Inserting a node into a doubly linked list just involves also changing the reference from the subsequent element and adding one from the new element back to the previous one

Inserting at the Head Allocate new node Insert new element Have new node point to old head Update head to point to new node Singly Linked Lists 12

Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node Singly Linked Lists 13

14 Linked Lists Delete a node from a singly-linked list:

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node Singly Linked Lists 15

16 Linked Lists Data in a linked list is accessed sequentially; we start at the head of the list and follow the references. This is called traversing the list. node = list.firstNode while node not null (do something with node.data) node = node.next Source for pseudocode: Wikipedia This makes a linked list less efficient than an array list for operations that require a great deal of traversal, because accessing an array element by index is very efficient. Also, memory is allocated on the fly when a node is added. This makes it more likely that different nodes will be far apart in memory, which sometimes makes it more expensive to traverse the list.

Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node Singly Linked Lists 17

18 Linked Lists Course page has link to linkedlist and listcompare packages

19 I/O Expense This is a good time to point out that I/O is very expensive. Rerun the List Comparer code above with System.out.println statements inside the loops in the add and get methods.

Doubly Linked List A doubly linked list can be traversed forward and backward Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next element node trailer header nodes/positions elements © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 20

Insertion p A B C p q A B C X p q A B X C Insert a new node, q, between p and its successor. p A B C p q A B C X p q A B X C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 21

Deletion A B C D p A B C p D A B C Remove a node, p, from a doubly linked list. A B C D p A B C p D A B C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 22

Doubly-Linked List in Java © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 23

Doubly-Linked List in Java, 2 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 24

Doubly-Linked List in Java, 3 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 25

Doubly-Linked List in Java, 4 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 26

27 Cycles If traversing a list by following the next references will result in returning to a node already visited, a cycle exists. This happens if one of the next references points back to a node that appeared earlier in the list. A cycle will make it impossible to traverse the list fully or to add a node at the end. The part of the list that constitutes the cycle is equivalent to a circular linked list, but this is much more likely to happen as a result of a programming error than of deliberate design.

Cycles and Floyd’s Algorithm 28 Cycles and Floyd’s Algorithm A cycle will result in traversal continuing infinitely. However, we can’t test for that, since there is no specific amount of traversal time after which we can show that there must be a cycle. We could test for cycles by traversing the list, keeping a new list of references to the nodes in the original list, and checking the new list each time we encounter a new node. This would not be very expensive in memory, since we are keeping references. However, the computation required to check the new list each time would increase as the size of the list increases. To test for cycles efficiently, create two references to nodes and set them both equal to head. Use a loop to traverse the list in parallel but at two different speeds (usually one moves one node at a time and the other moves two nodes at a time.) If one of the references reaches the last element (the one whose next is null) there is no cycle. If the two references ever refer to the same node, there is a cycle.

29 Iterators An iterator is an object that provides methods for traversing the elements of a collection. Even with Lists, which are not hard to manipulate using loops, iterators sometimes provide easier ways to traverse data structures In many cases, the enhanced for loop is just as convenient as an iterator.

30 The List Iterator

Iterator public static void main(String[] args) { 31 Iterator public static void main(String[] args) { List<String> names = new LinkedList<String>(); names.add("John"); names.add("Paul"); names.add("George"); names.add("Ringo"); String currName; ListIterator<String> namesIterator = names.listIterator(); while(namesIterator.hasNext()){ currName = namesIterator.next(); System.out.println(currName + " " + (currName.compareTo("Mick") < 0? "earlier than Mick": "not earlier than Mick")); }