Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.

Slides:



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

© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant)
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
CHAPTER 7 Queues.
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.
Data Structure Dr. Mohamed Khafagy.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
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 are queues? Queue Implementation Using Linked Lists. Applications of Queues.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Implementing and Using Stacks
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 Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues What is a Queue? Queue Implementations: Queue As Array
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
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.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
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.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
© 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,
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.
Recap: Solution of Last Lecture Activity
Queues Queues Queues.
Queues What is a queue? Queue Implementations: As Array
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.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
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,
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
Queues What is a Queue? Queue Implementations: As Array
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
Introduction to Stacks
CE 221 Data Structures and Algorithms
Introduction to Stacks
Queues What is a queue? Queue Implementations: As Array
Data Structures & Programming
Presentation transcript:

Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues

What is a queue? Queues are linear data structures in which we add elements to one end and remove them from the other end. The first item to be en-queued is the first to be de-queued. Queue is therefore called a First In First Out (FIFO) structure. Queue operations: Enqueue Dequeue GetHead

What is a queue? enqueue(1); enqueue(5); dequeue(); RearFront Given the following Queue, how will it change when we apply the given operations?

Queue Implementation In our implementation, a queue is a container that extends the AbstractContainer class and implements the Queue interface. We provide three implementations for Queue –QueueAsArray –QueueAsCircularArray –QueueAsLinkedList public interface Queue extends Container{ public abstract Object getHead(); public abstract void enqueue(Object obj); public abstract Object dequeue(); }

QueueAsArray public class QueueAsArray extends AbstractContainer implements Queue { protected Object[] array; protected int rear = 0; protected int size; public QueueAsArray(int size) { array = new Object[size]; this.size = size; } public void purge(){ int index = 0; while(count > 0){ array[index] = null; index++; count--; } rear = 0; } Complexity is O(n)

QueueAsArray (Cont.) public Object getHead(){ if(count == 0) throw new ContainerEmptyException(); else return array[0]; } public void enqueue(Object obj){ if(count == size){ throw new ContainerFullException(); } else{ array[rear++] = obj; count++; } Complexity is O(1)

QueueAsArray (Cont.) public Object dequeue(){ if(count == 0) throw new ContainerEmptyException(); else { Object obj = array[0]; count--; for(int k = 1; k <= count; k++) array[k - 1] = array[k]; rear--; return obj; } Complexity is O(n)

QueueAsArray (Cont.) public Iterator iterator() { return new Iterator() { int index = 0; public boolean hasNext(){ return index < count; } public Object next(){ if(index == count) throw new NoSuchElementException(); else { Object obj = array[index++]; return obj; } }; }

QueueAsCircularArray Implementation By using modulo arithmetic for computing array indexes, we can have a queue implementation in which each of the operations enqueue, dequeue, and getHead has complexity O(1) Enqueue(“P”) will result in …

QueueAsCircularArray Implementation (Cont.) Dequeue() will result in

QueueAsCircularArray public class QueueAsCircularArray extends AbstractContainer implements Queue { protected Object[] array; protected int front = 0; protected int rear = 0; protected int size; public QueueAsCircularArray(int size) { array = new Object[size]; this.size = size; } public void purge(){ int index = front; while(count > 0){ array[index] = null; index = (index + 1) % size; count--; } front = rear = 0; } Complexity is O(n)

QueueAsCircularArray public Object getHead(){ if(count == 0) throw new ContainerEmptyException(); else return array[front]; } public void enqueue(Object obj){ if(count == size) throw new ContainerFullException(); else { array[rear] = obj; rear = (rear + 1) % size; count++; } public Object dequeue(){ if(count == 0)throw new ContainerEmptyException(); else { Object obj = array[front]; front = (front + 1) % size; count--; return obj; } Complexity is O(1)

QueueAsCircularArray public Iterator iterator(){ return new Iterator() { int index = front; int counter = 0; public boolean hasNext(){ return counter < count; } public Object next(){ if(counter == count) throw new NoSuchElementException(); else { Object obj = array[index]; index = (index + 1) % size; counter++; return obj; } }; }

QueueAsLinkedList public class QueueAsLinkedList extends AbstractContainer implements Queue { protected MyLinkedList list; public QueueAsLinkedList(){list = new MyLinkedList();} public void purge(){ list.purge(); count = 0; } public Object getHead(){ if(count == 0) throw new ContainerEmptyException(); else return list.getFirst(); } Complexity is O(1)

QueueAsLinkedList public void enqueue(Object obj){ list.append(obj); count++; } public Object dequeue(){ if(count == 0) throw new ContainerEmptyException(); else { Object obj = list.getFirst(); list.extractFirst(); count--; return obj; } Complexity is O(1)

QueueAsLinkedList public Iterator iterator() { return new Iterator() { MyLinkedList.Element position = list.getHead(); public boolean hasNext(){ return position != null; } public Object next(){ if(position == null) throw new NoSuchElementException(); else{ Object obj = position.getData(); position = position.getNext(); return obj; } }; }

Application of Queues Direct applications –Waiting lines: Queues are commonly used in systems where waiting line has to be maintained for obtaining access to a resource. For example, an operating system may keep a queue of processes that are waiting to run on the CPU. –Access to shared resources (e.g., printer) –Multiprogramming Indirect applications –Auxiliary data structure for algorithms –Component of other data structures

Priority Queues In a normal queue the enqueue operation add an item at the back of the queue, and the dequeue operation removes an item at the front of the queue. A priority queue is a queue in which the dequeue operation removes an item at the front of the queue; but the enqueue operation insert items according to their priorities. A higher priority item is always enqueued before a lower priority element. An element that has the same priority as one or more elements in the queue is enqueued after all the elements with that priority.