Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

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.
Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Fundamentals of Python: From First Programs Through Data Structures
Lecture - 9 On Queues. Prepared by, Jesmin Akhter, Lecturer, IIT,JU QUEUES A Queue is a linear list of elements in which deletions can take place only.
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.
Queues Objectives: Describe a queue
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Data Structure Dr. Mohamed Khafagy.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
DATA STRUCTURE & ALGORITHMS
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CHP-4 QUEUE.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Stacks, Queues & Recursion
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Queues EENG212 Algorithm And Data Structures. DEFINITION OF QUEUE A Queue is an ordered collection of items from which items may be deleted at one end.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Implementation of QUEUE For more notes and topics visit: eITnotes.com.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Queue Queue: –Like any other data structure (apart from Array and Linked List), Queue also can be implemented, –Either as an Array or, –As a Linked List.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
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.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CE 221 Data Structures and Algorithms
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
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.
 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.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
CC 215 Data Structures Queue ADT
Program based on queue & their operations for an application
Chapter 15 Lists Objectives
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Algorithm and Data Structure Part III Dr. Naimah Yaakob
Stack and Queue APURBO DATTA.
QUEUE.
Queues Mohammad Asad Abbasi Lecture 5
Stack and Queue.
DATA STRUCTURE QUEUE.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues FIFO Enqueue Dequeue Peek.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
LINEAR DATA STRUCTURES
Presentation transcript:

Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples

Queue It is a linear data structure used to represent a linear list and permits deletion to be performed at one end and of the list and the insertions at the other end. The information in such a list is processed in the same order as it was received. i.e-FIRST-COME-FIRST-SERVE basis(FCFS). Or FIRST IN FIRST OUT(FIFO). 2

Queue items [MAXQUEUE- 1] items[2]C items[1]B items[0]A Front=0 Rear=2 3

Declaration of a Queue # define MAXQUEUE 50 /* size of the queue items*/ typedef struct { int front; int rear; int items[MAXQUEUE]; }QUEUE; 4

Implementation of queue. Two common ways in which queues may be implemented are as follows: ARRAYS POINTERS(one way linear linked list) 5

Operations of queue Insertion in queue. Deletion in queue. List(display) of the queue. 6

Different type of queue 1. Circular queue 2. Double Ended Queue 3. Priority queue 7

1.Circular queue Let we have an array named Q, that contains n element in which Q[1] comes after Q[n] in the array. When this technique is used to construct a queue is called circular queue. In other word we can say that a queue is called circular when the last room comes just before the first room. 8

Circular queue…. Q[1] Q[2] Q[3]... Q[n-1] Q[n] 9

Queue cont…. In a circular queue when rear=n, if we insert an element then this element is assigned to q[1] instead of increasing rear to n+1. Suppose queue contains only one element that is front=rear!=0 and suppose that the element is removed then the front and rear pointers are now assigned ‘0’ to indicate that the queue is EMPTY. 10

Application of queue An e.g. of queue is time sharing computer system where many users share the system simultaneously. The procedure, which is used to design such type of system, is Round Robin Technique. The railway reservation counter is also an example of queue where the people collect their tickets on FIFO or FCFS based. 11

TYPES OF QUEUES Suppose a queue Q has maximum size 5, say 5 elements pushed and 2 elements popped. There are three major variations in a simple queue. 1. Circular queue 2. Double ended queue (de-queue) 3. Priority queue Priority queue is generally implemented using linked list so we discussed it later. 12

CIRCULAR QUEUE In circular queues the elements Q[0],Q[1],Q[2].... Q[n – 1] is represented in a circular fashion with Q[1] following Q[n]. A circular queue is one in which the insertion of a new element is done at the very first location of the queue if the last location at the queue is full.  Suppose Q is a queue array of 6 elements.  Push and pop operation can be performed on circular. 13

Cont…..  After inserting an element at last location Q[5], the next element will be inserted at the very first location (i.e., Q[0]).  Circular queue is one in which the first element comes just after the last element. 14

