CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM.

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

Ceng-112 Data Structures I Chapter 5 Queues.
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)
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
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.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.
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.
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.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 MT258 Computer Programming and Problem Solving Unit 9.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
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.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
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)
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.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
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
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
 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.
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
UNIT II Queue.
CC 215 Data Structures Queue ADT
Queues Rem Collier Room A1.02
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Basic Data Structures – Continued (Queues)
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
Queues.
DATA STRUCTURE QUEUE.
Stacks and Queues CSE 373 Data Structures.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Queues Definition of a Queue Examples of Queues
CSE 373 Data Structures Lecture 6
CSCS-200 Data Structure and Algorithms
Presentation transcript:

CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM

What is a queue? Definition: A queue is a set of elements of the same type in which the elements are added at one end, called the back or rear, and deleted from the other end, called the front or first The general rule to process elements in a queue is that the customer at the front of the queue is served next and that when a new customer arrives, he or she stands at the end of the queue. That is, a queue is a First In First Out, or simply FIFO data structure. 2

Queues Implementation 3 Physical Model We must keep track both the front and the rear of the queue. One method is to keep the front of the array in the first location on the array. Then, we can simply increase the counter of the array to show the rear. Nevertheless, to delete an entry from this queue is very expensive, since after the first entry was served, all the existing entry need to be move back one position to fill in the vacancy. With a long queue this process can lead to poor performance.

Queues Implementation 4 Physical Model ‘a’‘c’‘d’‘g’‘v’‘e’ Before: ‘a’ at index 0 is deleted ‘c’‘d’‘g’‘v’‘e’ ‘c’‘d’‘g’‘v’‘e’ After: front rear

Queues Implementation 5 Linear Implementation Indicate the front and rear of the queue. We can keep track the entry of the queue without moving any entries.  Append an entry: increase the rear by one.  To get the entry: increase the front by one. Problem:  Queue will increase and never decrease  This lead to the end of the storage capacity

Queues Implementation 6 Linear Implementation ‘a’‘c’‘d’‘g’‘k’ Add ‘k’ to the queue: - rear + 1 (increase) front rear Delete ‘a’ in queue: - front + 1 (increase) ‘a’‘c’‘d’‘g’‘k’ front rear ‘c’‘d’‘g’‘k’‘z’ Add ‘z’ to the queue: - -rear + 1 (increase) Reach end of storage!! rear

Queues Implementation 7 Circular Arrays We can overcome the inefficient use of the space by using a circular array. In this way, as entry are added and removed from the queue, the head will continually chasing the tail around the array, thus you don’t have to worry about running out of space unless the queue is fully occupied

Queues Implementation 8 Circular Arrays

Queues Implementation 9   Circular Arrays : Boundary Condition to indicate whether a queue is empty or full. If there is exactly one entry in the queue, then the front index will equal to the rear index. When this one entry is removed, then the front will increase by 1, so that an empty queue is indicated when the rear is one position before the front.

Queues Implementation 10   Circular Arrays : Boundary Condition Now, suppose that the queue is nearly full whereby it only has one empty position left. Then the rear will be only one position behind the front, the same condition as empty queue.

Queues Implementation 11   Circular Arrays : Possible Solution Leaving one position to be empty in the array. full queue is when the rear is two positions behind the front. Introduce a new variable to indicate the queue is full or not. The variable could either:   A Boolean variable that will be used when the rear comes just before the front to indicate whether the queue is full or not. or   An integer variable that counts the number of entries in the queue. Use special value for rear and/or front indices. For example, the array entries are indexed from 0 to MAX-1; then an empty queue could be indicated by setting the rear index to –1.

Circular Queues in C -Array Concept- 12 PART 1

Sample Program 1 #define MAZQUEUE 10 typedef int QueueIndex; typedef char QueueEntry; typedef struct queue{ int count; QueueIndex front; QueueIndex rear; QueueEntry entry[MAXQUEUE]; } Queue; 13 determine the maximum item in the queue to avoid program crash to show current index/position in queue Determine front and rear of queue Size of array “entry”

