Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
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 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Data Structure Dr. Mohamed Khafagy.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Objectives of these slides:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CE 221 Data Structures and Algorithms
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
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.
Review Array Array Elements Accessing array elements
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
Queues: Implemented using Arrays
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Abstract Data Type Abstract Data Type as a design tool
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CSE 373 Data Structures Lecture 6
CE 221 Data Structures and Algorithms
Queues Definition of a Queue Examples of Queues
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
Presentation transcript:

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)

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 (called the rear of the 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 (called the rear of the queue)

Definitions Data structure which implements a first-in, first-out list; e.g. print queue, which contains a list of jobs to be printed in order. Data structure which implements a first-in, first-out list; e.g. print queue, which contains a list of jobs to be printed in order. In p, a queue is a data structure in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out). In programming, a queue is a data structure in which elements are removed in the same order they were entered. This is often referred to as FIFO (first in, first out). In contrast, a stack is a data structure in which elements are removed in the reverse order from which they were entered. This is referred to as LIFO (last in, first out). In contrast, a stack is a data structure in which elements are removed in the reverse order from which they were entered. This is referred to as LIFO (last in, first out).

Introduction to Queues The queue data structure is very similar to the stack. The queue data structure is very similar to the stack. In a stack, all insertions and deletions occur at one end, the top, of the list. In a stack, all insertions and deletions occur at one end, the top, of the list. In the queue, as in the stack, all deletions occur at the head of the list. In the queue, as in the stack, all deletions occur at the head of the list. However, all insertions to the queue occur at the tail of the list. However, all insertions to the queue occur at the tail of the list.

Introduction to Queues Basically, data enters the queue at one end and exits at the other end. Basically, data enters the queue at one end and exits at the other end.

ABC Front RearBC Front Rear

Introduction to Queues You get in line at the end and get serviced when you get to the front of the line. You get in line at the end and get serviced when you get to the front of the line. This characteristic gives a queue its first-in, first-out (FIFO) behavior. This characteristic gives a queue its first-in, first-out (FIFO) behavior.

Applications Ticketing counter Ticketing counter Bus stop line Bus stop line Bank Customers Bank Customers Printer SPOOL Printer SPOOL CPU Scheduling (at times) CPU Scheduling (at times) Etc etc etc Etc etc etc

Queue Operations Initialize the queue, Q, to be the empty queue. Initialize the queue, Q, to be the empty queue. Determine whether or not if the queue Q is empty. Determine whether or not if the queue Q is empty. Determine whether or not if the queue Q is full. Determine whether or not if the queue Q is full. Insert (enqueue) a new item onto the rear of the queue Q. Insert (enqueue) a new item onto the rear of the queue Q. Remove (dequeue) an item from the front of Q, provided Q is nonempty. Remove (dequeue) an item from the front of Q, provided Q is nonempty.

Queue Operations  isEmpty() – check to see if the queue is empty.  isFull() – check to see if the queue is full.  enqueue(element) - put the element at the end of the queue.  dequeue() – take the first element from the queue.  first() – return the first element in the queue without removing it.

Enqueue The queue insert is known as enqueue. The queue insert is known as enqueue. After the data has been inserted, this new element becomes the rear of the queue. After the data has been inserted, this new element becomes the rear of the queue.

Dequeue The queue delete operation is known as dequeue. The queue delete operation is known as dequeue. The data at the front of the queue is returned to the user and deleted from the queue. The data at the front of the queue is returned to the user and deleted from the queue.

Array Implementation Any implementation of a queue requires: storage for the data as well as markers (“pointers”) for the front and for the back of the queue. An array-based implementation would need structures like items, an array to store the elements of the queue Front, an index to track the front queue element Rear, an index to track the position following last queue element Additions to the queue would result in incrementing Rear. Deletions from the queue would result in incrementing Front. Clearly, we’d run out of space soon!

Queue using Arrays #define length 10 Struct queue { int items[length]; int front, rear; } Insert(q,x) Insert(q,x)q.items[++q.rear]=x; X=remove(q) X=remove(q)x=q.items[q.front++];

C B A q.front=0 q.rear=-1 q.front=0 q.rear=2 The queue is empty whenever q.rear<q.front The number of elements in the queue at any time is equal to the value of q.rear –q.front

= q.front C q.rear=2 C E q.front=2 q.rear=4 D Now there are 3 elements the queue but there is room for 5 If to insert F in the queue the q.rear must be increased by 1 to 5 and q. items[5] must be set to the value F. but q.items is an array of only 5 elements so this insertion cannot be made

