7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.

Slides:



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

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.
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
The ADT Stack Definition
queues1 Queues Data structures that wait their turn.
Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
COMP 103 Linked Stack and Linked Queue.
8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays.
Stacks, Queues & Deques CSC212.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
7.2 Priority Queue ADTs Priority queue concepts
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
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.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
11 Map ADTs  Map concepts  Map applications  A map ADT: requirements, contract.  Implementations of maps: using key-indexed arrays, entry arrays, linked-lists,
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
List Interface and Linked List Mrs. Furman March 25, 2010.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
9 Set ADTs  Set concepts  Set applications  A set ADT: requirements, contract  Implementations of sets: using member arrays, linked lists, boolean.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
The Queue ADT.
Linked Lists, Queues, Stacks
Computer Engineering Department Islamic University of Gaza
11 Map ADTs Map concepts. Map applications.
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.
Double-Ended Queues Chapter 5.
Queues Rem Collier Room A1.02
Priority Queue.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues.
Queue.
Chapter 13 Collections.
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 CSC212.
8 List ADTs List concepts. List applications.
Presentation transcript:

7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues in the Java class library © 2008 David A Watt, University of Glasgow Algorithms & Data Structures (M)

7-2 Queue concepts  A queue is a first-in-first-out sequence of elements. Elements can added only at one end (the rear of the queue) and removed only at the other end (the front of the queue).  The size (or length) of a queue is the number of elements it contains.

7-3 Example: bus queue  Consider a queue of persons at a bus-stop: BUS STOP

7-4 Queue applications  Print server – Uses a queue of print jobs.  Operating system – Disk driver uses a queue of disk input/output requests. – Scheduler uses a queue of processes awaiting a slice of processor time.

7-5 Example: demerging (1)  Consider a file of person records, each of which contains a person ’ s name, gender, birth-date, etc. The records are sorted by birth-date. We are required to rearrange the records such that females precede males but they remain sorted by birth-date within each gender group.  Bad idea: use a sorting algorithm. Time complexity is O(n log n) at best.  Good idea: use a demerging algorithm. Time complexity is O(n).

7-6 Example: demerging (2)  Demerging algorithm: To copy a file of person records from input to output, rearranged such that females precede males but their order is otherwise unchanged: 1.Make queues females and males empty. 2.For each person p in input, repeat: 2.1.If p is female, add p at the rear of females. 2.2.If p is male, add p at the rear of males. 3.While females is not empty, repeat: 3.1.Remove a person f from the front of females. 3.2.Write f to output. 4.While males is not empty, repeat: 4.1.Remove a person m from the front of males. 4.2.Write m to output. 5.Terminate.

7-7 Queue ADT: requirements  Requirements: 1)It must be possible to make a queue empty. 2)It must be possible to test whether a queue is empty. 3)It must be possible to obtain the size of a queue. 4)It must be possible to add an element at the rear of a queue. 5)It must be possible to remove the front element from a queue. 6)It must be possible to access the front element in a queue without removing it.

