Queues Rem Collier Room A1.02

Slides:



Advertisements
Similar presentations
Queues and Linked Lists
Advertisements

1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
© 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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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?
Queues.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
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)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
© 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,
Stacks II David Lillis School of Computer Science and Informatics
Elementary Data Structures
Review Array Array Elements Accessing array elements
Lists Rem Collier Room A1.02
CS505 Data Structures and Algorithms
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
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.
Program based on queue & their operations for an application
Stacks.
Stacks and Queues.
Queues Queues Queues.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queue, Deque, and Priority Queue Implementations
Stacks, Queues, and Deques
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.
Queues.
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.
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,
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
More Data Structures (Part 1)
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Stacks and Linked Lists
Presentation transcript:

Queues Rem Collier Room A1.02 School of Computer Science and Informatics University College Dublin, Ireland

Queues: Concept A queue is a container of objects / values. Insertion and removal based on first-in-first-out (FIFO) principle. Objects can be inserted at any time, but only the object that has been in the queue the longest can be removed. Terminology: Items can be enqueued (insertion) or dequeued (removal). The front of the queue is the next item to be dequeued The rear of the queue is where the last item was enqueued NOTE: We insert and remove from different places!

Example: Web spider Search engines (e.g. Google) use programs called spiders to discover new pages on the web Input = a seed page Repeatedly Parse the page and extract hyperlinks Follow one of the hyperlinks and go to 1 Spiders employ a Queue to store & select hyperlinks newly discovered pages next page to be explored

Queues: Functional Specification Core Operations: enqueue(v): Inserts value v onto rear of queue dequeue(): Removes the value at the front of the queue and returns it front(): Return the value at the front of the queue, without removing it Support Operations: size(): Returns the number of objects in the queue isEmpty(): Return a boolean indicating if the queue is empty.

Queues: Java Interface public interface Queue<T> { public void enqueue(T value); public T dequeue(); public T front(); public boolean isEmpty(); public int size(); }

Queues: Impl. Strategies Array-based Implementation: Array holds the objects pushed onto the queue Insertion begins at index 0. Auxiliary values needed to keep track of the “front” and “rear” of the queue of the queue. Finite Capacity Link-based Implementation: Objects stored in special “nodes” Nodes maintain ordering information Link to the next object in the stack. Need auxiliary references for “front” and “rear” nodes. Infinite Capacity

Link-based Queues

Link-Based Queue Auxiliary Data Structure: Node Key Nodes / Data: A reference to the object being stored in the queue A link to the next node in the queue (this is the link). Key Nodes / Data: The “front” node of the queue The “rear” node of the queue Need to keep track of the “size” of the queue front rear  front  rear Ireland France Empty Queue Queue of size 2

Link-Based Queue Algorithm enqueue(o): Input: an object o Output: none node  new Node(o) if (rear = null) then front  node else rear.next  node rear  node size  size + 1 Algorithm size(): Input: none Output: count of objects on the stack return size Algorithm isEmpty(): Output: true if the stack is empty, false otherwise return size = 0 Algorithm dequeue(): Input: none Output: the top object T  front front  front.next if (front = null) then rear  null else T.next  null size  size-1 return T.element Algorithm front(): return front.element

Link-Based Queues: Dry Runs View operations as atomic Show the state of the Linked List after each operation Example: enqueue(“France”) front rear  front  rear enqueue(“Ireland”) France front rear  front rear  France Ireland dequeue() Ireland

Link-Based Queues: Impl. Class name: LinkedQueue Fields: An inner class Node (see Worksheet) An integer, size (number of objects in the stack) Two Node fields, front and rear Constructors Default Constructor (sets front and rear to null and size to 0) Methods: 1 per operation: methods names should match operation names Implement methods based on pseudo code This is part of your next worksheet

Link-Based Queues: Analysis Operation Running Times: Issues: What happens if we dequeue from an empty queue? Operation Running Time enqueue(o) O(1) dequeue() front() isEmpty() size()

Array-based Queues

