SlideShare a Scribd company logo
Data Structure
Lecture No. 9
Queue
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
International Islamic University H-10, Islamabad, Pakistan
Video Lecture
 A stack is LIFO (Last-In First Out) structure.
 In contrast, a queue is a FIFO (First-In First-Out ) structure.
 A queue is a linear structure for which items can be only inserted at one end
and removed at another end.
Queue Operations
 Put(X) place X at the rear of the queue.
 Get() remove the front element and return it.
 Front() return front element without removing it.
 Is_Empty() return TRUE if queue is empty, FALSE otherwise
 Is_Full() return TRUE if queue is full, FALSE otherwise
Queues
Implementing Queue using Array
Index No. 0 1 2 3 Head Tail Count
A Queue -1 -1 0 An empty queue
Put(‘A’) A -1 0 1 Puts ‘A’ in Queue
Put(‘B’) A B -1 1 2 Puts ‘B’ in Queue
Put(‘C’) A B C -1 2 3 Puts ‘C’ in Queue
Put(‘D’) A B C D -1 (3) → -1 4 Puts ‘D’ in Queue
Put(‘E’) A B C D -1 -1 4 Error: Queue is full
Get() B C D 0 -1 3 Gets ‘A’ from Queue
Put(‘F’) F B C D 0 0 4 Puts ‘F’ in Queue
Get() F C D 1 0 3 Gets ‘B’ from Queue
Get() F D 2 0 2 Gets ‘C’ from Queue
Get() F (3) → -1 0 1 Gets ‘D’ from Queue
Get() 0 0 0 Gets ‘F’ from Queue
Get() 0 0 0 Error: Queue is empty
#include <iostream>
using namespace std;
typedef char Type;
#define MAX 4
class Queue{
private:
Type qu[MAX];//array of any type
int head; //index of start of queue
int tail; // index of end of queue
int count; // number of items
public:
Queue::Queue();
bool Is_Full();
bool Is_Empty();
void Put(Type var);
Type Get();
Type Front();
void Show();
};
4
Example 1: Implementing Queue using Array
Queue::Queue(){ //constructor
head = -1; tail = -1;
count = 0;
}
bool Queue::Is_Full(){
return count >= MAX-1;
}
bool Queue::Is_Empty(){
return count <= 0;
}
1 2
void Queue::Put(Type var){
// insert at tail of Queue
if(count >= MAX)
cout <<"Error: Queue is full n";
else{
qu[++tail] = var;
count++;
}
if(tail >=MAX-1)
// wrap around if past array end
tail = -1;
}
5
Example 1: Implementing Queue using Array
Type Queue::Get(){
// remove item from queue head
if(count <= 0){ // if queue empty
cout <<"Error: Queue is Emptyn";
return -1;
}
Type temp = qu[++head]; //store item
count--;
if(head >= MAX-1){
// wrap around if past array end
head = -1;
}
return temp; //return item
}
3 4
Type Queue::Front(){
if(count <= 0){ // if queue empty
cout <<"Error: Queue is Emptyn";
return -1;
}
Type temp = qu[++head];
--head;
return temp; //return item
}
void Queue::Show(){
for(int i = 0 ; i<MAX ; i++){
cout << qu[i] <<" ";
}
cout << "Head = " << head
<< " Tail = " << tail << endl;
}
6
Example 1: Implementing Queue using Array
int main( ){
Queue q;
cout<<"Put An"; q.Put('A');
cout<<"Put Bn"; q.Put('B');
cout<<"Put Cn"; q.Put('C');
cout<<"Put Dn"; q.Put('D');
cout<<"Put E"; q.Put('E');
cout<<"Got "; cout << q.Get() << endl;
cout<<"Put Fn"; q.Put('F');
cout<<"Got "; cout << q.Get() << endl;
cout<<"Got "; cout << q.Get() << endl;
cout<<"Got "; cout << q.Get() << endl;
cout<<"Got "; cout << q.Get() << endl;
cout<<"Got "; cout << q.Get() << endl;
system("PAUSE");
return 0;
}
5 6
 Using linked List: Recall
 Insert works in constant time for either end of a linked list.
 Remove works in constant time only.
 Seems best that head of the linked list be the front of the queue so that all