7-8 Queue ADT: contract (1)  Possible contract for homogeneous queues (expressed as a Java generic interface): public interface Queue { // Each Queue object is a homogeneous queue // whose elements are of type E. /////////////// Accessors /////////////// public boolean isEmpty (); // Return true if and only if this queue is empty. public int size (); // Return this queue’s size. public E getFirst (); // Return the element at the front of this queue.

7-9 Queue ADT: contract (2)  Possible contract (continued): ////////////// Transformers ////////////// public void clear (); // Make this queue empty. public void addLast (E it); // Add it as the rear element of this queue. public E removeFirst (); // Remove and return the front element of this queue. }

7-10 Implementation of queues using arrays (1)  Consider representing a bounded queue (size  cap) by: –variables size, front, rear –an array elems of length cap, containing the elements in elems[front…rear–1]. Empty queue: 0cap–1front=rear Invariant: element 0frontrear–1cap–1 unoccupiedfront elementrear element

7-11 Initially: front 0 rear elems 0 size 0 Homer 1 Marge 2 Maggie 3 Lisa 45 0 front 4 rear elems 4 size After adding Homer, Marge, Maggie, Lisa: 0 Homer 1 Marge 2 Maggie 3 Lisa 4 Bart 5 0 front 5 rear elems 5 size After adding Bart: 01 Marge 2 Maggie 3 Lisa 4 Bart 5 1 front 5 rear elems 4 size After removing the front element: 012 Maggie 3 Lisa 4 Bart 5 2 front 5 rear elems 3 size After removing the front element: 012 Maggie 3 Lisa 4 Bart 5 Ralph 2 front 0 rear elems 4 size After adding Ralph: Implementation of queues using arrays (2)  Animation (with cap = 6):

7-12 Implementation of queues using arrays (3)  Once the rightmost array slot is occupied, no more elements can be added, unless we shift elements to fill up any unoccupied leftmost slots.  But then operation addLast would have time complexity O(n), rather than O(1).  We can avoid this if we use a “cyclic array” instead of an ordinary array.

7-13 Cyclic arrays  In a cyclic array a of length n, every slot has both a successor and a predecessor. In particular: –the successor of a[n–1] is a[0] –the predecessor of a[0] is a[n–1].  Visualizing a cyclic array (of length 8): or

7-14 Implementation of queues using cyclic arrays (1)  Represent a bounded queue (size  cap) by: –variables size, front, rear –a cyclic array elems of length cap, containing the elements either (a) in elems[front…rear–1] or (b) in elems[front…cap–1] and elems[0…rear–1]. cap–1 front rear–1 0 (b) element cap–1 front rear–1 0 Invariant: (a) element Empty queue: 0cap–1front=rear

7-15 Initially: 0 front 0 rear elems 0 size Implementation of queues using cyclic arrays (2)  Animation (with cap = 6): HomerMargeMaggieLisa 0 front 4 rear elems 4 size After adding Homer, Marge, Maggie, Lisa: HomerMargeMaggieLisaBart 0 front 5 rear elems 5 size After adding Bart: MargeMaggieLisaBart 1 front 5 rear elems 4 size After removing the front element: MaggieLisaBart 2 front 5 rear elems 3 size After removing the front element: MaggieLisaBartRalph 2 front 0 rear elems 4 size After adding Ralph: NelsonMaggieLisaBartRalph 2 front 1 rear elems 5 size After adding Nelson: NelsonMartinMaggieLisaBartRalph 2 front 2 rear elems 6 size After adding Martin: NelsonMartinLisaBartRalph 3 front 2 rear elems 5 size After removing the front element:

7-16 Implementation of queues using cyclic arrays (3)  Java implementation: public class ArrayQueue implements Queue { private E[] elems; private int size, front, rear; /////////////// Constructor /////////////// public ArrayQueue (int cap) { elems = (E[]) new Object[cap]; size = front = rear = 0; }

7-17 Implementation of queues using cyclic arrays (4)  Java implementation (continued): /////////////// Accessors /////////////// public boolean isEmpty () { return (size == 0); } public int size () { return size; } public E getFirst () { if (size == 0) throw … ; return elems[front]; }

7-18 Implementation of queues using cyclic arrays (5)  Java implementation (continued): ////////////// Transformers /////////////// public void clear () { size = front = rear = 0; } public void addLast (E it) { if (size == elems.length) … elems[rear++] = it; if (rear == elems.length) rear = 0; size++; } NB

7-19 Implementation of queues using cyclic arrays (6)  Java implementation (continued): public E removeFirst () { if (size == 0) throw … ; E frontElem = elems[front]; elems[front++] = null; if (front == elems.length) front = 0; size--; return frontElem; } }  Analysis: –All operations have time complexity O(1). NB

7-20 Implementation of queues using SLLs (1)  Represent an (unbounded) queue by: –an SLL, whose header contains links to the first node (front) and last node (rear). –a variable size (optional). Invariant: element front rear size Empty queue: front rear size 0 Illustration: Homer Marge Maggie Lisa front rear size 4

7-21 Implementation of queues using SLLs (2)  Java implementation: public class LinkedQueue implements Queue { private Node front, rear; private int size; /////////////// Inner class /////////////// private static class Node { public E element; public Node succ; public Node (E x, Node s) { element = x; succ = s; } }

7-22 Implementation of queues using SLLs (3)  Java implementation (continued): /////////////// Constructor /////////////// public LinkedQueue () { front = rear = null; size = 0; } /////////////// Accessors /////////////// public boolean isEmpty () { return (front == null); }

7-23 Implementation of queues using SLLs (4)  Java implementation (continued): public int size () { return size; } public E getFirst () { if (front == null) throw … ; return front.element; }

7-24 Implementation of queues using SLLs (5)  Java implementation (continued): ////////////// Transformers /////////////// public void clear () { front = rear = null; size = 0; } public void addLast (E it) { Node newest = new Node(it, null); if (rear != null) rear.succ = newest; else front = newest; rear = newest; size++; }

7-25 Implementation of queues using SLLs (6)  Java implementation (continued): public E removeFirst () { if (front == null) throw … ; E frontElem = front.element; front = front.succ; if (front == null) rear = null; size--; return frontElem; } }  Analysis: –All operations have time complexity O(1).

7-26 Queues in the Java class library  The library interface java.util.Queue is similar to the above interface Queue.  The library class java.util.LinkedList implements java.util.Queue, representing each queue by a doubly-linked-list. (This is overkill!)  Illustration: import java.util.*; Queue busQ = new LinkedList (); busQ.addLast(homer); busQ.addLast(marge); busQ.addLast(maggie); busQ.addLast(lisa); busQ.addLast(bart); Person p = busQ.removeFirst();

7-27 Example: demerging again (1)  Implementation of the demerging algorithm: public static void reSort ( BufferedReader input, BufferedWriter output) throws IOException { // Copy a file of person records from input to output, // rearranged such that females precede males but their // order is otherwise unchanged. Queue females = new LinkedList (), males = new LinkedList (); for (;;) { Person p = readPerson(input); if (p == null) break; // end of input

7-28 Example: demerging again (2)  Implementation (continued): if (p.female) females.addLast(p); else males.addLast(p); } while (! females.isEmpty()) { Person f = females.removeFirst(); writePerson(output, f); } while (! males.isEmpty()) { Person m = males.removeFirst(); writePerson(output, m); } }