QUEUE.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

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.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
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,
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
CHP-4 QUEUE.
CIRCULAR LINKED LIST. Circular Linked List- A circular linked list is a linked list in which last element or node of the list points to first node. For.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
Stacks, Queues & Recursion
Data Strcutures.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
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.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CHP-3 STACKS.
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.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
UNIT-II Topics to be covered Singly linked list Circular linked list
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
 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.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
© 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.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
COSC160: Data Structures: Lists and Queues
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Algorithm and Data Structure Part II Dr. Naimah Yaakob
Objectives In this lesson, you will learn to: Define stacks
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Linked Lists A linked list or one way list is a linear collection of data elements called nodes where the order is given by means of pointers It is divided.
Queues Mohammad Asad Abbasi Lecture 5
STACKS.
INSERTION INTO A LINEAR ARRAY Set J = N Repeat step 3 and 4 while J>= K Set LA[ J+1] = LA [ J ] Set J = J-1 Set LA [K] = ITEM Set N = N+1 Exit.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
DATA STRUCTURE QUEUE.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
STACK, QUEUES, RECURSION
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
Introduction to Data Structures
Queues FIFO Enqueue Dequeue Peek.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

QUEUE

Definition of Queue A Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue). The first element inserted into the queue is the first element to be removed. For this reason a queue is sometimes called a FIFO (first-in first-out) list as opposed to the stack, which is a LIFO (last-in first-out).

Representing a Queue Using an Array A Queue is maintained by a linear array and two pointer variables: FRONT, containing the location of front element of the queue and REAR, containing the location of rear(last) element of the queue. The condition FRONT=NULL indicates that queue is empty. Whenever an element is deleted from the queue, the value of FRONT is increased by 1. Similarly, whenever an element is added to queue, the value of REAR is increased by 1.

Queue as a circular queue It can be seen that after N insertions in a Queue represented by an array of N elements, the rear element of Queue will occupy last part of array. This occurs even though the queue itself may not contain many elements. Now, if we want to insert an element ITEM into a queue, we have to move or rearrange the elements of entire queue to the beginning of the queue. This procedure may be very expensive. Another method to do so is to represent a queue as a circular queue

i,e QUEUE[1] comes after QUEUE[N] in array i,e QUEUE[1] comes after QUEUE[N] in array. With this assumption, we insert ITEM into queue by assigning ITEM to QUEUE[1]. Thus instead of increasing REAR to N+1, we reset REAR=1 and then assign QUEUE[REAR]=ITEM Similarly, If FRONT=N and an element of QUEUE is deleted, we reset FRONT=1 instead of increasing FRONT to N+1

Algorithm for Inserting in a QUEUE Algorithm: QINSERT(QUEUE, N, FRONT, REAR,ITEM) This algorithm inserts an element in a linear queue Step 1:[Queue already filled] If REAR=N, then: Write: ‘OVERFLOW’ and Exit. Step 2: If FRONT=NULL, then: [Queue initially empty] Set FRONT:=1 and REAR:=1 Else: Set REAR:=REAR+1 [End of If structure] Step 3: Set QUEUE[REAR]:=ITEM Step 4: Return

Algorithm: QDELETE(QUEUE,N,FRONT,REAR,ITEM) This algorithm deletes an element from a queue Step 1: If FRONT=NULL, then: Write: ‘UNDERFLOW’ Exit Step 2: Set ITEM:=QUEUE[FRONT] Step 3: If FRONT=REAR, then: [Empty Queue] Set FRONT:=NULL and REAR:=NULL Else: Set FRONT:=FRONT+1 [End of If structure] Step 4: Return

Algorithm: QINSERT(QUEUE, N, FRONT, REAR,ITEM) This algorithm inserts an element in a circular queue Step 1:[Queue already filled] If FRONT=1 and REAR=N or FRONT=REAR+1, then: Write: ‘OVERFLOW’ Exit Step 2: If FRONT=NULL, then: [Queue initially empty] Set FRONT:=1 and REAR:=1 Else If REAR=N, then: Set REAR:=1 Else: Set REAR:=REAR+1 [End of If structure] Step 3: Set QUEUE[REAR]:=ITEM Step 4: Return

