Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Similar documents
The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Linked List Nodes (reminder)

Dynamic Data Structures

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Programming II (CS300)

Linked Lists. Chapter 4

Programming II (CS300)

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

Ashish Gupta, Data JUET, Guna

Outline. iterator review iterator implementation the Java foreach statement testing

CS 310: Array-y vs Linky Lists

CS32 Discussion Week 3

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

Introduction to Computer Science II CS S-20 Linked Lists III

+ Abstract Data Types

ITI Introduction to Computing II

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Chapter 17: Linked Lists

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

ITI Introduction to Computing II

Linked List. ape hen dog cat fox. tail. head. count 5

ITI Introduction to Computing II

List ADT. B/W Confirming Pages

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Class 26: Linked Lists

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

2/22/2013 LISTS & TREES

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

ITI Introduction to Computing II

Data Structures (CS301) LAB

Java Collections Framework reloaded

CMSC 341. Linked Lists. Textbook Section 3.5

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

CS350: Data Structures Linked Lists

1. Introduction. Lecture Content

Building Java Programs

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

CMPT 225. Lecture 6 linked list

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Dynamic Data Structures and Generics

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Chapter 17: Linked Lists

Java Review: Objects

Laboratorio di Algoritmi e Strutture Dati

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

CE204 Data Structures and Algorithms Part 2

Tutorial 2: Linked Lists

Introduction to Linked Data Structures

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Today's Agenda. > To give a practical introduction to data structures. > To look specifically at Lists, Sets, and Maps

Arrays & Linked Lists

Data Structure. Recitation III

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked Lists

Spring 2008 Data Structures (CS301) LAB

Queues Fall 2018 Margaret Reid-Miller

Java Collections Framework

Points off Total off Net Score. CS 314 Final Exam Spring 2017

doubly linked lists Java LinkedList

Introduction to Linked Lists

Dynamic Data Structures and Generics

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1

Doubly-Linked Lists

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Title Description Participants Textbook

Data Structures Brett Bernstein

Collections Questions

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Building Java Programs

Lists. ordered lists unordered lists indexed lists 9-3

Class 32: The Java Collections Framework

DNHI Homework 3 Solutions List, Stacs and Queues

Model Solutions. COMP 103: Test April, 2013

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

Model Solutions. COMP 103: Test May, 2013

CS 314 Final Fall 2012

EECS 2011 M: Fundamentals of Data Structures

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Dynamic Data Structures and Generics. Chapter 10

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

Transcription:

Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references: Online Java Tutorial at java.sun.com/docs/books/tutorial/collections/ General-Purpose Implementations A set of implementations for the Collection interfaces provided in java.util: Interface Set List Map Implementation HashSet, TreeSet, ArrayList, LinkedList, HashMap, TreeMap, More than one implementation is provided for each interface: each implementation uses a different data structure each is optimized for some operations, but might not be so good for others users can define their own implementations if they need to List Implementations 2

General-Purpose Implementations (continued) Each implementation of Collection has a default constructor a constructor with a Collection parameter (used to transform one type of collection to another) Sorted collections have two more constructors one that takes a Comparator another that takes a SortedCollection List Implementations 3 ArrayList Implementation: CArrayList List elements are stored in an array in positions 0 to size-1 The get and set positional access operations are O(1). We can access items in the list in a random order much like the tracks on a CD. We call such a structure a random access data structure. Adding at the end is O(1) if there is space O(n) if the array is full o need to create a larger array and copy the n elements of the old array o can use System.arraycopy() to copy the array efficiently If we have an estimate of the max size of the list, we can create a large enough array using CArrayList(int initialcapacity) List Implementations 4

