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.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Queues and Linked Lists
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
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.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
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.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
Implementing and Using Stacks
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Linked List, Stacks Queues
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Stacks and Linked Lists
Presentation transcript:

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 but don't have indexes. – Client can only add to the end of the queue, and can only examine/remove the front of the queue.

The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: – enqueue(object): inserts an element at the end of the queue – object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: – object front(): returns the element at the front without removing it – integer size(): returns the number of elements stored – boolean isEmpty(): indicates whether no elements are stored Exceptions – Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues3

Queue Example Linked Lists4 front enqueue(5)- OperationOutput enqueue(3)- dequeue()5 enqueue(7)- dequeue()3 front()7 dequeue()7 dequeue()“error” isEmpty()true enqueue(9)- enqueue(7)- size()2 enqueue(3)- enqueue(5)- dequeue()9

Applications of Queues Direct applications – Waiting lists – Access to shared resources (e.g., printer) – Multiprogramming Indirect applications – Auxiliary data structure for algorithms – Component of other data structures Queues5

Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty Queues6 Q 012rf normal configuration Q 012fr wrapped-around configuration

Queue Operations We use the modulo operator (remainder of division) Queues7 Algorithm size() return (N  f + r) mod N Algorithm isEmpty() return (f  r) Q 012rf Q 012fr

Queue Operations (cont.) Operation enqueue throws an exception if the array is full This exception is implementation- dependent Queues8 Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Q 012rf Q 012fr

Queue Operations (cont.) Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Queues9 Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Q 012rf Q 012fr

Queue Interface in Java Java interface corresponding to our Queue ADT Requires the definition of class EmptyQueueException No corresponding built-in Java class Queues10 public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: 1. e = Q.dequeue() 2. Service element e 3. Q.enqueue(e) Queues11 The Queue Shared Service 1.Deque the next element 3.Enqueue the serviced element 2.Service the next element

Summary of Queues Definition of Queues (basic) Array implementation Assignment 1: (due on Tuesday of Week 6) Implement the array-based Queue in Java assuming the interface is already given in Queue.java. Write a main method to have a Queue with the operations on side 4 of lecture 3. Hint: Please look at ArrayStack.java. Queues12

Part-B3 Linked Lists Linked Lists13

Introducing Linked Lists (continued) Think of each element in a linked list as being an individual piece in a child's pop chain. To form a chain, insert the connector into the back of the next piece © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Introducing Linked Lists (continued) Inserting a new piece into the chain – breaking a connection and – reconnecting the chain at both ends of the new piece. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Introducing Linked Lists (continued) Removal of a piece – breaking its two connections – removing the piece, and then reconnecting the chain. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores – element – link to the next node Linked Lists17 next elem node ABCD 

Inserting at the Head 1.Allocate a new node Linked Lists18  head 2. Update new element 3. Have new node point to old head 4. Update head to point to new node Baltimore Rome ScattleToronto  head Rome ScattleToronto Baltimore  head Rome ScattleToronto (a) (b) (c)

Removing at the Head 1.Update head to point to next node in the list Linked Lists19  head Baltimore Rome ScattleToronto  head Rome ScattleToronto Baltimore  head Rome ScattleToronto (c) (b) (a) 2.Allow garbage collector to reclaim the former first node

Inserting at the Tail Linked Lists20 1.Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node  head Rome ScattleToronto (a) tail  head Rome ScattleToronto (b) tail Zurich  head Rome ScattleToronto (c) tail  Zurich

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 Linked Lists21 The interface of data structure list is in List.java. The implementation is in NodeList.java. But it uses DNode.java. Actually, it is doubly linked list.

bat  cat  sat  vat NULL Linked Lists

bat  cat  sat  vat NULL mat  Insertion Compare this with the insertion in arrays!

bat  cat  sat  vat NULL mat  dangling reference Deletion

The Node Class for List Nodes (the file is source/Node.java) public classNode{ // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node(){ this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } Linked Lists25

Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time Linked Lists26  t nodes elements

Queue with a Singly Linked List We can implement a queue with a singly linked list – The front element is stored at the first node – The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(?) time Linked Lists27 f r  nodes elements

List ADT (§ 5.2.3) The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: – size(), isEmpty() Accessor methods: – first(), last() – prev(p), next(p) Update methods: – replace(p, e) – insertBefore(p, e), insertAfter(p, e), – insertFirst(e), insertLast(e) – remove(p) Linked Lists28

Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: – element – link to the previous node – link to the next node Special trailer and header nodes Linked Lists29 prevnext elem trailer header nodes/positions elements node

Insertion We visualize operation insertAfter(p, X), which returns position q Linked Lists30 ABXC ABC p ABC p X q pq

Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p){link v to its predecessor} v.setNext(p.getNext()){link v to its successor} (p.getNext()).setPrev(v){link p’s old successor to v} p.setNext(v){link p to its new successor, v} return v{the position for the element e} Linked Lists31

Deletion We visualize remove(p), where p = last() Linked Lists32 ABCD p ABC D p ABC

Deletion Algorithm Algorithm remove(p): t = p.element{a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()){linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null){invalidating the position p} p.setNext(null) return t Linked Lists33

Insertion of an Element at the Head

Have a new node: header RomeSeattle trailer Baltimore Toronto

Update the links: header RomeSeattle trailer Baltimore Toronto

Deleting an Element at the Tail

Update the links: header Rome Seattle trailer BaltimoreToronto

Performance In the implementation of the List ADT by means of a doubly linked list – The space used by a list with n elements is O(n) – The space used by each position of the list is O(1) – All the operations of the List ADT run in O(1) time – Operation element() of the Position ADT runs in O(1) time Linked Lists 41