Naïve Array-Based Queue Create a queue using an array by specifying a maximum size N for our stack, e.g., N = 1000. The queue consists of: an N-element array Q and Two integer variables front and rear, the index of the front and rear elements in array Q respectively. Illustration: rear 1 2 3 4 5 6 7 8 9 Q U E Q What if rear == N-1? front

Circular Arrays Naïve approach suffers from wasted space: Dequeued indices are not reused. Could do an array copy with every dequeue – O(n)! Solution: Logically join the first and last cells in the array: When front (or rear) reaches the end of the array, perform a “wraparound” by setting it to 0… Rear N - 1 Front

Circular Array Queue Size In the basic configuration, we can calculate the number of items in the queue as follows: Size = rear - front In a wrap-around configuration, this equation gives us a negative value whose value represents the number of empty positions in the array… By adding N on to this value we get the number of filled positions in the array! Hence, for configuration 2: Size = N + rear - front Finally, we can use the fact the N mod N = 0 to show that, in either case, the size of the queue can be found as follows: Size = (N + rear - front) mod N

Array-Based Queue Algorithm enqueue(o): Input: an object o Output: none Q[rear]  o rear  (rear + 1) % N Algorithm size(): Input: none Output: count of objects on the stack return (rear – front) % N Algorithm isEmpty(): Output: true if the stack is empty, false otherwise return rear = front Algorithm dequeue(): Input: none Output: the front object e  Q[front] Q[front]  null front  (front+1) % N return e Algorithm front(): Output: the top object return Q[front]

Array-Based Queues: Dry Runs View operations as atomic Show the state of the array, S, and top element index, t, after each operation Example: operation f r 1 2 3 4 5 6 7 8 9 Initial State enqueue(H) H enqueue(A) A dequeue() enqueue(P) P

Array-Based Queues: Impl. Class name: ArrayQueue Fields: An array of objects, Q An integer, N (array size) Two integers, front and rear Constructors Default Constructor (sets N to 1000) Constructor with 1 integer parameter (used to set value of N) Methods: 1 per operation: methods names should match operation names – except for naming conventions (lower case first letter) Implement methods based on pseudo code This is part of your next worksheet

Array-Based Queues: Analysis Operation Running Times: Issues: What happens if we dequeue from an empty queue? What happens if we enqueue on to a full queue? Which Implementation Strategy is better? Operation Running Time LBQ Running Time enqueue(o) O(1) dequeue() front() isEmpty() size()

Deques

Deque: Concept A deque (pronounced “deck”) is a container of objects / values. Insertion and removal based on the first or last-in first or last-out (FLI-FLO) principle. Terminology: Items can be “inserted” at the front or back of the deque. Items can be “removed” at the front or back of the deque NOTE: We insert and remove from both the front and the back.

Deques: Functional Specification Core Operations: insertFirst(o): Inserts object o onto front of the deque insertLast(o): Inserts object o onto the rear of the deque removeFirst(): Removes the object at the front of the deque and returns it removeLast(): Removes the object at the rear of the deque and returns it Support Operations: size(): Returns the number of objects in the deque isEmpty(): Return a boolean indicating if the deque is empty front(): Return the front object in the deque rear(): Return the rear object in the deque

Deques: Impl. Strategies Array-based Implementation: Use a circular array Need to handle transitions between normal and wraparound modes. Finite Capacity Link-based Implementation: Objects stored in special “nodes” Nodes maintain ordering information Link to the next and previous objects in the deque. Need auxiliary references for “front” and “rear” nodes. Infinite Capacity

Doubly Linked Lists In a Doubly Linked List, the objects are stored in nodes. Each node maintains: A reference to the object A reference to the next node in the list A reference to the previous node in the list Again, we store references to “key” nodes / entry points. These provide us with a way of accessing the list prev element next “Rome”

Link-Based Deque Auxiliary Data Structure: Node Key Nodes / Data: A reference to the object being stored in the deque A link to the next node in the deque Key Nodes / Data: The “front” node of the deque The “rear” node of the deque Need to keep track of the “size” of the deque front rear   front  rear Ireland France Empty Deque Deque of size 2