 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.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks, Queues, and Linked Lists
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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.
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.”
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 Objectives: Describe a queue
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.
 Balancing Symbols 3. Applications
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,
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
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.”
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
CHP-4 QUEUE.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Stacks And Queues Chapter 18.
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.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C, 2e
Queues.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Program based on queue & their operations for an application
Objectives In this lesson, you will learn to: Define stacks
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
Stacks Stack: restricted variant of list
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Queues.
DATA STRUCTURE QUEUE.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
CSCS-200 Data Structure and Algorithms
Data Structures – Week #4
Presentation transcript:

 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 etc. The person who come first, he/she gets the service first. Similarly in data structure,  Queue is a linear list in which insertions can take place at one end of list, known as rear of the list, and deletions can take place at other end of list, known as front of list.  Queue is a ADT as stack except, nature of insertion and deletion of elements.  Nature of queue is First Come First Serve (FCFS) or First In First Out (FIFO) Example – Printer task queue, keystroke queue etc.

It can be implemented either using array or using linked list. In both the cases, Queue is defined by its two pointers – 1.FRONT – If queue is empty then FRONT = NULL 2.REAR – If queue is full then REAR = N where, N is the size of array used as queue

564 frontrear Insert (Enqueue) 15, 4,9, front rear Delete (Dequeue) 4 times 496 frontrear

56 4 Insert (Enqueue) 15, front rear front rear Delete (Dequeue) 3 times 15 9 rearfront

1.CreateQueue – Creates an empty queue 2.Enqueue – Inserts element into queue 3.Dequeue – Deletes element from queue 4.IsEmpty – Checks the emptiness of queue 5.IsFull – Checks the fullness of queue

C code: struct queue{ int array[SIZE]; int front, rear; }; Create queue: Algorithm Create_Queue(Q): This algorithm creates a queue with rear = -1 and front = -1. Here, Q is structure of an {array and front, rear} elements. front and rear represents the first and last item of queue. 1. Q -> front = Q -> rear = -1 // -1 means queue is empty

Algorithm Enqueue(Q, Item) – This algorithm inserts an element into queue Q. Item is the element which is to be inserted. 1.If (Q->front == -1 or Q->rear == -1)\\ Check for emptiness 2. Q->front = Q->rear = 0 3.Else If ( Q-> rear == SIZE-1)\\ Check for fullness 4. If(Q->front == 0) 5. Exit 6. Temp = Q->front 7. While(Temp rear) 8. Q->array[Temp – Q->front] = Q->array[Temp] 9. Temp = Temp Q->rear = Q->rear – Q->front Q->front = Else 13. Q->rear = Q->rear Q->array[Q->rear] = Item Complexity = O(n)

Algorithm Dequeue(Q) – This algorithm deletes an element from queue Q. Item is the element which is returned after deletion. 1.If (Q->front == -1 or Q->rear == -1)\\ Check for emptiness 2. Display “Queue is empty” 3. Exit 4.Item = Q->array[Q->front] 5.If ( Q-> rear == Q->front)\\ Only one element 6. Q->front = Q->rear = -1 8.Else 9. Q->front = Q->front Return Item Complexity = O(1)

Algorithm Peep(Q) – This algorithm accesses an element from queue Q. Item is the element which is returned. 1.Item = Q->array[Q->front] 2.Return Item Complexity = O(1)

If last position of queue is occupied then it is not possible to enqueue any more elements even though some positions are vacant towards the front end of queue. Solutions: 1.Shift the elements towards beginning of queue or toward left and adjust the front and rear accordingly. But, if linear queue is very long then it would be very time consuming to shift the elements towards vacant positions. 2.Make circular queue

During enqueue of an element in queue, if rear reaches to (SIZE – 1) considering queue starting index is 0 then, Make rear = front rear Creation of circular queue is same as linear queue front rear

Algorithm EnCqueue(CQ, Item) – This algorithm inserts an element into queue CQ. Item is the element which is to be inserted. 1.If (CQ->front == -1 or CQ->rear == -1)\\ Check for emptiness 2. CQ->front = CQ->rear = 0 3.Else If ( CQ-> rear == SIZE-1)\\ Check for fullness 4. If(CQ->front == 0) 5. Exit 6. CQ->rear = 0 7.Else If(CQ->rear = = (CQ->front – 1)) 8. Exit 9.Else 10. CQ->rear = CQ->rear CQ->array[CQ->rear] = Item Complexity = O(1)

Algorithm DeCqueue(CQ) – This algorithm deletes an element from queue CQ. Item is the element which is returned after deletion. 1.If (CQ->front == -1 or CQ->rear == -1)\\ Check for emptiness 2. Display “Queue is empty” 3. Exit 4.Item = CQ->array[CQ->front] 5.If ( CQ-> rear == CQ->front)\\ Only one element 6. CQ->front = CQ->rear = Else if (CQ->front == SIZE -1) 9. CQ->front = 0 10.Else 11. CQ->front = CQ->front Return Item Complexity = O(1)

 Size of queue must be known in advance. In real scenario, it is not possible.