Array Implementation: CArrayList Adding and removing an element at a given index is expensive we need to shift part of the array both operations are O(n) on average removing using an iterator is also O(n) on average List Implementations 5 CArrayList Implementation Example public class CArrayList<E> { public static final int INIT_CAPACITY = 10; private static final int INCREMENT_FACT = 2; private E[] data; private int count; private int capacity; public CArrayList() { data = (E[]) new Object[INIT_CAPACITY]; count = 0; capacity = INIT_CAPACITY; List Implementations 6

CArrayList Example (cont d) public boolean add(e element) { // if array is full, increase its capacity if (count == capacity) { growarray(); data[count] = element; count++; return true; List Implementations 7 CArrayList Example (cont d) private void growarray() { capacity *= INCREMENT_FACT; E[] temp = (E[]) new Object[capacity]; System.arraycopy(data, 0, temp, 0, count); data = temp; // rest of implementation available for // download from course web site List Implementations 8

Array Implementation: CArrayList The array based implementation of our list has one very nice feature: we can get an item at a particular index in O(1) time and some disadvantages: it takes O(n) time on average to insert or remove from the list expanding the list is an O(n) operation if the list fills up In the next section we examine another implementation of the List interface that removes the above disadvantages but also loses the O(1) time for accesses. List Implementations 9 Singly Linked List A singly linked list (or just linked list) consists of a series of nodes Each node has two fields: the data stored in that node a link (reference) to the next node in the list the link in the last node is null the list is accessed through a reference to its first node (head) Example: A linked list of integers: head 10 20 30 Initially, list is empty; nodes are added one at a time as needed List Implementations 10

Singly Linked List A linked list has the advantage that it grows or shrinks dynamically as items are added to or removed from the list. It has the disadvantage that you cannot get an element in the list in O(1) time. If you want to get the item at the end of the list, you have to traverse the entire list to reach it. We call such a structure a sequential access data structure. Before we consider a full-blown implementation of the List interface using a linked list, we will learn how to perform a few basic operations on a linked list like inserting an item at the head of the list, inserting at the end of the list and removing an item from the list. List Implementations 11 Java Declaration of Singly Linked List Need to define a class Node to represent a node in the linked list. This class will be a private static inner class of CLinkedList - the class that implements the linked list. We make it static because instances of the inner class do not need to access any of the data associated with the outer class. Given that the class is a private inner class, it is not accessible outside of the containing class. List Implementations 12

Java Declaration of Node class private static class Node<E> { E element; Node<E> next; Node() { this(null, null); Node( E obj ) { this(obj, null); Node(E obj, Node nxt) { element = obj; next = nxt ; List Implementations 13 Linked List Class We now consider the implementation of some of the methods of the CLinkedList class: public class CLinkedList<E> { private Node<E> head; private Node<E> tail; private int numitems; public CLinkedList() { head = tail = null; numitems = 0; List Implementations 14

Linked List Operations cont d // add to end of list void add(e obj) { Node<E> newnode = new Node<E>(obj); Node<E> cur = head; if (cur == null) { head = tail = newnode; else { tail.next = newnode; tail = newnode; numitems++; List Implementations 15 Linked List Operations cont d When we remove an object from a list, not only we need a reference to the node containing the object we want to remove, but we also need a reference to the previous node so that we keep update the next links correctly. boolean remove(e obj) { Node<E> cur = head; Node<E> prev = null; // find a reference to the node containing obj // and to the previous node while(cur!= null &&!cur.element.equals(obj)) { prev = cur; cur = cur.next; List Implementations 16

Linked List Operations cont d // if we failed to find obj in list if (cur == null) return false; if (cur == head && head == tail) head = tail = null; else if (cur == head) head = cur.next; else if (cur == tail) { tail = prev; tail.next = null; else prev.next = cur.next; numitems--; return true; List Implementations 17 Doubly Linked List Singly linked lists have the following disadvantages: you can traverse the list in only one direction o Recall that in the Java collections framework, the List interface allows a client to get a ListIterator over the list and that this iterator can move either forwards or backwards through the list. if you have a reference to a node that you want to delete or you have a reference to a node and want to insert a new node before it, you have to traverse the list to find a pointer to the previous node. A doubly linked list is a variation of a linked list that does not have the disadvantages listed above. List Implementations 18

Doubly Linked List (cont'd) In a doubly linked list each node has two links: one to the next node, one to the previous node Example: A doubly linked list of integers: head 10 20 30 In last node, link to next node is null In first node, link to previous node is null Now we can easily find the previous node traverse the list in reverse order List Implementations 19 Java Declaration of Doubly Linked List private static class Node<E> { E element; Node<E> previous; Node<E> next; Node() { this(null, null, null) ; Node(E obj) { this(obj, null, null) ; Node(E obj, Node prv, Node nxt) { element = obj; previous = prv; next = nxt; List Implementations 20

Doubly Linked List Class We now consider the implementation of some of the methods of the GLinkedList class: public class GLinkedList<E> { private Node<E> head; private Node<E> tail; private int numitems; public GLinkedList() { head = tail = null; numitems = 0; List Implementations 21 Some Operations on Doubly Linked Lists // add to end of list void add(e obj) { Node<E> newnode = new Node<E>(obj, tail, null); if( head == null ) head = tail = newnode; else { tail.next = newnode; tail = newnode; numitems++; List Implementations 22

Some Operations cont d Remove an object from the list (removes only first instance found): boolean remove(e obj) { Node<E> cur = head; // look for a node containing obj while (cur!= null && cur.element.equals(obj)) cur = cur.next; // if we failed to find obj in the list if(cur == null) return false; List Implementations 23 Some Operations cont d if (cur == head && head == tail) head = tail = null; else if (cur == head) { head = cur.next; head.prev = null; else if (cur == tail) { tail = cur.prev; tail.next = null; else { cur.next.previous = cur.previous; cur.previous.next = cur.next; numitems--; return true; List Implementations 24

Linked List Implementation of List: GLinkedList GLinkedList is in Examples : implements List interface uses a doubly linked list with references to the head and tail nodes uses modcount to count # of modifications o an iterator uses this to detect concurrent modifications GListIterator (in same example) implements ListIterator interface keeps track of o last node returned by next() o next node to be returned by next() o # of modifications done by the iterator GLinkedList is very similar to Java s LinkedList but LinkedList uses a circular doubly linked list with a head node List Implementations 25 GLinkedList (cont ) After executing: List<Integer> glist = new GLinkedList<Integer>(); Iterator<Integer> itr = glist.iterator(); glist.add(new Integer(5)); glist.add(new Integer(6)); glist.add(new Integer(7)); itr.next(); List Implementations 26

GLinkedList (cont d) glist and itr look like: head tail size modcount glist 3 3 lastnode nextnode nextindex ExpectedModCount itr 1 3 5 6 7 List Implementations 27 Exercises Chapter 20, page 771 Exercises P20.3, P20.5 P20.9 starting from the GlinkedList class provided on the Web site. List Implementations 28