Ashish Gupta, Data JUET, Guna

Similar documents

.:: UNIT 4 ::. STACK AND QUEUE

Queue. Data Structure

Your Topic Goes Here Queue

1 P age DS & OOPS / UNIT II

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks and Queues. CSE 373 Data Structures Lecture 6

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

ITI Introduction to Computing II

Data Structure - Stack and Queue-

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Chapter 18: Stacks And Queues

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture Data Structure Stack

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

Stack and Queue. Stack:

CSC 1052 Algorithms & Data Structures II: Linked Queues

Chapter 18: Stacks And Queues

CMSC Introduction to Algorithms Spring 2012 Lecture 7

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

! A data type for which: ! An ADT may be implemented using various. ! Examples:

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

CSEN 301 Data Structures and Algorithms

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

DATA STRUCTURE UNIT I

Data Structures. Lecture 5 : The Queues

Programming. Lists, Stacks, Queues

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

UNIT-2 Stack & Queue

Linked List. April 2, 2007 Programming and Data Structure 1

Data Structures and Algorithms for Engineers

Queue rear tail front head FIFO

Queue with Array Implementation

Postfix (and prefix) notation

LIFO : Last In First Out

8. Fundamental Data Structures

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Queue: Queue Representation: Basic Operations:

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Queue Linked List Implementation

Stacks and Queues. CSE Data Structures April 12, 2002

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Lecture 3: Stacks & Queues

Chapter 9 STACK, QUEUE

1. Introduction. Lecture Content

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

CS 206 Introduction to Computer Science II

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Queues. Queue ADT Queue Implementation Priority Queues

CS24 Week 4 Lecture 2

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

data structures and algorithms lecture 6

CMSC 132: Object-Oriented Programming II. Stack and Queue

Computer Science Foundation Exam

Data Structures. Lecture 4 : The Queues. Dr. Essam Halim Houssein Lecturer, Faculty of Computers and Informatics, Benha University

LECTURE OBJECTIVES 6-2

BBM 201 DATA STRUCTURES

Ashish Gupta, Data JUET, Guna

Insert. SEMCOM Page 1 of 11

Introduction to Data Structures and Algorithms

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Queue ADT. CMPT 125 Feb. 28

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Algorithms and Data Structures

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Programming Abstractions

Stack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea

Stacks and Queues III

12 Abstract Data Types

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Linear Data Structures

DS L9: Queues

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

CSC 1052 Algorithms & Data Structures II: Queues

List, Stack and Queue Implementation

An Introduction to Queues With Examples in C++

Subject : Computer Science. Paper: Data Structures. Module: Priority Queue and Applications. Module No: CS/DS/14

Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays

ADTs Stack and Queue. Outline

Transcription:

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

Representation It can 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

Representation (Cont.) 5 6 4 front rear Insert (Enqueue) 15, 4,9,6 5 6 4 15 4 9 6 front rear Delete (Dequeue) 4 times 4 9 6 front rear

Representation front rear 5 6 4 Insert (Enqueue) 15, 9 front rear 5 6 4 15 9 Delete (Dequeue) 3 times front 15 9 rear

Basic Operations 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

Create 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

Enqueue 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<= Q->rear) 8. Q->array[Temp Q->front] = Q->array[Temp] 9. Temp = Temp + 1 10. Q->rear = Q->rear Q->front + 1 11. Q->front = 0 12. Else 13. Q->rear = Q->rear + 1 14. Q->array[Q->rear] = Item Complexity = O(n)

Dequeue 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 = -1 7. Q->rear = -1 8. Else 9. Q->front = Q->front + 1 10. Return Item Complexity = O(1)

Accessing Front Element Algorithm Peek(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)

Limitation of Linear Queue 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

Circular Queue During enqueue of an element in queue, if rear reaches to (SIZE 1) considering queue starting index is 0 then, Make rear = 0 0 1 2 3 4 5 6 7 4 9 6 14 front rear front 4 9 Creation of circular queue is same as linear queue. 6 14 rear

EnCqueue 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 + 1 11. CQ->array[CQ->rear] = Item Complexity = O(1)

DeCqueue 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 = -1 7. CQ->rear = -1 8. Else if (CQ->front == SIZE -1) 9. CQ->front = 0 10. Else 11. CQ->front = CQ->front + 1 12. Return Item Complexity = O(1)

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

Create queue C code: Create queue: struct Node{ int INFO; struct Node *NEXT; }; struct Queue{ struct Node *front; struct Node *rear; }Q; 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

EnLqueue 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)

DeLqueue 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. Return Item Complexity = O(1)

Deque Linear queue which allows insertion and deletion from both ends of list Addition or deletion can be performed from front or rear Insertion Deletion 5 6 4 15 4 9 6 front rear Deletion Insertion 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

Operations on deque 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

Priority Queue 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.

Implementation of priority queue Linear Linked List 6 1 8 2 12 3 14 4 front rear Multiple queues, provide priority to each queue

Create priority queue C code: Create queue: struct Node{ int INFO; int priority; struct Node *NEXT; }; struct Queue{ struct Node *front; struct Node *rear; }Q; 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

Insertion in priority queue 1. Make node with provided item and assigning priority 2. Insert at proper place according to priority 11 3 6 1 8 2 12 3 14 4 front rear

Applications of priority queue 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.

Summary 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