Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
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 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.
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.
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.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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 CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
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)
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,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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.
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 –
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
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.
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.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
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
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Data Structures Using C, 2e
Queues.
CS505 Data Structures and Algorithms
UNIT II Queue.
CC 215 Data Structures Queue ADT
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Queue.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
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.
DATA STRUCTURE QUEUE.
CSC 143 Queues [Chapter 7].
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Queues FIFO Enqueue Dequeue Peek.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
Data Structures – Week #4
Getting queues right … finally (?)
Presentation transcript:

Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.

Definition of a Queue A queue is a data structure that models/enforces the first-come first-serve order, or equivalently the first-in first-out (FIFO) order. That is, the element that is inserted first into the queue will be the element that will deleted first, and the element that is inserted last is deleted last. A waiting line is a good real-life example of a queue. (In fact, the Britich word for “line” is “queue”.)

A Graphic Model of a Queue Head: All items are deleted from this end Tail: All new items are added on this end

Applications of Queues -- Queue is a linear data structure that permits insertion of new element at one end and deletion of an element at the other end. -- The end at which insertion of a new element can take place is called ‘ rear ‘ and the end at which deletion of an element take place is called ‘ front ‘. -- The first element that gets added into queue is the first one to get removed from the list, Hence Queue is also referred to as First-In-First-Out ( FIFO ) list. -- Queue must be created as empty. -- Whenever an element is inserted into queue, it must be checked whether the queue is full or not. -- Whenever an element is deleted form queue, it must be checked whether the queue is empty or not. -- We can implement the queue ADT either with array or linked list. Queue ADT struct queueNode { int data; struct queueNode *next; }; init_queue( ) addq ( ) delq ( ) isEmpty ( ) printQueue ( ) 4 rear front 3 6 8 2 5 addq (4) delq ( ) 7 Applications of Queues Execution of Threads Job Scheduling Event queuing Message Queueing Types of Queues circular queues priority queues double-ended queues

Operations on Queues Insert(item): (also called enqueue) It adds a new item to the tail of the queue Remove( ): (also called delete or dequeue) It deletes the head item of the queue, and returns to the caller. If the queue is already empty, this operation returns NULL getHead( ): Returns the value in the head element of the queue getTail( ): Returns the value in the tail element of the queue isEmpty( ) Returns true if the queue has no items size( ) Returns the number of items in the queue

Examples of Queues An electronic mailbox is a queue The ordering is chronological (by arrival time) A waiting line in a store, at a service counter, on a one-lane road Equal-priority processes waiting to run on a processor in a computer system

Queue as a Class Much like stacks and linked lists were designed and implemented as classes, a queue can be conveniently packaged as a class It seems natural to think of a queue as similar to a linked list, but with more basic operations, to enforce the FIFO order of insertion and deletion

