Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
 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.
Data Structures for Media Queues. Queue Abstract Data Type Queue Abstract Data Type Sequential Allocation Sequential Allocation Linked Allocation Linked.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
DATA STRUCTURE & ALGORITHMS
 Balancing Symbols 3. Applications
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: 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.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Structures Using C++
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
Stacks And Queues Chapter 18.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
 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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Review Array Array Elements Accessing array elements
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
Data Structures Using C, 2e
Queues.
CC 215 Data Structures Queue ADT
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Data Structures – Week #3
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Queues.
DATA STRUCTURE QUEUE.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
CE 221 Data Structures and Algorithms
Data Structures – Week #3
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Queues Definition of a Queue Examples of Queues
CSE 373 Data Structures Lecture 6
CSCS-200 Data Structure and Algorithms
Data Structures – Week #4
Presentation transcript:

Data Structures – Week #4 Queues

January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List Implementation of Queues Queue Applications

January 12, 2016Borahan Tümer, Ph.D.3 Queues (Kuyruklar) A queue is a list of data with the restriction that 1.data can be inserted from the “rear” or “tail,” and 2.data can be retrieved from the “front” or “head” of the list. By “rear” we mean a pointer pointing to the element that is last added to the list whereas “front” points to the first element. A queue is a first-in-first-out (FIFO) structure.

January 12, 2016Borahan Tümer, Ph.D.4 Operations on Queues Two basic operations related to queues: –Enqueue (Put data to the rear of the queue) –Dequeue (Retrieve data from the front of the queue)

Data Structures for Queues Queues can be implemented using –arrays, or –linked lists. January 12, 2016Borahan Tümer, Ph.D.5

January 12, 2016Borahan Tümer, Ph.D.6 Array Implementation of Queues Queues can be implemented using arrays. During the execution, queue can grow or shrink within this array. The array has two “open” ends. One end of the doubly-open-ended array is the rear where the insertions are made. The other is the front where elements are removed.

January 12, 2016Borahan Tümer, Ph.D.7 Array Implementation of Queues Initialization of an n-node queue: –front=0; rear=-1; Condition for an empty queue: –In general: rear+1 = front –In particular: rear = -1; Condition for a full queue –In general: rear-(n-1) = front; –In particular: rear  n-1;

January 12, 2016Borahan Tümer, Ph.D.8 Sample C Implementation #define queueSize …; struct dataType { … } typedef struct dataType dataType; struct queueType { int front; int rear; dataType content[queueSize]; } typedef struct queueType queueType; queueType queue;

January 12, 2016Borahan Tümer, Ph.D.9 Sample C Implementation… isEmpty() and isFull() // Initialize Queue (i.e., set value of front and rear to 0) queue.rear=-1; queue.front=0; int isEmpty(queueType q) { return (q.rear < q.front); } int isFull(queueType q, int n) { return (q.rear >= q.front + (n-1)); }

