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

Slides:



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

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
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.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.
9-1 9 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.
Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.
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:
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.
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:
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.
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.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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.
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.
Chapter 5 Linked Lists II
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown.
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.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
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
Stacks.
Priority Queue.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Chapter 16-2 Linked Structures
Queues.
Queue.
ADT list.
Chapter 16 Linked Structures
8 List ADTs List concepts. List applications.
CMPT 225 Lecture 8 – Queue.
Presentation transcript:

7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues in the Java class library. © 2001, D.A. Watt and D.F. Brown

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 length of a queue is the number of elements it contains. An empty queue has length zero.

7-3 Queue concepts (continued) Illustration (queue of persons): BUS STOP

7-4 Queue applications Print server  maintains a queue of print jobs. Disk driver  maintains a queue of disk input/output requests. Scheduler (e.g., in an operating system)  maintains a queue of processes awaiting a slice of machine time.

7-5 Example 1: demerging (1) Consider a file of person records, each of which contains a person’s name, gender, date of birth, etc. The records are sorted by date of birth. We are required to rearrange the records such that females precede males but they remain sorted by date of birth 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 1 (2) Demerging algorithm: To rearrange a file of person records such that females precede males but their order is otherwise unchanged: 1.Make queues females and males empty. 2.Until the input file is empty, repeat: 2.1.Let p be the next person read in from the file. 2.2.If p is female, add p to the rear of females. 2.3.If p is male, add p to the rear of males. 3.Until females is empty, repeat: 3.1.Write out the person removed from the front of females. 4.Until males is empty, repeat: 4.1.Write out the person removed from the front of males. 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 length 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, expressed as a Java interface: public interface Queue { // Each Queue object is a queue whose elements are objects. /////////////// Accessors /////////////// public boolean isEmpty (); // Return true if and only if this queue is empty. public int size (); // Return this queue’s length. public Object 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 (Object elem); // Add elem as the rear element of this queue. public Object removeFirst (); // Remove and return the front element of this queue. }

7-10 Invariant: 0frontrear–1maxlen–1 element Implementation of queues using arrays (1) Consider representing a bounded queue (length  maxlen) by:  a variable length, containing the current length  variables front and rear  an array elems of length maxlen, containing the queued elements in elems[front…rear–1]: Empty queue: 0maxlen–1front=rear unoccupied rear elementfront element

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

7-12 Implementation using arrays (3) If we must shift elements along the array, operations addLast and removeFirst will have time complexity O(n). 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 component 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]. Alternative ways of visualizing a cyclic array (length 8):

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

7-15 Implementation using cyclic arrays (2) Animation (with maxlen = 6): Initially: front 0 rear elems 0 length 0 Homer 1 Marge 2 Bart 3 Lisa 45 0 front 4 rear elems 4 length After adding Homer, Marge, Bart, Lisa: 0 Homer 1 Marge 2 Bart 3 Lisa 4 Maggie 5 0 front 5 rear elems 5 length After adding Maggie: 01 Marge 2 Bart 3 Lisa 4 Maggie 5 1 front 5 rear elems 4 length After removing the front element: 012 Bart 3 Lisa 4 Maggie 5 2 front 5 rear elems 3 length After removing the front element: 012 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 0 rear elems 4 length After adding Ralph: 0 Nelson 12 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 1 rear elems 5 length After adding Nelson: 0 Nelson 1 Martin 2 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 2 rear elems 6 length After adding Martin: 0 Nelson 1 Martin 23 Lisa 4 Maggie 5 Ralph 3 front 2 rear elems 5 length After removing the front element:

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

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

7-18 Implementation using cyclic arrays (5) Java implementation (continued): /////////////// Transformers /////////////// public void clear () { for (int i = 0; i < elems.length; i++) elems[i] = null; front = rear = length = 0; } public void addLast (Object elem) { if (length == elems.length) throw … ; elems[rear++] = elem; if (rear == elems.length) rear = 0; length++; }

7-19 Implementation using cyclic arrays (6) Java implementation (continued): public Object removeFirst () { if (length == 0) throw … ; Object frontElem = elems[front]; elems[front++] = null; if (front == elems.length) front = 0; return frontElem; } } Analysis:  Most operations have time complexity O(1).  Operation clear has time complexity O(maxlen).

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

7-21 Implementation using SLLs (2) Java implementation: public class LinkedQueue implements Queue { private SLLNode front, rear; /////////////// Constructor /////////////// public LinkedQueue () { front = rear = null; } /////////////// Accessors /////////////// public boolean isEmpty () { return (front == null); }

7-22 Implementation using SLLs (3) Java implementation (continued): public int size () { int length = 0; for (SLLNode curr = front; curr != null; curr = curr.succ) length++; return length; } public Object getFirst () { if (front == null) throw … ; return front.element; }

7-23 Implementation using SLLs (4) Java implementation (continued): /////////////// Transformers /////////////// public void clear () { front = rear = null; } public void addLast (Object elem) { SLLNode newest = new SLLNode(elem, null); if (rear != null) rear.succ = newest; else front = newest; rear = newest; }

7-24 Implementation using SLLs (5) Java implementation (continued): public Object removeFirst () { if (front == null) throw … ; Object frontElem = front.element; front = front.succ; if (front == null) rear = null; return frontElem; } } Analysis:  Most operations have time complexity O(1), but size is O(n).  However, size too would be O(1) if we used a variable length.

7-25 Queues in the Java class library Java provides no Queue interface or class as such. However, the java.util.LinkedList class provides all the above Queue operations. Illustration: import java.util.LinkedList; LinkedList queue = new LinkedList(); queue.addLast("Homer"); queue.addLast("Marge"); queue.addLast("Bart"); queue.addLast("Lisa"); queue.addLast("Maggie"); System.out.println(busQueue.removeFirst());

7-26 Example 2: demerging revisited (1) Recall the demerging algorithm of Example 1. Implementation: public static void reSortPersons ( BufferedReader input, BufferedWriter output) throws IOException { LinkedList femaleQueue = new LinkedList(); LinkedList maleQueue = new LinkedList(); for (;;) { Person p = readPerson(input); if (p == null) break; // end of input if (p.female) femaleQueue.addLast(p); else maleQueue.addLast(p); }

7-27 Example 2 (2) Implementation (continued): while (! femaleQueue.isEmpty()) { Person f = femaleQueue.removeFirst(); writePerson(output, f); } while (! maleQueue.isEmpty()) { Person m = maleQueue.removeFirst(); writePerson(output, m); } }