Cont…… At any time the position of the element to be inserted will be calculated by the relation Rear = (Rear + 1) % SIZE After deleting an element from circular queue the position of the front end is calculated by the relation Front= (Front + 1) % SIZE After locating the position of the new element to be inserted, rear, compare it with front. If (rear = front), the queue is full and cannot be inserted anymore. 15

ALGORITHMS Inserting an element to circular Queue 1. Initialize FRONT = – 1; REAR = REAR = (REAR + 1) % SIZE 3. If (FRONT is equal to REAR) (a) Display “Queue is full” (b) Exit 4. Else (a) Input the value to be inserted and assign to variable “DATA” 5. If (FRONT is equal to – 1) (a) FRONT = 0 (b) REAR = 0 6. Q[REAR] = DATA 7. Repeat steps 2 to 5 if we want to insert more elements 8. Exit 16

Cont… Deleting an element from a circular queue 1. If (FRONT is equal to – 1) (a) Display “Queue is empty” (b) Exit 2. Else (a) DATA = Q[FRONT] 3. If (REAR is equal to FRONT) (a) FRONT = –1 (b) REAR = –1 4. Else (a) FRONT = (FRONT +1) % SIZE 5. Repeat the steps 1, 2 and 3 if we want to delete more elements 6. Exit 17

2.DEQUES A deque is a homogeneous list in which elements can be added or inserted and deleted or removed from both the ends. We can add a new element at the rear or front end and also we can remove an element from both front and rear end. Hence it is called Double Ended Queue. 18

Cont…. There are two types of deque depending upon the restriction to perform insertion or deletion operations at the two ends. 1. Input restricted deque 2. Output restricted deque An input restricted deque is a deque, which allows insertion at only 1 end, rear end, but allows deletion at both ends, rear and front end of the lists. An output-restricted deque is a deque, which allows deletion at only one end, front end, but allows insertion at both ends, rear and front ends, of the lists. 19

The possible operation performed on deque 1. Add an element at the rear end 2. Add an element at the front end 3. Delete an element from the front end 4. Delete an element from the rear end Only 1 st, 3 rd and 4 th operations are performed by input- restricted deque 1st, 2 nd and 3 rd operations are performed by output- restricted deque. 20

ALGORITHUM Let Q be the array of MAX elements. front (or left) and rear (or right) are two array index (pointers), where the addition and deletion of elements occurred. Let DATA be the element to be inserted. Before inserting any element to the queue left and right pointer will point to the – 1. INSERT AN ELEMENT AT THE RIGHT SIDE OF THE DE-QUEUE 1. Input the DATA to be inserted 2. If ((left == 0 && right == MAX–1) || (left == right + 1)) (a) Display “Queue Overflow” (b) Exit 3. If (left == –1) // if queue is initially empty (a) left = 0 (b) right = 0 4. Else (a) if (right == MAX –1)// right is at last position of queue (i) right = 0 (b) else (i) right = right+1 5. Q[right] = DATA 6. Exit 21

Cont…. INSERT AN ELEMENT AT THE LEFT SIDE OF THE DE-QUEUE 1. Input the DATA to be inserted 2. If ((left == 0 && right == MAX–1) || (left == right+1)) (a) Display “Queue Overflow” (b) Exit 3. If (left == – 1) (a) Left = 0 (b) Right = 0 4. Else (a) if (left == 0) (i) left = MAX – 1 (b) else (i) left = left – 1 5. Q[left] = DATA 6. Exit 22

ALGORITHMS FOR DELETING AN ELEMENT Let Q be the array of MAX elements. front (or left) and rear (or right) are two array index (pointers), where the addition and deletion of elements occurred. DATA will contain the element just deleted. 23

Cont…. DELETE AN ELEMENT FROM THE RIGHT SIDE OF THE DE-QUEUE 1. If (left == – 1) (a) Display “Queue Underflow” (b) Exit 2. DATA = Q [right] 3. If (left == right) //queue has only one element (a) left = – 1 (b) right = – 1 4. Else (a) if(right == 0) (i) right = MAX-1 (b) else (i) right = right-1 5. Exit 24

