Professional Documents
Culture Documents
CHAPTER 6:
QUEUES
MOHAMED HASSAN ABDULLE
HORMUUD UNIVERSITY
EMAIL: JALAQSANE2003@GMAIL.COM
FACEBOOK: MOHAMED HASSAN UGASKA
Mobile: +252615825152 Or +252615000196
HTTP://HU.EDU.SO/
Course Name: Data Structure Lecturer : Eng . Ugaska
Chapter Objectives
Objectives
10 Front = 0
Rear = 0
Front
10 3 Front = 0
Rear = 1
Fig. 3. Push (3)
Rear
Stack examples
What is Queues?
Definition
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 ‘.
Front and rear are also called head and tail respectively.
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.
Cont…….
In computer science, queuing refers to lining up
jobs for a computer or device. For example, if you
want to print a number of documents, the
operating system (or a special print spooler)
queues the documents by placing them in a
special area called a print buffer or print queue.
The printer then pulls the documents off the
queue one at a time.
Another term for this is print spooling .
The order in which a system executes jobs on a
queue depends on the priority system being used.
Queues
Queues 14
Queues
12 69 50 32 78 88
rear front
15
Queue Applications
Real life examples
Waiting in line
Applications related to Computer Science
Threads: way for a program to divide split itself into two or more simultaneously
running tasks.
D rear
C rear C D rear
rear B B C
rear B
A front A front A front B front
front A
Queue can be implemented in two ways:
1. Using arrays (static)
2. Using pointers (dynamic)
Implementation of queue using pointers will be
discussed in later chapter . Let us discuss
underflow and overflow conditions when a queue
is implemented using arrays.
If we try to pop (or delete or remove) an element
from queue when it is empty, underflow occurs. It
is not possible to delete (or take out) any element
when there is no element in the queue.
ALGORITHM FOR QUEUE
OPERATIONS
Let Q be the array of some specified size say SIZE
INSERTING AN ELEMENT INTO THE QUEUE
1. Initialize front=0 rear = –1
2. Input the value to be inserted and assign to variable “data”
3. If (rear >= SIZE)
(a) Display “Queue overflow”
(b) Exit
4. Else
(a) Rear = rear +1
5. Q[rear] = data
6. Exit
DELETING AN ELEMENT FROM QUEUE
1. If (rear< front)
(a) Front = 0, rear = –1
(b) Display “The queue is empty”
(c) Exit
2. Else
(a) Data = Q[front]
3. Front = front +1
4. Exit
Simple example of queue in C++
#include<iostream>
#define MAX 50
int queue_arr[MAX];
{
{
cout<<"\nQueue Underflow\n";
return;
}
else
{
//deleteing the element
cout<<"\nElement deleted from queue is : %d\n";
queue_arr[front];
front=front+1;
}
}/*End of del()*/
void display()
{
int i;
//Checking whether the queue is empty or not
if (front == -1 || front > rear)
{
cout<<"\nQueue is empty\n";
return;
}
else
{
cout<<"\nQueue is :\n";
for(i=front;i<= rear;i++)
cout<<queue_arr[i];
cout<<"\n";
}
}/*End of display() */
int main()
{
int choice;
while (1)
{
//Menu options
cout<<"\n1.Insert\n";
cout<<"\n2.Delete\n";
cout<<"\n3.Display\n";
cout<<"4.Quit\n";
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1 :
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
return 0;
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
OTHER QUEUES
Questions
?