A Rough Class for Queue class Queue{ public: typedef int datatype; // datatype is the type of items to be //added to the queue. By changing int to some other type, the // queue is easily changed to handle other data types Queue( ); void enqueue(datatype x); datatype dequeue( ); datatype peekHead( ); datatype peekTail( ); int size( ); bool isEmpty( ); private: //A container for items. It’s determined in the implementation };

A Linked List Implementation of the Queue Class The container for items is a linked list, that is, an object of type List Let’s call it: List q;

A Class for Queue: A Linked List Implementation class Queue{ public: typedef int datatype; Queue( ) { }; void enqueue(datatype x) {q.insertTail(x);}; datatype peekHead( ) {assert(size()>0); q.getHead( )->getData( );}; datatype peekTail( ) {assert(size()>0); q.getTail( )->getData( );}; datatype dequeue( ) {assert(size()>0); int x=peekHead( ); q.removeHead( ); return x;}; int size( ) {return q.size( );}; bool isEmpty( ) {return q.isEmpty( );}; private: List q; }; // Time: All member functions take O(1) time.

A Dynamic-Array Implementation of the Queue Class The container for items is a dynamic array It is accomplished as: datatype *p=new datatype[50];

A Class for Queue using Dynamic Arrays class Queue{ public: typedef int datatype; Queue(int capacity = 50) ; void enqueue(datatype x); datatype dequeue( ); datatype peekHead( ); datatype peekTail( ); int size( ); bool isEmpty( ); private: int capacity; // default value is 50 datatype *p; // pointer a dynamic array created by constructor int length; // number of actual elements in the queue int head; // index of the head element in the array int tail; // index of the tail element in the array };

How head and tail Change head increases by 1 after each dequeue( ) tail increases by 1 after each enqueue( ) head tail Now: 1 2 47 48 49 4 3 head tail After enqueue: 1 2 47 48 49 4 3 head tail After dequeue: 1 2 47 48 49 4 3

False-Overflow Issue First Suppose 50 calls to enqueue have been made, so now the queue array is full Assume 4 calls to dequeue( ) are made Assume a call to enqueue( ) is made now. The tail part seems to have no space, but the front has 4 unused spaces; if never used, they are wasted. 49 48 47 4 3 2 1 49 48 47 4 3 2 1

Solution: A Circular Queue Allow the head (and the tail) to be moving targets When the tail end fills up and front part of the array has empty slots, new insertions should go into the front end Next insertion goes into slot 0, and tail tracks it. The insertion after that goes into a lot 1, etc. head tail 1 2 47 48 49 4 3

Illustration of Circular Queues Current state: After One Call to enqueue() head 1 2 47 48 49 3 4 tail head tail 1 2 47 48 49 3 4 head tail 1 2 47 48 49 3 4

Numerics for Circular Queues head increases by (1 modulo capacity) after each dequeue( ): head = (head +1) % capacity; tail increases by (1 modulo capacity) after each enqueue( ): tail = (tail +1) % capacity;

Complete Implementation of Queues with Dynamic Arrays Show the class structure as a refresher (next slide) The implementations of the constructor and member functions will follow afterwards

The Class Structure (revisited) class Queue{ public: typedef int datatype; Queue(int capacity = 50) ; void enqueue(datatype x); datatype dequeue( ); datatype peekHead( ); datatype peekTail( ); int size( ); bool isEmpty( ); private: int capacity; // default value is 50 datatype *p; // pointer a dynamic array created by constructor int length; // number of actual elements in the queue int head; // index of the head element in the array int tail; // index of the tail element in the array };

Implementations of the Members Queue::Queue(int capacity){ this.capacity = capacity; p = new datatype[capacity]; length=0; head=0; tail=0; }; int Queue::size( ){return length;}; bool Queue::isEmpty( ) {return (length==0);}; Queue::datatype Queue::peekHead( ){assert(length>0);return p[head];}; Queue::datatype Queue::peekTail( ){assert(length>0); return p[tail];}; Time: All O(1) time.

Implementation enqueue( ) void Queue::enqueue(datatype x){ if (length==0) {p[tail]=x; length++;} else if (length<capacity) {length++; tail=tail+1 % capacity; p[tail]=x; } else {// Filled. Create a new array twice big. Copy current array to it. Add x. datatype *q= new datatype[2*capacity]; int j = head; // copy filled array p (from head to tail) to array q[0…] for (int i=0;i<capacity;i++) {q[i]=p[j]; j=j+1 % capacity;} head = 0; tail = capacity; // pointing to the first empty slot for now q[tail]=x; // put x in the first empty slot length++; // account for adding x capacity = 2*capacity; // reflect that capacity is now doubled delete [] p; // deletes the old array pointed to by p p =q; // makes p point to the new array. } }; Time: O(1) except at overflow, in which case O(length).

Implementation dequeue( ) Queue::datatype Queue::dequeue( ){ assert(length>0); datatype x= p[head]; head=head+1 % capacity; return x; }; Time: O(1).

A Graphic Model of a Queue Head: All items are deleted from this end Tail: All new items are added on this end

Implementing Queue ADT using Array int queue[10] ,front, rear ; void init_queue() { front = rear = -1 ; } void addq ( int item ){ if ( rear == 9 ) { printf("\nQueue is full"); return ; rear++ ; queue [ rear ] = item ; if ( front == -1 )front = 0 ; int delq( ){ int data ; if ( front == -1 ) { printf("\nQueue is Empty"); return 0; data = queue[front] ; queue[front] = 0 ; if ( front == rear ) front = rear = -1 ; else front++ ; return data ; void display() { int i; if(front==-1) printf("\nQueue is empty."); else { printf("\nElements are : \n"); for (i=front;i<=rear;i++) printf("%5d",queue[i]); } int main() { int ch,num; init_queue(); do { printf("\n\tMENU\n\n1. Add to Queue”); printf(“\n2. Delete form Queue"); printf("\n3. Display Queue\n4. Exit."); printf("\n\n\tYour Choice: "); scanf("%d",&ch); switch(ch) case 1: printf("\nEnter an element : "); scanf("%d",&num); addq(num);break; case 2: num=delq(); printf("\nElement deleted : %d",num); break; case 3: display(); break; case 4: exit(0); default: printf("\nInvalid option.."); }while(1);

Implementing Queue ADT using Liked List struct q_node { int data; struct q_node *next; }*rear,*front; void init_queue() { rear=NULL; front=NULL; } void addq(int item) { struct q_node *t; t=(struct q_node*)malloc(sizeof(struct q_node)); t->data=item; t->next=NULL; if(front==NULL) rear=front=t; else { rear->next=t; rear=rear->next; int delq() { struct q_node *temp; if(front==NULL) { printf("\nQueue is empty."); return 0; } else { int num = front->data; temp = front; front=front->next; free(temp); return num; void display() { struct q_node *temp=front; if(front==NULL) printf("\nQueue is empty."); else { printf("\nElements in Queue :\n"); while(temp!=NULL) { printf("%5d",temp->data); temp=temp->next; } int main() { int ch,num; init_queue(); do { printf("\n\tMENU\n\n1. Add\n2. Delete"); printf("\n3. Display Queue\n4. Exit."); printf("\n\n\tYour Choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter an element : "); scanf("%d",&num); addq(num);break; case 2: num=delq(); printf("\nElement deleted : %d",num); break; case 3: display(); break; case 4: exit(0); default:printf("\nInvalid option.."); }while(1);