Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.

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

QUEUE Education is the best friend. An educated person is respected everywhere. Education beats the beauty and the youth. Chanakya.
Stacks, Queues, and Linked Lists
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
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 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
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 Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
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.
Queue Overview Queue ADT Basic operations of queue
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CHP-4 QUEUE.
Reference: Vinu V Das, Principles of Data Structures using C and C++
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Queues EENG212 Algorithm And Data Structures. DEFINITION OF QUEUE A Queue is an ordered collection of items from which items may be deleted at one end.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Introduction Queue is a linear Data Structure in which the operations are performed based on FIFO (First In First Out) principle.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
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
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)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
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.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
UNIT-1 UNIT – I Introduction to data structures: Abstract data type (ADT), Stacks and Queues circular queues and their implementation with arrays. Stack.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field.
 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.
STACKS & QUEUES for CLASS XII ( C++).
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
UNIT II Queue.
QUEUES.
Program based on queue & their operations for an application
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stack and Queue APURBO DATTA.
Algorithms and Data Structures
Queues Mohammad Asad Abbasi Lecture 5
Stack and Queue.
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Queues FIFO Enqueue Dequeue Peek.
Abstract Data Type Abstract Data Type as a design tool
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Queues.
Presentation transcript:

Data Structures Queue Namiq Sultan 1

Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items may be deleted form the end (front). A queue is logically a first in first out (FIFO) linear data structure. front rear 2

Queue  Representing queues const MAX = 8; struct queue{ int items[MAX]; int front, rear; } q;  Queue Basic Operations  add(q, x) Adds item x at the rear of the queue q  x = remove(q) Removes the front element from the queue q. q q.rear q.front q.items[0] q.items[7] 3

Queue  Demonstration q.front =0; q.rear = -1; add(q,4); add(q, 7); add(q, 3); x=remove(q); y=remove(q); add(q, 9); q.rear q.front Initial conditions 4

Important !!!!! If q.rear==MAX-1 we cannot add elements even though there are free cells in the front of the queue. What will we do? 5

Queue This problem can be solved by one of the following: 1. Shifting the elements whenever q.rear equals MAX-1 and there are empty cells in the front of the queue. 2. Converting this linear queue to a circular queue. 6

Circular Queue q.front To add an element, the element is inserted in first location of the queue. It behaves like a circular list. Add(q, 5) q.rear

Circular Queue q.rear q.front The previous initial condition: q.fron = 0 q.rear = -1 is not suitable here. It is difficult to differentiate between an empty queue and full queue. 8

Circular Queue Solution: Make front points to the element immediately preceding the first item. Initial condition: front = rear = 0; The following conditions are satisfied:  Queue is empty if: front == rear  Queue is full if front == rear + 1 OR front == 0 && rear == MAX-1 9

Program 5.1 //PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY #include # using namespace std; const MAX = 10; class c_queue{ int cq_arr[MAX]; int front,rear; public: //a constructor is created to initialize the variables c_queue() { front= 0; rear = 0; } //public function declarations void insert(); void del(); void display(); }; 10

Program 5.1 void c_queue::insert() { int added_item; //Checking for overflow condition if ((front == 0 && rear == MAX-1) || (front == rear +1)) { cout<<"\nQueue Overflow \n"; return; } else if (rear == MAX-1) /*rear is at last position of queue */ rear = 0; else rear = rear + 1; cout<<"\nInput the element for insertion in queue:"; cin>>added_item; cq_arr[rear] = added_item; }/*End of insert()*/ 11

Program 5.1 //This function will delete an element from the queue void c_queue::del() { //Checking for queue underflow if (front == rear) { cout<<"\nQueue Underflow\n"; return; } else if (front == MAX-1) front = 0; else front = front + 1; cout<<"\nElement deleted from queue is:"<<cq_arr[front]<<"\n"; }/*End of del()*/ 12

Program 5.1 // Display the elements in the queue void c_queue::display() { int front_pos = front, rear_pos = rear; if (front == rear) { // is empty or not cout<<"\nQueue is empty\n"; return; } cout<<"\nQueue elements:\n"; //Displaying the queue elements if (front_pos < rear_pos ) while(front_pos < rear_pos){ cout<<cq_arr[++front_pos]<<", "; } else{ while(front_pos < MAX-1){ cout<<cq_arr[++front_pos]<<", " ; } front_pos = -1; while(front_pos < rear_pos){ cout<<cq_arr[++front_pos]<<", " ; } } /*End of else*/ cout<<"\n"; } /*End of display() */ 13

Program 5.1 void main() { int choice; c_queue co; //Creating the object while(1){ //Menu options cout <<"\n1. Insert\n"; cout <<"2. Delete\n"; cout <<"3. Display\n"; cout <<"4. Quit\n"; cout <<"\n Enter your choice: "; cin>>choice; switch(choice){ case 1: co.insert(); break; case 2 : co.del(); break; case 3: co.display(); break; case 4: exit(1); default: cout<<"\nWrong choice\n"; } /*End of switch*/ } /*End of while*/ } /*End of main()*/ 14

APPLICATIONS OF QUEUE 1. Round robin techniques for processor scheduling is implemented using queue. 2. Printer server routines (in drivers) are designed using queues. 3. All types of customer service software (like Railway/Air ticket reservation) are designed using queue to give proper service to the customers. 15