removes will be from the front.
 Inserts will be at the end of the list.
Implementing Queue using Linked List
front
2
5
7
1 1 7 5 2
front
rear rear
front
2
5
7 1 7 5 2
front
rear rear
get()
front
2
5
7
9
7 5 2
front
rear
rear
put(9)
9
#include <iostream>
using namespace std;
typedef int Type;
struct Node{
Type data ;
Node *next ;
};
class Queue{
private :
Node *front, *rear ;
public :
Queue( ) ;
void Put ( Type Data ) ;
Type Get( ) ;
~Queue( ) ;
};
8
Example 2: Implementing Queue using Linked List
Queue :: Queue( ){
front = rear = NULL ;
}
void Queue :: Put ( Type Data ){
Node *newNode ;
newNode = new Node ;
if ( newNode == NULL )
cout << "nQueue is full" ;
newNode -> data = Data ;
newNode -> next = NULL ;
if ( front == NULL ){
rear = front = newNode ;
return ;
}
rear -> next = newNode ;
rear = rear -> next ;
}
1 2
Type Queue :: Get( ){
if ( front == NULL ){
cout << "Queue is empty" ;
return -1;
}
Node *current ;
Type Data ;
Data = front -> data ;
current = front ;
front = front -> next ;
delete current ;
if (front == NULL) rear = NULL;
return Data ;
}
Queue :: ~Queue( ){
if ( front == NULL )
return ;
Node *current ;
while ( front != NULL ){
9
Example 2: Implementing Queue using Linked List
current = front ;
front = front -> next ;
delete current ;
}
}
int main( ){
Queue a ;
a.Put ( 11 ) ;
a.Put ( -8 ) ;
a.Put ( 23 ) ;
a.Put ( 19 ) ;
cout << a.Get( ) << endl;
cout << a.Get( ) << endl;
cout << a.Get( ) << endl;
cout << a.Get( ) << endl;
cout << a.Get( ) << endl;
system("PAUSE");
return 0;
}
3 4
 The queue data structure is used in various CPU and disk scheduling. Here we
have multiple tasks requiring CPU or disk at the same time. The CPU or disk
time is scheduled for each task using a queue.
 The queue can also be used for print spooling wherein the number of print
jobs is placed in a queue.
 Handling of interrupts in real-time systems. The interrupts are handled in the
same order as they arrive i.e First come first served.
 Breadth-first Search in which the neighboring nodes of a tree are traversed
before moving on to next level uses a queue for implementation.
 In real life scenario, Call Center phone systems uses Queues to hold people
calling them in an order, until a service representative is free.
Applications of Queue
 Double ended queue is a more generalized form of queue data structure
which allows insertion and removal of elements from both the ends, i.e , front
and back.
Double Ended Queue
0 1 2 3 4 5 6
Front Rear
Pop Back
Push Back
Push Front
Pop Front
 Circular Queue is a linear data structure in which the
 operations are performed based on FIFO (First In First Out)
 principle and the last position is connected back to the first
 position to make a circle. It is also called ‘Ring Buffer’.
Circular Queue
 In priority queue, data when inserted, is stored based on its priority.
 When we remove a data from a priority queue, the data with least priority, will
get removed.
Priority Queue
9 0
Index
Number
7 1
7 2
6 3
5 4
3 5
1 6
Data with the highest priority
Data with the least priority

More Related Content

Similar to Data Structures and Agorithm: DS 09 Queue.pptx

05 queues
05 queues05 queues
05 queues
Rajan Gautam
 
Stack
StackStack
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
singhprpg
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
SSE_AndyLi
 