Solution to Problem Clearly, we’have run out of space Solutions include: Shifting the elements downward with each deletion Viewing array as a circular buffer, i.e. wrapping the end to the front

Solution 1 For arrays there are two methods For arrays there are two methods First is do it as we do in real world First is do it as we do in real world  Check if array is not empty  Simply dequeue from the first location of array say array[0] i.e. the zero th index  After dequeue shift all the elements of the array from array[index] to array[index-1]

DequeueABCD RearBCD DE-QUEUE

Dequeue Operation x=q.items[0]; for(i=0; i<q.rear; i++) q.items[i]=q.items[i+1]q.rear--; Method is costly as we have to move all the elements of the array Method is costly as we have to move all the elements of the array Do we need Front Index in this case? Do we need Front Index in this case?  No, Because we are always dequeue(ing) from the first index

Solution2: Circular Queue To avoid the costly operation of coping all the elements again we employ another method called circular queue To avoid the costly operation of coping all the elements again we employ another method called circular queue Simply dequeue the element and move the front pointer to next index Simply dequeue the element and move the front pointer to next index If q.rear==q.front queue is empty If q.rear==q.front queue is empty q.front=q.rear=max_size-1; q.front=q.rear=max_size-1; q[3] ? q[4] ? q[5] ? q[0] ? q[1]q[2] ?? front MAX_SIZE = 6 back

Implementation Struct queue { int items[length]; int front, rear; }; Struct queue q; q.front=q.rear=Max_size –1; bool IsEmpty(struct queue *pq) {return(pq->front==pq->rear)} int remove (struct queue *pq) { if (empty(pq)){ if (empty(pq)){ cout<<“queue underflow” return 0; } If (pq->front==Max_size-1) pq->front=0;else(pq->front)++;Return(pq->items[pq->front])}

Implementation cont. int insert(struct queue *pq, int x) { //make room for new element If (pq->rear==Max_size-1) pq->rear=0;else(pq->rear)++; //Check for overflow If (pq->rear==pq->front) { cout<<“queue overflow”; exit(1);}pq->items[pq->rear]=x;return;}

Queue Operations q[3] ? q[4] ? q[5] ? q[0] ? q[1]q[2] ?? back front q[3] ? q[4] ? q[5] ? q[0] A q[1]q[2] ?? front back q[3] ? q[4] ? q[5] ? q[0] A q[1]q[2] D? front back q[3] ? q[4] ? q[5] ? q[0] A q[1]q[2] DT front back

Queue Operations cont. q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] ? q[5] ? q[0] A q[1]q[2] DT front back

Queue Operations cont. q[3] E q[4] X q[5] ? q[0] A q[1]q[2] DT front back q[3] E q[4] X q[5] A q[0] A q[1]q[2] DT front q[3] E q[4] X q[5] A q[0] M q[1]q[2] DT front back wrap-around back

Circular Queue [2] [3] [2] [3] [1] [4] [0] [5] [0] [5] J2 J1 J3 Can be seen as a circular queue

Problems with above solution How to know if the queue is empty How to know if the queue is full What is the relation between front and back when queue is full? Solution 1. 1.Keep it simple… add counter to the queue 2. 2.Keep an empty slot between Front and Rear i.e., items array uses QUEUE_CAPACITY - 1 elements

Solution (a) define MAXQUEUESIZE 100; struct { char personName[25]; int personNIC;//lets assume NIC to be integer type. } Person; struct { int Count; /* number of queue items */ int Head; /* head of queue */ int Tail; /* tail of queue */ Person Items[MAXQUEUESIZE]; }Queue;

Solution (a) cont. void InitializeQueue(Queue *Q) { Q->Count = 0; /* zero count of items */ Q->Head = MAXQUEUESIZE-1; Q->Tail = MAXQUEUESIZE-1; } int Empty(Queue *Q) { return (Q->Count == 0); } int Full(Queue *Q) { return (Q->Count == MAXQUEUESIZE); }

FULL QUEUE FULL QUEUE [2] [3] [2] [3] [1] [4][1] [4] [0] [5] front =0 rear = 5 front =4 rear =3 J2 J3 J1 J4 J5 J6 J5 J7 J8 J9 Solution b:Leave one empty space when queue is full

EMPTY QUEUE [2] [3] [2] [3] [1] [4] [0] [5] [0] [5] front = 0 front = 0 rear = 0 rear = 3 J2 J1 J3