January 12, 2016Borahan Tümer, Ph.D.10 Enqueue() Operation int enqueue(queueType *qp,int n,dataType item) { if isFull(*qp,n) return 0; //unsuccessful insertion (*qp).content[++(*qp).rear]=item; return 1; //successful insertion } Running time of enqueue O(?) An O(1) operation

January 12, 2016Borahan Tümer, Ph.D.11 Enqueue Operation Animated Empty Queue a enqueued b enqueued c enqueued d enqueued … k enqueued l enqueued

January 12, 2016Borahan Tümer, Ph.D.12 Dequeue Operation int dequeue(queueType *qp,dataType *item) { if isEmpty(*qp) return 0; //unsuccessful removal *item = (*qp).content[0]; // always: front = 0 for (i=1; i <= (*qp).rear; i++) (*qp).content[i-1]= (*qp).content[i]; (*qp).rear--; return 1; //successful removal } O(?) An O(n) operation

January 12, 2016Borahan Tümer, Ph.D.13 O(n) Dequeue Operation Animated a dequeued b dequeued c dequeued d dequeued … k dequeued l dequeued Empty Queue

January 12, 2016Borahan Tümer, Ph.D.14 Improved Dequeue Operation int dequeue(queueType *qp,dataType *item) { if isEmpty(*qp) return 0; //unsuccessful removal *item = (*qp).content[(*qp).front++]; return 1; //successful removal } An O(1) operation

January 12, 2016Borahan Tümer, Ph.D.15 O(1) Dequeue Operation Animated a dequeued b dequeued c dequeued d dequeued … k dequeued l dequeued Empty Queue

January 12, 2016Borahan Tümer, Ph.D.16 Problem of O(1) Dequeue As front proceeds towards the larger indexed elements in the queue, we get supposedly available but inaccessible array cells in the queue (i.e., all elements with indices less than that pointed to by front). Whenever rear points to (n-1) st element, a shift operation still needs to be carried out. Solution: attaching the end of the queue to the start!!! Such queues we call circular queues.

January 12, 2016Borahan Tümer, Ph.D.17 Circular Queues Since with the existing conditions an empty and full circular queue is indistinguishable, we redefine the conditions for empty and full queue following a new convention: Convention: front points to the preceding cell of the cell with the data to be removed next. Empty circular queue condition: front=rear Full queue condition: front=(rear+1) mod n

January 12, 2016Borahan Tümer, Ph.D.18 Circular Queues (CQs) // Initialize Queue (i.e., set value of front and rear to n-1) queue.rear=n-1; queue.front=n-1; // i.e., -1 mod n int isEmptyCQ(queueType cq) { return (cq.rear == cq.front); } int isFullCQ(queueType cq, int n) { return (cq.rear == (cq.front-1 % n)); }

January 12, 2016Borahan Tümer, Ph.D.19 Enqueue Operation in CQs int enqueueCQ(queueType *cqp,dataType item) { if isFullCQ(*cqp,n) return 0;//unsuccessful insertion (*cqp).content[++(*cqp).rear % n]=item; return 1; //successful insertion } An O(1) operation

January 12, 2016Borahan Tümer, Ph.D.20 Dequeue Operation in CQs int dequeueCQ(queueType *cqp,dataType *item) { if isEmptyCQ(*cqp) return 0;//unsuccessful removal *item = (*cqp).content[++(*cqp).front % n]; return 1; //successful removal } An O(1) operation

January 12, 2016Borahan Tümer, Ph.D.21 Circular Queues int enqueueCQ(queueType *cqp,dataType item) { if isFullCQ(*cqp) return 0;//unsuccessful insertion (*cqp).content[++(*cqp).rear%n]=item; return 1; //successful insertion } int dequeueCQ(queueType *cqp,dataType *item) { if isEmptyCQ(*cqp) return 0;//unsuccessful removal *item = (*cqp).content[++(*cqp).front%n]; return 1; //successful removal }

January 12, 2016Borahan Tümer, Ph.D.22 Linked List Implementation of Queues //Declaration of a queue node Struct QueueNode { int data; struct QueueNode *next; } typedef struct QueueNode QueueNode; typedef QueueNode * QueueNodePtr; …

January 12, 2016Borahan Tümer, Ph.D.23 Linked List Implementation of Queues QueueNodePtr NodePtr, rear, front; … NodePtr = malloc(sizeof(QueueNode)); rear = NodePtr; NodePtr->data=2; // or rear->data=2 NodePtr->next=NULL;// or rear->next=NULL; Enqueue(&rear,&NodePtr); … Dequeue( ); …

January 12, 2016Borahan Tümer, Ph.D.24 Enqueue and Dequeue Functions Void Enqueue (QueueNodePtr *RearPtr, QueueNodePtr *NewNodePtr) { *NewNodePtr = malloc(sizeof(QueueNode)); (*NewNodePtr)->data=5; (*NewNodePtr)->next =NULL; (*RearPtr)->next=*NewNodePtr; *RearPtr = (*RearPtr)->next; } Void Dequeue(QueueNodePtr *FrontPtr) { QueueNodePtr TempPtr; TempPtr= *FrontPtr; *FrontPtr = (*FrontPtr)->next; free(TempPtr); // or you may return TempPtr!!! }

January 12, 2016Borahan Tümer, Ph.D.25 Linked List Implementation of Queues Void Enqueue (QueueNodePtr *RearPtr, QueueNodePtr *NewNodePtr) { *NewNodePtr = malloc(sizeof(QueueNode)); (*NewNodePtr)->data=5; (*NewNodePtr)->next =NULL; (*RearPtr)->next=*NewNodePtr; *RearPtr = (*RearPtr)->next; } Void Dequeue(QueueNodePtr *FrontPtr) { QueueNodePtr TempPtr; TempPtr= *FrontPtr; *FrontPtr = (*FrontPtr)->next; free(TempPtr); // or return TempPtr!!! }

January 12, 2016Borahan Tümer, Ph.D.26 Queue Applications All systems where a queue (a FIFO structure) is applicable can make use of queues. Possible examples from daily life are: –Bank desks –Market cashiers –Pumps in gas stations Examples from computer science are: –Printer queues –Queue of computer processes that wait for using the microprocessor

January 12, 2016Borahan Tümer, Ph.D.27 Priority Queues While a regular queue functions based on the arrival time as the only criterion as a FIFO structure, this sometimes degrades the overall performance of the system. Consider a printer queue in a multi-processing system where one user has submitted, say, a 200-page-long print job seconds before many users have submitted print jobs of only several pages long. A regular queue would start with the long print job and all others would have to wait. This would cause the average waiting time (AWT) of the queue to increase. AWT is an important measure used to evaluate the performance of the computer system, and the shorter the AWT, the better the performance of the system.

January 12, 2016Borahan Tümer, Ph.D.28 Priority Queues What may be done to improve the performance of the printer queue? Solution: Assign priority values to arriving jobs Then, jobs of the same priority will be ordered by their arrival time.

January 12, 2016Borahan Tümer, Ph.D.29 Priority Queues Assume a printer queue of jobs with three priorities, a, b, and c, where jobs with a (c) have the highest (lowest) priority, respectively. That is, jobs with priority a are to be processed first by their arrival times, and jobs of priority c last.

January 12, 2016Borahan Tümer, Ph.D.30 Priority Queues