C code: struct Node{ int INFO; struct Node *NEXT; }; struct Queue{ struct Node *front; struct Node *rear; }Q; Create queue: Algorithm Create_LQueue(Q): This algorithm creates a queue with rear = NULL and front = NULL. Here, Q is a linked list. front and rear the pointers, pointing to first and last item of queue. 1. Q -> front = Q -> rear = NULL// NULL means queue is empty

Algorithm EnLqueue(Q, Item) – This algorithm inserts an element into queue Q. Item is the element which is to be inserted. 1.Node Temp 2.Temp = Allocate memory 3.Temp->INFO = Item 4.Temp->NEXT = NULL 5.If (Q->front == NULL or Q->rear == NULL)\\ Check for emptiness 6. Q->front = Q->rear = Temp 7.Else 8. (Q->rear)->NEXT = Temp 9. Q->rear = Temp Complexity = O(1)

Algorithm DeLqueue(Q) – This algorithm deletes an element from queue Q. Item is the element which is returned after deletion. 1.Node Temp 2.If (Q->front == NULL or Q->rear == NULL)\\ Check for emptiness 3. Display “Queue is empty” 4. Exit 5.Item = (Q-> front) ->INFO 6.Temp = Q->front 7.If ( Q-> rear == Q->front)\\ Only one element 8. Q->front = NULL 9. Q->rear = NULL 10.Else 11. Q->front = (Q->front)-> NEXT 12.Free Temp 13.Return Item Complexity = O(1)

 Linear queue which allows insertion and deletion from both ends of list  Addition or deletion can be performed from front or rear There are two variations of deque – 1.Input restricted deque – Insertions from only one end but deletion from both ends 2.Output restricted deque – Insertion from both ends and deletion from one end only frontrear InsertionDeletion InsertionDeletion

1.Insertion at beginning – Using front pointer 2.Insertion at end – Using front pointer 3.Deletion at beginning – Using rear pointer 4.Deletion at end – Using rear pointer

Assume that the operators +, -, × are left associative and ^ is right associative. The order of precedence (from highest to lowest) is ^, x, +, -. The postfix expression corresponding to the infix expression a + b × c - d ^ e ^ f is A. abc × + def ^ ^ - B. abc × + de ^ f ^ - C. ab + c × d - e ^ f ^ D. - + a × bc ^ ^ def Ans:A abc × + def ^ ^ -

Following is C like pseudo code of a function that takes a Queue as an argument, and uses a stack S to do processing. void fun(Queue *Q) { Stack S; // Say it creates an empty stack S while (!isEmpty(Q)) // Run while Q is not empty { // deQueue an item from Q and push the dequeued item to S push(&S, deQueue(Q)); } while (!isEmpty(&S)) // Run while Stack S is not empty { // Pop an item from S and enqueue the popped item to Q enQueue(Q, pop(&S)); } What does the above function do in general? (A) Removes the last from Q (B) Keeps the Q same as it was before the call (C) Makes Q empty (D) Reverses the Q

 A queue where each element is assigned a priority.  Lower the number higher the priority.  An element with highest priority is processed first before any element of lower priority.  If two elements are of same priority then they are processed according to the order in which they were added in queue. Priority decision parameter – 1.Shortest job 2.Payment 3.Time 4.Type of Job etc.

 Linear Linked List  frontrear

C code: struct Node{ int INFO; int priority; struct Node *NEXT; }; struct Queue{ struct Node *front; struct Node *rear; }Q; Create queue: Algorithm Create_PQueue(Q): This algorithm creates a queue with rear = NULL and front = NULL. Here, Q is a linked list. front and rear the pointers, pointing to first and last item of queue. 1. Q -> front = Q -> rear = NULL// NULL means queue is empty

1.Make node with provided item and assigning priority 2.Insert at proper place according to priority frontrear 113

 Very useful in scheduling – - Traffic light - Job scheduling in operating system  Bandwidth management – When higher priority customer needs more bandwidth, all other lower priority customer are blocked for some duration. Here, priority may be according to plan which customer use.  Huffman coding – To transmit the data efficiently on network.

 Overview of Queue – - FIFO - Applications  Basic operations on queue – - Enqueue - Dequeue  Implementation of queue – - Using array - Using Linked List  Types of queue – - Linear Queue - Circular Queue - Deque - Priority Queue

The following postfix expression with single digit operands is evaluated using a stack: ^ / 2 3 * * - Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are: (A) 6, 1 (B) 5, 7 (C) 3, 2 (D) 1, 5 Ans: A

Following is C like pseudo code of a function that takes a number as an argument, and uses a stack S to do processing. What does the above function do in general? void fun(int n) { Stack S; // Say it creates an empty stack S while (n > 0) { // This line pushes the value of n%2 to stack S push(&S, n%2); n = n/2; } // Run while Stack S is not empty while (!isEmpty(&S)) printf("%d ", pop(&S)); // pop an element from S and print it } What does the above function do in general? (A) Prints binary representation of n in reverse order (B) Prints binary representation of n (C) Prints the value of Logn (D) Prints the value of Logn in reverse order