SlideShare una empresa de Scribd logo
1 de 20
CHAPTER FOUR
part II: Queue
1
Mulugeta G.
Queue
• A data structure that has access to its data at the front and rear
• Operates on FIFO (First In First Out) method
• Has two operations
• Enqueue: inserting data at the rear of the queue
• Dequeue: removing data at the front of the queue
2
front
rear
enqueue
dequeue
A
B
C
Queue
• Example:
• people waiting in line for ticket form a queue.
3
Queue
• Example:
4
Queue
• FIFO Principle of Queue:
– A Queue is like a line waiting to purchase tickets, where
the first person in line is the first person to be served. (i.e.,
First come first serve).
– The first entry that will be removed from the queue, is
called the front of the queue
– The one most recently added, is called the rear
5
Queue
• Basic Operations of Queue
• Enqueue(): Add an element to the end of the queue.
• Dequeue(): Remove an element from the front of the
queue.
• IsEmpty(): Check if the queue is empty.
• IsFull(): Check if the queue is full.
• Peek(): Get the value of the front of the queue without
removing it.
6
Queue
• Basic Operations of Queue
• peek(): Algorithm
•Implementation
7
int peek()
{
return queue[front];
}
begin procedure peek
return queue[front]
end procedure
Queue
• Basic Operations of Queue
•isfull(): Algorithm
Implementation
8
begin procedure isfull
if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
bool isfull()
{
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
Queue
• Basic Operations of Queue
• isempty(): Algorithm
9
begin procedure isempty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif
end procedure
Queue
• Basic Operations of Queue
• isempty(): Implementation
• If the value of front is less than MIN or 0, it tells that the queue
is not yet initialized, hence empty.
10
bool isempty()
{
if(front < 0 || front > rear)
return true;
else
return false;
}
Queue
 Queue operations work as follows:
• Two pointers, FRONT and REAR, are required
• FRONT track the first element of the queue
• REAR track the last element of the queue
• Initially, set value of FRONT and REAR to -1
11
Queue
 enqueue()
• Steps to enqueue (insert) data into a queue:
1. Check if the queue is full or not
2. If the queue is full, produce overflow error and exit.
3. If the queue is not full, increment rear pointer to point the next
empty space.
4. Add data element to the queue location, where the rear is pointing.
5. return success.
12
Queue
 enqueue()
13
Algorithm Implementation
begin procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
int enqueue(int data)
{
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
}
Queue
 dequeue()
• steps to perform dequeue operation:
1. Check if the queue is empty or not.
2. If the queue is empty, produce underflow error and exit.
3. If the queue is not empty, access the data where front is pointing.
4. Increment front pointer to point to the next available data element.
5. Return success.
14
Queue
 dequeue()
15
Algorithm Implementation
begin procedure dequeue
if queue is empty
return underflow
endif
data = queue[front]
front ← front + 1
return true
end procedure
int dequeue()
{
if(isempty())
return 0;
int data = queue[front];
front = front + 1;
return data;
}
Typesof Queue
16
In Linear Queue, an insertion takes
place from one end while the
deletion occurs from another end.
It is similar to the linear Queue
except that the last element of
the queue is connected to the
first element.
It is a special type of queue
data structure in which every
element has a priority
associated with it.
In Double Ended Queue, insertion and
deletion can be done from both ends of the
queue either from the front or rear.
Queue Implementation
• Two ways of implementing the Queue: Array and Linked list.
Using Array
17
void enqueue(int queue[], int item)
{
if (isfull())
{
cout<<"overflow";
}
else
{
rear = rear + 1;
queue[rear]=item;
}
}
int dequeue (int queue[], int item)
{
if (isempty())
{
cout<<"underflow";
}
else
{
item = queue[front];
front = front + 1;
return item;
}
}
Queue Implementation
• Two ways of implementing the Queue: Array and Linked list.
Using Linked List
18
void enqueue(struct node *ptr, int item) {
ptr = (struct node *) malloc (sizeof(struct
node)); if(ptr == NULL) {
cout<<"nOVERFLOWn";
return; }
else {
ptr -> data = item;
if(front == NULL) {
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL; }
else {
rear -> next = ptr;
rear = ptr;
rear->next = NULL; } } }
void dequeue
(struct node *ptr)
{
if(front == NULL)
{
cout<<"nUNDERFLOWn
";
return;
}
else
{
ptr = front;
front = front -> next;
delete ptr;
}
}
Applicationof Queue
i. In operating systems for handling interrupts
ii. To maintain the play list in media players in order to add and
remove the songs from the play-list.
iii. Disk Driver- maintains a queue of disk input/output requests
iv. Task scheduler in multiprocessing system- maintains priority
queues of processes
v. Telephone calls in a busy environment –maintains a queue of
telephone calls
vi. Simulation of waiting line- maintains a queue of persons
19
PART III
Stack
20

Más contenido relacionado

Similar a @Chapter 4 DSA Part II.pptx

Similar a @Chapter 4 DSA Part II.pptx (20)

Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Unit-ii-Queue ADT.pptx
Unit-ii-Queue ADT.pptxUnit-ii-Queue ADT.pptx
Unit-ii-Queue ADT.pptx
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
QUEUE PPT BY KULJIT SINGH.pptx
QUEUE PPT BY KULJIT SINGH.pptxQUEUE PPT BY KULJIT SINGH.pptx
QUEUE PPT BY KULJIT SINGH.pptx
 
Queues
QueuesQueues
Queues
 
Queue
QueueQueue
Queue
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Queues
Queues Queues
Queues
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 

Último

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Último (20)

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

@Chapter 4 DSA Part II.pptx

  • 1. CHAPTER FOUR part II: Queue 1 Mulugeta G.
  • 2. Queue • A data structure that has access to its data at the front and rear • Operates on FIFO (First In First Out) method • Has two operations • Enqueue: inserting data at the rear of the queue • Dequeue: removing data at the front of the queue 2 front rear enqueue dequeue A B C
  • 3. Queue • Example: • people waiting in line for ticket form a queue. 3
  • 5. Queue • FIFO Principle of Queue: – A Queue is like a line waiting to purchase tickets, where the first person in line is the first person to be served. (i.e., First come first serve). – The first entry that will be removed from the queue, is called the front of the queue – The one most recently added, is called the rear 5
  • 6. Queue • Basic Operations of Queue • Enqueue(): Add an element to the end of the queue. • Dequeue(): Remove an element from the front of the queue. • IsEmpty(): Check if the queue is empty. • IsFull(): Check if the queue is full. • Peek(): Get the value of the front of the queue without removing it. 6
  • 7. Queue • Basic Operations of Queue • peek(): Algorithm •Implementation 7 int peek() { return queue[front]; } begin procedure peek return queue[front] end procedure
  • 8. Queue • Basic Operations of Queue •isfull(): Algorithm Implementation 8 begin procedure isfull if rear equals to MAXSIZE return true else return false endif end procedure bool isfull() { if(rear == MAXSIZE - 1) return true; else return false; }
  • 9. Queue • Basic Operations of Queue • isempty(): Algorithm 9 begin procedure isempty if front is less than MIN OR front is greater than rear return true else return false endif end procedure
  • 10. Queue • Basic Operations of Queue • isempty(): Implementation • If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty. 10 bool isempty() { if(front < 0 || front > rear) return true; else return false; }
  • 11. Queue  Queue operations work as follows: • Two pointers, FRONT and REAR, are required • FRONT track the first element of the queue • REAR track the last element of the queue • Initially, set value of FRONT and REAR to -1 11
  • 12. Queue  enqueue() • Steps to enqueue (insert) data into a queue: 1. Check if the queue is full or not 2. If the queue is full, produce overflow error and exit. 3. If the queue is not full, increment rear pointer to point the next empty space. 4. Add data element to the queue location, where the rear is pointing. 5. return success. 12
  • 13. Queue  enqueue() 13 Algorithm Implementation begin procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure int enqueue(int data) { if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; }
  • 14. Queue  dequeue() • steps to perform dequeue operation: 1. Check if the queue is empty or not. 2. If the queue is empty, produce underflow error and exit. 3. If the queue is not empty, access the data where front is pointing. 4. Increment front pointer to point to the next available data element. 5. Return success. 14
  • 15. Queue  dequeue() 15 Algorithm Implementation begin procedure dequeue if queue is empty return underflow endif data = queue[front] front ← front + 1 return true end procedure int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data; }
  • 16. Typesof Queue 16 In Linear Queue, an insertion takes place from one end while the deletion occurs from another end. It is similar to the linear Queue except that the last element of the queue is connected to the first element. It is a special type of queue data structure in which every element has a priority associated with it. In Double Ended Queue, insertion and deletion can be done from both ends of the queue either from the front or rear.
  • 17. Queue Implementation • Two ways of implementing the Queue: Array and Linked list. Using Array 17 void enqueue(int queue[], int item) { if (isfull()) { cout<<"overflow"; } else { rear = rear + 1; queue[rear]=item; } } int dequeue (int queue[], int item) { if (isempty()) { cout<<"underflow"; } else { item = queue[front]; front = front + 1; return item; } }
  • 18. Queue Implementation • Two ways of implementing the Queue: Array and Linked list. Using Linked List 18 void enqueue(struct node *ptr, int item) { ptr = (struct node *) malloc (sizeof(struct node)); if(ptr == NULL) { cout<<"nOVERFLOWn"; return; } else { ptr -> data = item; if(front == NULL) { front = ptr; rear = ptr; front -> next = NULL; rear -> next = NULL; } else { rear -> next = ptr; rear = ptr; rear->next = NULL; } } } void dequeue (struct node *ptr) { if(front == NULL) { cout<<"nUNDERFLOWn "; return; } else { ptr = front; front = front -> next; delete ptr; } }
  • 19. Applicationof Queue i. In operating systems for handling interrupts ii. To maintain the play list in media players in order to add and remove the songs from the play-list. iii. Disk Driver- maintains a queue of disk input/output requests iv. Task scheduler in multiprocessing system- maintains priority queues of processes v. Telephone calls in a busy environment –maintains a queue of telephone calls vi. Simulation of waiting line- maintains a queue of persons 19