QUEUES.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
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.
Lecture - 9 On Queues. Prepared by, Jesmin Akhter, Lecturer, IIT,JU QUEUES A Queue is a linear list of elements in which deletions can take place only.
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.
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
DATA STRUCTURE & ALGORITHMS
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
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.
Reference: Vinu V Das, Principles of Data Structures using C and C++
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Data Structures Using C++
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.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and 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.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
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.
STACKS & QUEUES for CLASS XII ( C++).
Programming Circular Linked List.
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,
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
UNIT II Queue.
Queues Chapter 4.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Algorithms and Data Structures
QUEUE.
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, Queues
DATA STRUCTURE QUEUE.
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
CSC 248 – fundamentals of Data structure
Data Structures – Week #4
Data Structures Using C++ 2E
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

QUEUES

Definition A queue is a set of the same type in which elements are added at one end, called back or rear, and deleted from the other end, called front. Queue is used in a system which is modeled on the First In First Out The rear of the queue is accessed whenever a new element is added to the queue, and the front of the queue is accessed whenever an element is deleted from the queue.

Queue operations initializeQueue: Initializes the queue to an empty state isEmptyQueue: Determines whether the queue is empty isFullQueue: Determines whether the queue is full. front: Returns the front, that is the first element back: Returns the last element of the queue addQueue: Adds a new element to the rear of the queue deleteQueue: Removes the front element from the queue3

Implementation of Queues as Arrays Initially, the queue is empty. After operation: addQueue(Queue, ‘A’); After two more addQueue operations: addQueue(Queue, ‘B’); addQueue(Queue, ‘C’);

deletion Consider the deleteQueue operation: deleteQueue(); Will this design work? Suppose A stands for adding and D stands for deleting. AAADADADADADADADA…

problem... AAADADADADADADADA… The sequence of operations would eventually set the index queueRear to point to the last array position, giving the impression that the queue is full.

solution… One solution to this is that when the queue overflows to the rear (that is, queueRear points to the last array position), we can check the value of the index queueFront indicates that there is room in the front of the array, then when queueRear gets to the last array position, we can slide all of the queue elements toward the first array position. This solution is good if the queue size is very small; otherwise, the program may execute more slowly.

another solution… Other solution to this problem is to assume that the array is circular – that is, the first array position immediately follows the last array position We will consider the array containing the queue to be circular, although we will draw the figures of the array holding the queue elements as before

Implementation to arrays 1 #include<iostream> #include<cstdlib> //#define MAX 5 using namespace std; void insert(); void del(); void display(); int const MAX=5; int queue[MAX]; int rear=-1; int front=-1; int main() { int choice; while(1) cout<<"\n\nMAIN MENU\n"; cout<<"1.Insert\n"; cout<<"2.Delete\n"; cout<<"3.Display\n"; cout<<"4.Exit"; cout<<"\n\nEnter Your Choice: ";

void insert() { int item; if(rear==MAX-1) cout<<"\nQueue Full!!!\n"; } else if(front==-1) front=0; cout<<"\nEnter the Element: "; cin>>item; rear=rear+1; queue[rear]=item ; cin>>choice; switch(choice) { case 1: insert(); display(); break; case 2: del(); case 3: case 4: exit(0); default: cout<<"\nWrong Choice!!! Try Again.\n"; } return 0;

void del() { if(front==-1||front>rear) cout<<"\nQueue Empty!!!\n"; return ; } else cout<<"\nElement Deleted from Queue is "<<queue[front]<<"\n"; front=front+1; display(); void display() { int i; if(front==-1||front>rear) cout<<"\nQueue Empty!!!\n"; } else cout<<"\nQueue is:\n"; for(i=front;i<=rear;i++) cout<<queue[i]<<" "; cout<<"\n";

Implementation using LL while (c < n) { cout<<"Enter the value to be entered into queue\n"; cin>>x; push(x); c++; } cout<<"\n\nRemoved Values\n\n"; while(true) if (front != NULL) cout<<remove()<<endl; else break; #include<iostream> #include<stdio.h> #include<conio.h> using namespace std; struct node { int data; node *next; }*front = NULL,*rear = NULL,*p = NULL,*np = NULL; int main() int n, c = 0, x; cout<<"Enter the number of values to be pushed into queue\n"; cin>>n;

cout<<"empty queue\n"; } else p = front; x = p->data; int remove() { int x; if(front == NULL) cout<<"empty queue\n"; } else p = front; x = p->data; front = front->next; delete(p); return(x); void push(int x) { np = new node; np->data = x; np->next = NULL; if(front == NULL) front = rear = np; rear->next = NULL; } else rear->next = np; rear = np;