Cont….. DELETE AN ELEMENT FROM THE LEFT SIDE OF THE DE-QUEUE 1. If (left == – 1) (a) Display “Queue Underflow” (b) Exit 2. DATA = Q [left] 3. If(left == right) (a) left = – 1 (b) right = – 1 4. Else (a) if (left == MAX-1) (i) left = 0 (b) Else (i) left = left Exit 25

Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called the rear and removed from another end, called the front of the list. Two basic operations are associated with queue: 1. “Insert” operation is used to insert an element into a queue. 2. “Delete” operation is used to delete an element from a queue. ● FIFO list Example: Queue: AAA, BBB, CCC, DDD, EEE AAABBBCCCDDDEEE Rear Front EEE DDD CCC BBB AAA Rear Front

09/10/08 Example: Consider the following queue (linear queue). Rear = 4 and Front = 1 and N = (1) Insert 20. Now Rear = 5 and Front = (2) Delete Front Element. Now Rear = 5 and Front = (3) Delete Front Element. Now Rear = 5 and Front = (4) Insert 60. Now Rear = 6 and Front =

Drawback of Linear Queue Once the queue is full, even though few elements from the front are deleted and some occupied space is relieved, it is not possible to add anymore new elements, as the rear has already reached the Queue’s rear most position. Circular Queue This queue is not linear but circular. Its structure can be like the following figure: Figure: Circular Queue having Rear = 5 and Front = 0 In circular queue, once the Queue is full the "First" element of the Queue becomes the "Rear" most element, if and only if the "Front" has moved forward. otherwise it will again be a "Queue overflow" state. 28

Example: Consider the following circular queue with N = Initially, Rear = 0, Front = Insert 10, Rear = 1, Front = Insert 50, Rear = 2, Front = Insert 20, Rear = 3, Front = Insert 70, Rear = 4, Front = Delete front, Rear = 4, Front = 2. Rear Front 29

7. Insert 100, Rear = 5, Front = Insert 40, Rear = 1, Front = Insert 140, Rear = 1, Front = 2. As Front = Rear + 1, so Queue overflow. 10. Delete front, Rear = 1, Front = 3. Front Rear FrontRear Front 11. Delete front, Rear = 1, Front = Delete front, Rear = 1, Front = 5. Rear Front 30

QUEUE OPERATIONS Initialize the queue Insert to the rear of the queue Remove (Delete) from the front of the queue Is the Queue Empty Is the Queue Full What is the size of the Queue 31

INITIALIZE THE QUEUE items [MAXQUEUE-1]..... items[1] items[0]front=0 rear=-1 The queue is initialized by having the rear set to -1, and front set to 0. Let us assume that maximum number of the element we have in a queue is MAXQUEUE elements as shown below. 32

insert(&Queue, ‘A’) an item (A) is inserted at the Rear of the queue items [MAXQUEUE -1].... items[3] items[2] items[1] items[0]A Front=0, Rear=0 33

insert(&Queue, ‘B’) A new item (B) is inserted at the Rear of the queue items [MAXQUEUE -1].... items[3] items[2] items[1]B Rear=1 items[0]A Front=0 34

insert(&Queue, ‘C’) A new item (C) is inserted at the Rear of the queue items [MAXQUEUE -1].... items[3] items[2]C Rear=2 items[1]B items[0]A Front=0 35

insert(&Queue, ‘D’) A new item (D) is inserted at the Rear of the queue items [MAXQUEUE-1].... items[3]D Rear=3 items[2]C items[1]B items[0]A Front=0 36

char remove(&Queue) an item (A) is removed (deleted) from the Front of the queue items [MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]BFront=1 items[0]A 37

char remove(&Queue) Remove two items from the front of the queue. items [MAXQUEUE-1].... items[3]DRear=3 items[2]CFront=2 items[1]B items[0]A 38

char remove(&Queue) Remove two items from the front of the queue. items [MAXQUEUE-1].... items[3]DFront=Rear=3 items[2]C items[1]B items[0]A 39