stack.ppt
stack.pptstack.ppt
stack.ppt
ssuserec1395
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
SherinRappai
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Stack
StackStack
Queue
QueueQueue
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
Pulkitmodi1998
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
ecomputernotes
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Katang Isip
 
Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
Ain-ul-Moiz Khawaja
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 

Similar to Data Structures and Agorithm: DS 09 Queue.pptx (20)

05 queues
05 queues05 queues
05 queues
 
Stack
StackStack
Stack
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Stack
StackStack
Stack
 
Queue
QueueQueue
Queue
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
 

Recently uploaded

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 

Data Structures and Agorithm: DS 09 Queue.pptx

  • 1. Data Structure Lecture No. 9 Queue Engr. Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan Video Lecture
  • 2.  A stack is LIFO (Last-In First Out) structure.  In contrast, a queue is a FIFO (First-In First-Out ) structure.  A queue is a linear structure for which items can be only inserted at one end and removed at another end. Queue Operations  Put(X) place X at the rear of the queue.  Get() remove the front element and return it.  Front() return front element without removing it.  Is_Empty() return TRUE if queue is empty, FALSE otherwise  Is_Full() return TRUE if queue is full, FALSE otherwise Queues
  • 3. Implementing Queue using Array Index No. 0 1 2 3 Head Tail Count A Queue -1 -1 0 An empty queue Put(‘A’) A -1 0 1 Puts ‘A’ in Queue Put(‘B’) A B -1 1 2 Puts ‘B’ in Queue Put(‘C’) A B C -1 2 3 Puts ‘C’ in Queue Put(‘D’) A B C D -1 (3) → -1 4 Puts ‘D’ in Queue Put(‘E’) A B C D -1 -1 4 Error: Queue is full Get() B C D 0 -1 3 Gets ‘A’ from Queue Put(‘F’) F B C D 0 0 4 Puts ‘F’ in Queue Get() F C D 1 0 3 Gets ‘B’ from Queue Get() F D 2 0 2 Gets ‘C’ from Queue Get() F (3) → -1 0 1 Gets ‘D’ from Queue Get() 0 0 0 Gets ‘F’ from Queue Get() 0 0 0 Error: Queue is empty
  • 4. #include <iostream> using namespace std; typedef char Type; #define MAX 4 class Queue{ private: Type qu[MAX];//array of any type int head; //index of start of queue int tail; // index of end of queue int count; // number of items public: Queue::Queue(); bool Is_Full(); bool Is_Empty(); void Put(Type var); Type Get(); Type Front(); void Show(); }; 4 Example 1: Implementing Queue using Array Queue::Queue(){ //constructor head = -1; tail = -1; count = 0; } bool Queue::Is_Full(){ return count >= MAX-1; } bool Queue::Is_Empty(){ return count <= 0; } 1 2
  • 5. void Queue::Put(Type var){ // insert at tail of Queue if(count >= MAX) cout <<"Error: Queue is full n"; else{ qu[++tail] = var; count++; } if(tail >=MAX-1) // wrap around if past array end tail = -1; } 5 Example 1: Implementing Queue using Array Type Queue::Get(){ // remove item from queue head if(count <= 0){ // if queue empty cout <<"Error: Queue is Emptyn"; return -1; } Type temp = qu[++head]; //store item count--; if(head >= MAX-1){ // wrap around if past array end head = -1; } return temp; //return item } 3 4
  • 6. Type Queue::Front(){ if(count <= 0){ // if queue empty cout <<"Error: Queue is Emptyn"; return -1; } Type temp = qu[++head]; --head; return temp; //return item } void Queue::Show(){ for(int i = 0 ; i<MAX ; i++){ cout << qu[i] <<" "; } cout << "Head = " << head << " Tail = " << tail << endl; } 6 Example 1: Implementing Queue using Array int main( ){ Queue q; cout<<"Put An"; q.Put('A'); cout<<"Put Bn"; q.Put('B'); cout<<"Put Cn"; q.Put('C'); cout<<"Put Dn"; q.Put('D'); cout<<"Put E"; q.Put('E'); cout<<"Got "; cout << q.Get() << endl; cout<<"Put Fn"; q.Put('F'); cout<<"Got "; cout << q.Get() << endl; cout<<"Got "; cout << q.Get() << endl; cout<<"Got "; cout << q.Get() << endl; cout<<"Got "; cout << q.Get() << endl; cout<<"Got "; cout << q.Get() << endl; system("PAUSE"); return 0; } 5 6
  • 7.  Using linked List: Recall  Insert works in constant time for either end of a linked list.  Remove works in constant time only.  Seems best that head of the linked list be the front of the queue so that all removes will be from the front.  Inserts will be at the end of the list. Implementing Queue using Linked List front 2 5 7 1 1 7 5 2 front rear rear front 2 5 7 1 7 5 2 front rear rear get() front 2 5 7 9 7 5 2 front rear rear put(9) 9
  • 8. #include <iostream> using namespace std; typedef int Type; struct Node{ Type data ; Node *next ; }; class Queue{ private : Node *front, *rear ; public : Queue( ) ; void Put ( Type Data ) ; Type Get( ) ; ~Queue( ) ; }; 8 Example 2: Implementing Queue using Linked List Queue :: Queue( ){ front = rear = NULL ; } void Queue :: Put ( Type Data ){ Node *newNode ; newNode = new Node ; if ( newNode == NULL ) cout << "nQueue is full" ; newNode -> data = Data ; newNode -> next = NULL ; if ( front == NULL ){ rear = front = newNode ; return ; } rear -> next = newNode ; rear = rear -> next ; } 1 2
  • 9. Type Queue :: Get( ){ if ( front == NULL ){ cout << "Queue is empty" ; return -1; } Node *current ; Type Data ; Data = front -> data ; current = front ; front = front -> next ; delete current ; if (front == NULL) rear = NULL; return Data ; } Queue :: ~Queue( ){ if ( front == NULL ) return ; Node *current ; while ( front != NULL ){ 9 Example 2: Implementing Queue using Linked List current = front ; front = front -> next ; delete current ; } } int main( ){ Queue a ; a.Put ( 11 ) ; a.Put ( -8 ) ; a.Put ( 23 ) ; a.Put ( 19 ) ; cout << a.Get( ) << endl; cout << a.Get( ) << endl; cout << a.Get( ) << endl; cout << a.Get( ) << endl; cout << a.Get( ) << endl; system("PAUSE"); return 0; } 3 4
  • 10.  The queue data structure is used in various CPU and disk scheduling. Here we have multiple tasks requiring CPU or disk at the same time. The CPU or disk time is scheduled for each task using a queue.  The queue can also be used for print spooling wherein the number of print jobs is placed in a queue.  Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive i.e First come first served.  Breadth-first Search in which the neighboring nodes of a tree are traversed before moving on to next level uses a queue for implementation.  In real life scenario, Call Center phone systems uses Queues to hold people calling them in an order, until a service representative is free. Applications of Queue
  • 11.  Double ended queue is a more generalized form of queue data structure which allows insertion and removal of elements from both the ends, i.e , front and back. Double Ended Queue 0 1 2 3 4 5 6 Front Rear Pop Back Push Back Push Front Pop Front
  • 12.  Circular Queue is a linear data structure in which the  operations are performed based on FIFO (First In First Out)  principle and the last position is connected back to the first  position to make a circle. It is also called ‘Ring Buffer’. Circular Queue
  • 13.  In priority queue, data when inserted, is stored based on its priority.  When we remove a data from a priority queue, the data with least priority, will get removed. Priority Queue 9 0 Index Number 7 1 7 2 6 3 5 4 3 5 1 6 Data with the highest priority Data with the least priority