1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.

Slides:



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

Stacks, Queues, and Linked Lists
Queues and Linked Lists
CS 367 – Introduction to Data Structures
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
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.
CSCI2100B Linked List Jeffrey
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++)
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
Stacks, Queues, and Deques
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.
Queues.
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
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:
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
LinkedList Many slides from Horstmann modified by Dr V.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
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.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
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.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
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.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Linked Lists CS 367 – Introduction to Data Structures.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Linked Data Structures
CSE 1342 Programming Concepts
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Elementary Data Structures
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Chapter 12 – Data Structures
Tirgul 12 Data Structures (2) 1.
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
Java collections library
Priority Queue.
Basic Data Types Queues
Linked Lists, Stacks and Queues Textbook Sections
Data Structures ADT List
Data Structures ADT List
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures ADT List
More Data Structures (Part 1)
CS210- Lecture 6 Jun 13, 2005 Announcements
Presentation transcript:

1

Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today

Java collections library public interface java.util.List booleanadd(int index, E element) Inserts the specified element at the specified position in this list (optional operation). Eremove(int index)Removes the element at the specified position in this list … Implementations: LinkedListdoubly-linked list implementation ArrayListresizable-array implementation 3

Example client 4 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); 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); }

Example client Input: “judge me by my size do > you > ” 5 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); 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); }

Example client Can we implement StackQueue efficiently using a linked list? 6 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); 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); }

Example client Can we implement StackQueue efficiently using a linked list? enqueue and dequeue can run in Θ(1) but pop will need to run in Θ(n) we can do better 7 public static void main(String[] args) { StackOfStrings buffer = new StackQueue (); 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); }

Doubly Linked Lists (Java implementation in Assignment 2) ssignment-2/ 8

Doubly Linked Lists 9 head  tail  tobeornotto null class DLLNode { String item; DLLNode next; DLLNode prev; }

Doubly Linked Lists 10 head  tail  tobeornotto null class DLLNode { String item; DLLNode next; DLLNode prev; } nullto notbe null 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 States of nodes:

Doubly Linked Lists classDLLofString 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 11

Doubly Linked Lists classDLLofString 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 12

Three main cases to deal with 13 head  tail  tobeornotto null head tail  to null head  null tail  null >1 node 1 node 0 nodes

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

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

insertLast(“be”) 16 head  tail  tobeornotto null >1 node

insertLast(“be”) 17 head  tail  tobeornottobe null >1 node

insertLast(“be”) 18 head  tail  tobeornottobe null >1 node

insertLast(“be”) 19 head  tail  tobeornottobe null >1 node

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

insertLast(“be”) 21 head = tail  tobe null 1 node

insertLast(“be”) 22 head = tail  tobe null 1 node

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

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

insertLast(“be”) 25 head  nulltail  null 0 nodes

insertLast(“be”) 26 head  nulltail  null be null 0 nodes

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

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

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

30 /** * Inserts an element in the doubly linked list 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. data : The new data of class T that needs to be added to the list none * */ public void insertBefore( int pos, T data )

void insertBefore(pos,“be”) 31 pos == 0 (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 (insert at the head of the DLL) 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”) 32 pos == 0 (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 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”) DLL of size 1 insertFirst(“be”) DLL of size > 1 insertFirst(“be”)

void insertBefore(pos,“be”) 33 pos == 0 (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 (insert at the head of the DLL) 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 insertFirst(“be”)not possible 0 < pos < 0 DLL of size > 1 insertFirst(“be”)?

void insertBefore(pos,“be”) 34 pos == 0 (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 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”)not possible 0 < pos < -1 insertFirst(“be”) (here pos == -1) DLL of size 1 insertFirst(“be”)not possible 0 < pos < 0 insertFirst(“be”) (here pos == 0) DLL of size > 1 insertFirst(“be”)??

void insertBefore(pos,“be”) 35 pos == 0 (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 (insert at the head of the DLL) pos ≥ DLL.size (insert at the end of the DLL) DLL of size 0 insertFirst(“be”)not possible 0 < pos < -1 insertFirst(“be”) (here pos == -1) insertFirst(“be”)InsertLast(“be”) DLL of size 1 insertFirst(“be”)not possible 0 < pos < 0 insertFirst(“be”) (here pos == 0) insertFirst(“be”)InsertLast(“be”) DLL of size > 1 insertFirst(“be”)?? InsertLast(“be”)

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

insertBefore(2,“nice”) 37 head  tail  tobeornottobe null

insertBefore(2,“nice”) 38 head  ptr  tail  tobeornottobe null

insertBefore(2,“nice”) 39 head  ptr  tail  tobeornottobe null tmp  nice null

insertBefore(2,“nice”) 40 head  ptr  tail  tobeornottobe null tmp  nice null

insertBefore(2,“nice”) 41 head  ptr  tail  tobeornottobe null tmp  nice null

insertBefore(2,“nice”) 42 head  ptr  tail  tobeornottobe null tmp  nice null

insertBefore(2,“nice”) 43 head  ptr  tail  tobeornottobe null tmp  nice