Continue… Several operations that can be performed to a Queue : 1. Create a queue 2. Test for an empty queue 3. Test for a full queue 4. Append (add) an item into queue 5. Serve (delete) an item from the queue 6. return the number of entries in the queue 14 void CreateQueue( Queue *q) { q count = 0; q front = 0; q rear = -1; } void CreateQueue( Queue *q) { q count = 0; q front = 0; q rear = -1; } bool QueueEmpty(Queue *q){ return (q->count count <= 0);} bool QueueEmpty(Queue *q){ return (q->count count <= 0);} bool QueueFull(Queue *q){ return ( q->count >= MAXQUEUE); return ( q->count >= MAXQUEUE);} bool QueueFull(Queue *q){ return ( q->count >= MAXQUEUE); return ( q->count >= MAXQUEUE);} void Append(QueueEntry x, Queue *q){ if (QueueFull(q)) if (QueueFull(q)) printf ("full queue" ); else { else {q->count++; q->rear = (q->rear + 1 ) % MAXQUEUE; q->entry[q->rear] = x; }} void Append(QueueEntry x, Queue *q){ if (QueueFull(q)) if (QueueFull(q)) printf ("full queue" ); else { else {q->count++; q->rear = (q->rear + 1 ) % MAXQUEUE; q->entry[q->rear] = x; }} void Serve(QueueEntry *x, Queue *q){ if (QueueEmpty(q)) if (QueueEmpty(q)) printf ("empty queue"); printf ("empty queue"); else { else {q->count--; *x = q->entry[q->front]; q->front =(q->front + 1 ) % MAXQUEUE; }} void Serve(QueueEntry *x, Queue *q){ if (QueueEmpty(q)) if (QueueEmpty(q)) printf ("empty queue"); printf ("empty queue"); else { else {q->count--; *x = q->entry[q->front]; q->front =(q->front + 1 ) % MAXQUEUE; }} int QueueSize( Queue *q){ return q->count; } int QueueSize( Queue *q){ return q->count; }

Sample Program 1 void main() { int a = 3, b = 8, c = 12; Queue q; CreateQueue(&q); //create a queue Append(a, &q); //insert value of a in queue Append(c, &q); //insert value of c in queue Serve(&b,&q); //delete entry in queue ;store in b printf("B is now : %d\n", b); //display value b } output??? 15

Circular Queues in C -Linked List Concept- 16 PART 2

Queues Implementation 17 Linked List Overview

Data Structure typedef char QueueEntry; typedef struct queuenode { QueueEntry info; struct queuenode *next; } QueueNode; typedef struct queue { QueueNode *front; QueueNode *rear; } Queue; Queue *z; 18

Create Queue void CreateQueue(Queue *q){ q->front = q->rear = NULL; } 19 bool QueueEmpty( Queue *q) { return(q->front == NULL); } Test Empty

Create Node QueueNode *CreateNode(QueueEntry x) { QueueNode *p; p=QueueNode*)malloc(sizeof(QueueNode)); if(!p) printf("Unable to allocate memory "); else { p->info = x; p->next = NULL; } return p; } 20

Append void Append(QueueEntry x, Queue *q) { QueueNode *np; np=CreateNode(x); if (np == NULL) printf ("Can’t append – queue full" ); else if (QueueEmpty(q)) q->front = q->rear = np; else { q->rear->next = np; q->rear = np; } } 21

Serve void Serve(QueueEntry *x, Queue *q) { QueueNode *p; if(QueueEmpty(q)) printf (“Fail…Queue is empty"); else { p = q->front; q->front = q->front->next; if(QueueEmpty(q)) // if(q->front == NULL) q-> rear = NULL; *x = p->info; free(p); } } 22

Main () void main() { char alp; CreateQueue(z); Append('i',z); Append('f',z); Append('a',z); Serve(&alp,z); printf("Alp is %c \n",alp); } output??? 23

Queue Application Queue is used to synchronize between two different processes that run at different speed. For instance, CPU and Printer. A buffer or a spooler that uses queue techniques introduced, store all the printed item pass by CPU. It’s because of the CPU operation is faster. By using a spooler, CPU time can be better utilized. Queue can also be implemented as input/output buffer in operating system that wants to handle many devices with different speeds. 24

Circular Queues in C -Linked List Concept- 25 EXERCISE

26 Question

27 Question a) a)Give the data structure of the queue. a) a)Assume a queue Q of type above has been created and initialized. Write the function that receives Q as its parameter and compute the total amount of all orders.

That’s all for today TQ.. 28