Algorithm: QDELETE(QUEUE,N,FRONT,REAR,ITEM) This algorithm deletes an element from a circular queue Step 1: If FRONT=NULL, then: Write: ‘UNDERFLOW’ Exit Step 2: Set ITEM:=QUEUE[FRONT] Step 3: If FRONT=REAR, then: [Empty Queue] Set FRONT:=NULL and REAR:=NULL Else If FRONT=N, then: Set FRONT:=1 Else: Set FRONT:=FRONT+1 [End of If structure] Step 4: Return

Consider the following queue of characters where QUEUE is a circular array which is allocated six memory cells FRONT=2, REAR=4 QUEUE: _ A C D _ _ Describe the queue as following operations take place: F is added to queue Two letters are deleted K , L and M are added R is added to queue S is added to queue One letter is deleted

Solution: FRONT=2, REAR=5 QUEUE: _ A C D F_ FRONT=4, REAR=5 QUEUE: _ _ _ D F _ REAR=2, FRONT=4 QUEUE: L M _ D F K FRONT=6, REAR=2 QUEUE: L M _ _ _ K FRONT=6, REAR=3 QUEUE: L M R_ _ K FRONT=2, REAR=3 QUEUE: _M R _ _ _ REAR=4, FRONT=2 QUEUE: _ M R S _ _ FRONT=4, REAR=4 QUEUE: _ _ _ S _ _ FRONT=REAR=0 [ As FRONT=REAR, queue is empty] Since FRONT=NULL, no deletion can take place. Underflow occurred

DEQUE(Double ended Queue)- A deque is a queue in which elements can be added or removed at either end but not in the middle. A deque is usually maintained by a circular array DEQUE with pointers LEFT and RIGHT, which point to two ends of deque. The elements extend from LEFT end to RIGHT end of deque. The term circular comes from the fact that DEQUE[1] comes after DEQUE [N].The condition LEFT=NULL will be used to indicate that a deque is empty.

There are two variations of a deque Input-restricted deque- It is a deque which allows insertions at only one end of list but allows deletions at both ends of the list. Output-restricted deque- It is a deque which allows deletions at only one end of list but allows insertions at both ends of list

LEFT=2, RIGHT=4 DEQUE: _ A,C,D, _ , _ Consider the following deque of characters where DEQUE is a circular array which is allocated six memory cells. LEFT=2, RIGHT=4 DEQUE: _ A,C,D, _ , _ Describe deque while the following operation take place F is added to right of deque (b) Two letters on right are deleted (c) K,L and M are added to the left of the deque (d) One letter on left is deleted. (e) R is added to the left of deque. (f) S is added to right of deque (g) T is added to the right of deque

F is added to right of deque LFET=2, RIGHT=5 _A C D F _ (b) Two letters on right are deleted LEFT=2 RIGHT=3 _A C _ _ _ (c) K,L and M are added to the left of the deque LEFT=5 RIGHT=3 K A C _ M L (d) One letter on left is deleted. LEFT=6 RIGHT=3 K A C _ _ L (e) R is added to the left of deque. LEFT=5 RIGHT= 3 K A C _ R L (f) S is added to right of deque LEFT=5 RIGHT= 4 K A C S R L (g) T is added to the right of deque Since LEFT= RIGHT+1 , the array is full and hence T cannot be added to the deque

Linked representation of the Queue A linked queue is a queue implemented as a linked list with two pointer variables FRONT and REAR pointing to the nodes in the front and rear of the queue. The INFO field of list hold the elements of the queue and LINK field holds pointer to neighboring element of queue. In case of insertion in linked queue, a node borrowed from AVAIL list and carrying the item to be inserted is added as the last node of linked list representing the queue. Rear pointer is updated to point to last node just added to the list In case of deletion, first node of list pointed to by FRONT is deleted and FRONT pointer is updated to point to next node in the list.