char remove(&Queue) Remove one more item from the front of the queue. items [MAXQUEUE-1].. items[4]Front=4 items[3]DRear=3 items[2]C items[1]B items[0]A 40

INSERT / REMOVE ITEMS Assume that the rear= MAXQUEUE-1 What happens if we want to insert a new item into the queue? items[MAXQUEUE-1]Xrear=MAXQUEUE items[3]Dfront=3 items[2]C items[1]B items[0]A 41

INSERT / REMOVE ITEMS What happens if we want to insert a new item F into the queue? Although there is some empty space, the queue is full. One of the methods to overcome this problem is to shift all the items to occupy the location of deleted item. 42

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]BFront=1 items[0]A 43

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]BFront=1 items[0]B 44

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]C items[1]C items[0]B 45

REMOVE ITEM items[MAXQUEUE-1].... items[3]DRear=3 items[2]D items[1]C items[0]B 46

REMOVE ITEM items[MAXQUEUE-1].... items[3]D items[2]DRear=2 items[1]C items[0]B 47

INSERT / REMOVE ITEMS Since all the items in the queue are required to shift when an item is deleted, this method is not preferred. The other method is circular queue. When rear = MAXQUEUE-1, the next element is entered at items[0] in case that spot is free. 48

Initialize the queue. items [6] front=rear=6 items[5] items[4] items[3] items[2] items[1] items[0] 49

Insert items into circular queue items [6] front=6 items[5] items[4] items[3] items[2] items[1] items[0]A rear=0 Insert A,B,C to the rear of the queue. 50

Insert items into circular queue items [6] front=6 items[5] items[4] items[3] items[2] items[1]B rear=1 items[0]A Insert A,B,C to the rear of the queue. 51

Insert items into circular queue Insert A,B,C to the rear of the queue. items [6] front=6 items[5] items[4] items[3] items[2]C rear=2 items[1]B items[0]A 52

Remove items from circular queue Remove two items from the queue. items [6] items[5] items[4] items[3] items[2] Crear=2 items[1] B items[0] Afront=0 53

Remove items from circular queue Remove two items from the queue. items [6] items[5] items[4] items[3] items[2] Crear=2 items[1] Bfront=1 items[0] A 54

Remove items from circular queue Remove one more item from the queue. items [6] items[5] items[4] items[3] items[2] Crear=front=2 items[1] B items[0] A 55

Insert D,E,F,G to the queue. items [6] G rear=6 items[5]F items[4] E items[3] D items[2] Cfront=2 items[1] B items[0] A 56

Insert H and I to the queue. items [6] G items[5]F items[4] E items[3] D items[2] Cfront=2 items[1] B items[0] Hrear=0 57

Insert H and I to the queue. items [6] G items[5]F items[4] E items[3] D items[2] Cfront=2 items[1] I items[0] Hrear=0 58

Insert J to the queue. items [6] G items[5]F items[4] E items[3] D items[2] ??front=rear=2 items[1] I items[0] H 59

3.PRIORITY QUEUES The priority queue is a data structure in which intrinsic ordering of the elements determines the results of its basic operations. An ascending priority queue is a collection of items into which items can be inserted arbitrarily and from which only the smallest item can be removed. On the other hand a descending priority queue allows only the largest item to be removed. 60

Priority QUEUE Operations Insertion The insertion in Priority queues is the same as in non-priority queues. Deletion Deletion requires a search for the element of highest priority and deletes the element with highest priority. The following methods can be used for deletion/removal from a given Priority Queue: An empty indicator replaces deleted elements. After each deletion elements can be moved up in the array decrementing the rear. The array in the queue can be maintained as an ordered circular array 61

Priority Queue Declaration Queue data type of Priority Queue is the same as the Non-priority Queue. #define MAXQUEUE 10 /* size of the queue items*/ typedef struct { int front, rear; int items[MAXQUEUE]; }QUEUE; 62

Summary 1. Circular queue 2. Double Ended Queue 3. Priority queue 63