Java collections library

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues and Linked Lists
CS 367 – Introduction to Data Structures
Data Structures ADT List
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Data Structure Lecture-5
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
LinkedList Many slides from Horstmann modified by Dr V.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Introduction to Data Structures and Algorithms
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Lab - 11 Keerthi Nelaturu. Ordered Structure Using Doubly linked list All elements in the list are arranged in an order Implement the Ordered Structure.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Week 15 – Monday.  What did we talk about last time?  Tries.
1 CS162: Introduction to Computer Science II Abstract Data Types.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
An Array-Based Implementation of the ADT List
Linked Data Structures
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Marcus Biel, Software Craftsman
Week 4 - Friday CS221.
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
COP 3503 FALL 2012 Shayan Javed Lecture 8
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
Priority Queue.
Chapter 17 Object-Oriented Data Structures
Basic Data Types Queues
Programming in Java Lecture 11: ArrayList
List Data Structure.
Data Structures ADT List
Data Structures ADT List
Data Type (how to view it)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ADT list.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
Queues CSC212.
Data Structures ADT List
Stacks and Queues.
More Data Structures (Part 1)
CS210- Lecture 6 Jun 13, 2005 Announcements
General List.
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

Java collections library public interface java.util.List<E> boolean add(int index, E element) Inserts the specified element at the specified position in this list (optional operation). E remove(int index) Removes the element at the specified position in this list … Implementations: LinkedList doubly-linked list implementation ArrayList resizable-array implementation

Example client pop: return and remove the most recent element public static void main(String[] args) { StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); } } pop: return and remove the most recent element dequeue: return and remove the least recent element enqueue: add an element

Example client public static void main(String[] args) { StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); } } Input: “judge me by my size do > you > < < < < < ? >”

Example client public static void main(String[] args) { StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); } } Can we implement StackQueue efficiently using a linked list? head  to be or not null

Example client public static void main(String[] args) { StackOfStrings buffer = new StackQueue<String>(); while (!StdIn.isEmpty()) String s = StdIn.readString(); if (s.equals(“<”)) StdOut.print(buffer.pop()); else if (s.equals(“>”)) StdOut.print(buffer.dequeue()); else stack.enqueue(s); } } Can we implement StackQueue efficiently using a linked list? enqueue and push will run in Θ(1) one of dequeue and pop will run in Θ(1) and the other in Θ(N) we can do better

Doubly Linked Lists (Java implementation in Assignment 2) https://www.scss.tcd.ie/Vasileios.Koutavas/teaching/cs2010/mt1718/assignment-2/

Doubly Linked Lists (DLL) class DLLNode { String item; DLLNode next; DLLNode prev; } head  tail to be or not null

Doubly Linked Lists (DLL) class DLLNode { String item; DLLNode next; DLLNode prev; } head  tail to be or not null Kinds of nodes inside a DLL: null to not be Empty DLL The only node of a DLL with 1 node The first node of a DLL with >1 node The last node of a DLL with >1 node A middle node of a DLL with >2 items

Doubly Linked Lists class DLLofString DoublyLinkedList() void insertFirst(String s) inserts s at the head of the list String getFirst() returns string at the head of the list boolean deleteFirst() removes string at the head of the list void insertLast(String s) inserts s at the end of the list String getLast(String s) returns string at the end of the list boolean deleteLast() removes string at the end of the list void insertBefore(int pos, String s) inserts s before position pos String get(int pos) returns string at position pos boolean deleteAt(int pos) deletes string at position pos Interface somewhat different than that of java.util.List. How to make interface generic? class DoublyLinkedList<T>

Doubly Linked Lists class DLLofString DoublyLinkedList() void insertFirst(String s) inserts s at the head of the list String getFirst() returns string at the head of the list boolean deleteFirst() removes string at the head of the list void insertLast(String s) inserts s at the end of the list String getLast(String s) returns string at the end of the list boolean deleteLast() removes string at the end of the list void insertBefore(int pos, String s) inserts s before position pos String get(int pos) returns string at position pos boolean deleteAt(int pos) deletes string at position pos Interface somewhat different than that of java.util.List. How to make interface generic? class DoublyLinkedList<T>

Three main cases to deal with head  tail to be or not null >1 node head tail  to null 1 node head  null tail  null 0 nodes

/** * Inserts an element at the end of the doubly linked list * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertLast( T data )

void insertLast(“be”) DLL of size 0 ? DLL of size 1 DLL of size > 1 Richard Feynman: “Consider simple examples but not too simple!” (paraphrase) cases for size of the DLL

insertLast(“be”) >1 node head  tail to be or not null

insertLast(“be”) >1 node head  tail tmp to be or not null

insertLast(“be”) >1 node head  tail tmp to be or not null

insertLast(“be”) >1 node head  tail to be or not null

insertLast(“be”) 1 nodes head = tail  to null

insertLast(“be”) 1 node head = tail  tmp to be null

insertLast(“be”) 1 node head = tail  tmp to be null

insertLast(“be”) 1 node head  tail to be null

insertLast(“be”) 1 node * Same as the case with >1 node head  tail to be null * Same as the case with >1 node

insertLast(“be”) 0 nodes head  null tail  null

insertLast(“be”) 0 nodes head  null tmp  tail  null be null

insertLast(“be”) 0 nodes head = tail  be null

how can we implement this? /** * Inserts an element at the beginning of the doubly linked list * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertFirst( T data ) how can we implement this?

void insertFirst(“be”) DLL of size 0 ? DLL of size 1 DLL of size > 1 write down an example for each case cases for size of the DLL

/** * Inserts an element in the doubly linked list * @param pos : The integer location at which the new data should be * inserted in the list. We assume that the first position in the list * is 0 (zero). If pos is less than 0 then add to the head of the list. * If pos is greater or equal to the size of the list then add the * element at the end of the list. * @param data : The new data of class T that needs to be added to the list * @return none * */ public void insertBefore( int pos, T data )

void insertBefore(pos,“be”) (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 DLL of size 1 DLL of size > 1

void insertBefore(pos,“be”) (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) DLL of size 1 DLL of size > 1

void insertBefore(pos,“be”) (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) not possible 0 < pos < -1 DLL of size 1 0 < pos < 0 DLL of size > 1 ?

void insertBefore(pos,“be”) (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) not possible 0 < pos < -1 (here pos == -1) DLL of size 1 0 < pos < 0 (here pos == 0) DLL of size > 1 ?

void insertBefore(pos,“be”) (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) not possible 0 < pos < -1 (here pos == -1) InsertLast(“be”) DLL of size 1 0 < pos < 0 (here pos == 0) DLL of size > 1 ?

void insertBefore(pos,“be”) (insert at the head of the DLL) 0 < pos < DLL.size -1 (insert in the middle of the DLL) pos == DLL.size -1 pos < 0 pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) not possible 0 < pos < -1 (here pos == -1) InsertLast(“be”) DLL of size 1 0 < pos < 0 (here pos == 0) DLL of size > 1 ? same as case to the left

insertBefore(2,“nice”) head  tail to be or not null

insertBefore(2,“nice”) head  ptr tail to be or not null

insertBefore(2,“nice”) head  ptr tail to be or not null tmp  nice null

insertBefore(2,“nice”) head  ptr tail to be or not null tmp  nice null

insertBefore(2,“nice”) head  ptr tail to be or not null tmp  nice null

insertBefore(2,“nice”) head  ptr tail to be or not null tmp  nice null

insertBefore(2,“nice”) head  ptr tail to be or not null tmp  nice