Unlike the array representation, linked queue functions as a linear queue and there is no need to view it as circular for efficient management of space.

Algorithm:LINKQINSRT(INFO,LINK,FRONT,REAR,AVAIL,ITEM This algorithm inserts an item in linked list implementation of the queue. Step 1: If AVAIL=NULL,then: Write: ‘OVERFLOW’ Exit Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Step 3: Set INFO[NEW]:=ITEM and LINK[NEW]:=NULL Step 4: If FRONT=NULL, then: Set FRONT=REAR=NEW Else: Set LINK[REAR]:=NEW and REAR:=NEW Step 5: Return

Algorithm: LINKQDEL(INFO,LINK,FRONT,AVAIL,ITEM) This algorithm deletes an element from the front of the queue Step 1: If FRONT=NULL,then: Write:’UNDERFLOW’ Exit Step 2: Set TEMP:=FRONT Step 3: Set ITEM:=INFO[FRONT] Step 4: Set FRONT:=LINK[FRONT] Step 5: Set LINK[TEMP]:=AVAIL and AVAIL:=TEMP Step 6: Return

Priority Queue- A priority queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from following rules: An element of higher priority is processed before any element of lower priority Two elements of same priority are processed according to the order in which they were added to queue An example of a priority queue is a time sharing system. Programs of higher priority are processed first and programs with same priority form a standard queue

One-way list representation of a priority queue One way to maintain a priority queue in memory is by means of a one-way list Each node in list will contain three items of information: an information field INFO, a priority number PRN and a link field LINK. A node X precedes a node Y in list If X has higher priority than Y Or when both have same priority but X was added to list before Y

Algorithm:LKQINS(INFO,LINK,FRONT,PRN,AVAIL,ITEM, P) This algorithm inserts an item in linked list implementation of priority queue Step 1: If AVAIL=NULL,then: Write: ‘OVERFLOW’ Exit Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Step 3: [Enter the data and priority of new node] Set INFO[NEW]:=ITEM and PRN[NEW]:=P Step 4: Set PTR:=FRONT Step 5: If PRN[PTR]>PRN[NEW], then LINK[NEW]:=FRONT FRONT:=NEW Return [End of If Structure]

Step 5: Repeat while PTR≠NULL and PRN[PTR]<=PRN[NEW] Set SAVE:=PTR Set PTR:=LINK[PTR] [End of If Structure] Step 6: If PRN[PTR]>PRN[NEW] Set LINK[SAVE]:=NEW Set LINK[NEW]:=PTR Else: Set LINK[NEW]=NULL Step 7: Return

Another way to maintain a priority queue in memory is to use a separate queue for each level of priority . Each such queue will appear in its own circular array and must have its own pair of pointers, FRONT and REAR. If each queue is allocated the same amount of space, a two dimensional array QUEUE can be used instead of the linear arrays for representing a priority queue. If K represents the row K of the queue, FRONT[K] and REAR[K] are the front and rear indexes of the Kth row. 1 2 3 4 5 6 1 AAA 2 BBB CCC XXX 3 4 FFF DDD EEE 5 GGG Priority

Algorithm: QINSERT( QUEUE,N, FRONT, REAR,ITEM,K) This algorithm inserts an element in a priority queue in a row with priority K. N is the size of the Kth row. Step 1:[Queue already filled] If FRONT[K]=1 and REAR[K]=N or FRONT[K]=REAR[K]+1, then: Write: ‘OVERFLOW’ Exit Step 2: If FRONT[K]=NULL, then: [Queue initially empty] Set FRONT[K]:=1 and REAR[K]:=1 Else If REAR[K]=N, then: Set REAR[K]:=1 Else: Set REAR[K]:=REAR[K]+1 [End of If structure] Step 3: Set QUEUE[K][REAR[K]]:=ITEM Step 4: Return