Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
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.
queues1 Queues Data structures that wait their turn.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Data Structures for Media Queues. Queue Abstract Data Type Queue Abstract Data Type Sequential Allocation Sequential Allocation Linked Allocation Linked.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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,
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structure Dr. Mohamed Khafagy.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
 Balancing Symbols 3. Applications
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks And Queues Chapter 18.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CE 221 Data Structures and Algorithms
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.,
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
 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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Elementary Data Structures
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
UNIT II Queue.
Program based on queue & their operations for an application
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Data Structures Interview / VIVA Questions and Answers
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
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,
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Presentation transcript:

Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain

introduction A queue is a first-in-first-out (FIFO) sequential data structure in which elements are added (enqueued) at one end (the back) and elements are removed (dequeued) at the other end (the front). A good example is a queue. We encounter queues all the time in every day life. What makes a queue a queue? What is the essence of queueness? BA FrontRear

Queue Concept

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.

Operation on Queue using array Insert Delete Empty full

Queue First-in, First-out (FIFO) structure Operations ◦ insertqueue: insert element at rear ◦ deletequeue: remove & return front element ◦ empty: check if the queue has no elements Sample use ◦ handling requests and reservations

Array Implementation of Queues An Object array and two integers ◦ front: index of first element in queue ◦ rear: index of first FREE element in queue... front rear 0 4

ArrayQueue public class ArrayQueue implements Queue { Object store[]; int front, rear; static final int MAX = 100; public ArrayQueue() { store = new Object[MAX]; front = rear = 0; } //... }

Check for Empty and Enqueue public class ArrayQueue implements Queue { //... public boolean empty() { // queue is empty if first element // is the same as the first free element return ( front == rear ); } public void enqueue( Object o ) { // put data in first free slot // move rear index to next slot if ( rear < MAX ) store[rear++] = o; } //... }... front rear 0 4

Dequeue Operation public class ArrayQueue implements Queue { //... public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { return store[front++]; }... front rear 0 4

Dequeue that allows GC public class ArrayQueue implements Queue { // … public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { Object data = store[front]; // get front data // set pointer to null, so it can be garbage-collected // later (when caller code doesn’t let’s go of pointer store[front] = null; front++; return data; }... front rear 0 4

Circular Array Suppose many enqueue operations followed by many dequeue operations Result: rear approaches MAX but the queue is not really full Solution: Circular Array ◦ allow rear (and front) to “wrap around” the array (if rear = MAX-1, incrementing rear means resetting it to 0)

Alternative Alternative ways of visualizing a cyclic array (length 8)

Empty operation Return (queue_of_front == queue_of_rear)

Remove operation If (empty(queue) { Print(“underflow); exit; } If (queue_of_front == max-1) queue_of_front =0; Else (queue_of_front)++; Return (queue_items[queue_of_front]);

Insert operation If(queue_of_rear == max-1) Queue_of_rear=0; Else (queue_of_rear)++; If (queue_of_rear ==queue_of_front) { Print(“queue overflow”); (queue_of_rear)--; exit; } Queue_items[queue_of_rear]=x;

Circular Array, continued When is the array full? ◦ Simple answer: when (rear == front) ◦ Problem: this is the same condition as empty Solution: Reserve a slot ◦ full: when ( (rear+1) % MAX == front) (one free slot left) ◦ empty: when ( rear == front ) Note: “wastes” a slot ◦ alternative: have a boolean field called hasElements ◦ full: when ( hasElements && (rear == front)) ◦ But not really better  hasElements takes up extra space too  Also, need to take care of hasElements in enqueue and dequeue

Revised Enqueue public class ArrayQueue implements Queue { //... public void enqueue( Object o ) { if ( (rear + 1) % MAX != front ) { store[rear] = o; rear = (rear + 1) % MAX; } //... }

Linked List Implementation front null rear

The ObjectNode Class public class ObjectNode { Object data; ObjectNode next; public void setData( Object o ) { data = o; } public Object getData() { return data; } public void setNext( ObjectNode p ) { next = p; } public ObjectNode getNext() { return next; } }

Enqueue front null rear null

Dequeue front null rear return this object

The LinkedQueue Class public class LinkedQueue implements Queue { ObjectNode front; ObjectNode rear; public LinkedQueue() { front = rear = null; } // other Queue methods }

Applications Queue for bill submission Play list of audio songs. Printer job scheduling ◦ Round Robin scheduler Joseph problem discussed in book.

Performance the time needed to add or delete an item is constant and independent of the number of items in the queue. both addition and deletion as an O(1) operation. For any given real machine+operating system+language combination, addition may take c1 seconds and deletion c2 seconds, but we aren't interested in the value of the constant, it will vary from machine to machine, language to language, etc. The key point is that the time is not dependent on n - producing O(1) algorithms. O(1) methods are already very fast, and it's unlikely that effort expended in improving such a method will produce much real gain!

Priority Queues Often the items added to a queue have a priority associated with them: this priority determines the order in which they exit the queue - highest priority items are removed first. This situation arises often in process control systems. Imagine the operator's console in a large automated factory. occasionally something breaks or fails and alarm messages are sent. These have high priority because some action is required to fix the problem

Priority Queue The priority queue is a data structure in which th natural ordering of the elements does determine the results of its basic operations. Two types of priority queue ◦ Ascending ◦ Descending Stack can be view as descending priority queue, whose element are ordered by time of insertion. Queue is ascending priority queue.

Array implementation of priority Queue The delete operation in ascending priority queue. This raise two issues ◦ Locate smallest element, Every element of the array must examined. ◦ How element in the middle of array be deleted. Priority queue deletion requires both searching and deletion.

Solutions 1. An empty indicator can be placed into deleted position. can be value or separate field. Disadvantages are search process to locate max or min examine all deleted positions. 2. Each deletion campact the array by shiffting all elements. So the deletion become so inefficient. 3. Maintain an array as an ordered circuler array.this method moves the work of searching and shiffting from deletion operation to insertion.

Solution In the last solution array is sorted and searching is half expensive as compared to unordered array.

Link implementation of priority queue Home work/Lab

Disadvantage by link list Link list occupies more storage than a corresponding element in an array. Time spent in Management of available list