DS Unit 3
DS Unit 3
UNIT – III
QUEUES
INTRODUCTION TO QUEUES
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 the deletion of an element take place is called
front, and the end at which insertion of a new element can take place
is called rear.
The deletion or insertion of elements can take place only at the front
or rear end called dequeue and enqueue.
The first element that gets added into the queue is the first one to get
removed from the queue.
The queue is referred to as First-In-First-Out list (FIFO).
Again insert another element 33 to the queue. The status of the queue is:
Again insert another element 44 to the queue. The status of the queue is:
Again insert another element 55 to the queue. The status of the queue is:
2
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Again insert another element 66 to the queue. The status of the queue is:
An element can be added to the queue only at the rear end of the
queue. Before adding an element in the queue, it is checked whether queue
is full. If the queue is full, then addition cannot take place. Otherwise, the
element is added to the end of the list at the rear side.
Now, delete an element 11. The element deleted is the element at the
front of the queue. So the status of the queue is:
Now, delete an element 22. The element deleted is the element at the front of
the queue. So the status of the queue is:
3
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Now, delete an element 33. The element deleted is the element at the front of
the queue. So the status of the queue is:
Now, delete an element 44. The element deleted is the element at the front of
the queue. So the status of the queue is:
Now, delete an element 55. The element deleted is the element at the front of
the queue. So the status of the queue is:
The dequeue operation deletes the element from the front of the
queue. Before deleting and element, it is checked if the queue is empty. If
not the element pointed by front is deleted from the queue and front is now
made to point to the next element in the queue.
5
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
{
int i;
if(front == rear)
{
printf("\n\n\t Queue is Empty");
return;
}
else
{
printf("\n Elements in Queue are:");
for(i = front; i < rear; i++)
{
printf("%d\t", Q[i]);
}
}
}
int menu()
{
int ch;
clrscr();
printf("\n \tQueue operations using ARRAY..");
printf("\n -----------**********-------------\n");
printf("\n 1. Insert ");
printf("\n 2. Delete ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice:");
scanf("%d", &ch);
return ch;
}
int main(void)
{
6
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
int ch;
do
{
ch = menu();
switch(ch)
{
case 1:
insertQ();
break;
case 2:
deleteQ();
break;
case 3:
displayQ();
break;
case 4:
return;
}while(ch!=4);
return 0;
}
7
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
We can perform the similar operations on two ends of the list using
two pointers front pointer and rear pointer.
8
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Dequeue operation
The dequeue operation deletes the first element from the front end of
the queue.
Initially it is checked, if the queue is empty.
If it is not empty, then return the value in the node pointed by front,
and moves the front pointer to the next node.
10
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
}
void deleteQ()
{
node *temp;
if(front == NULL)
{
printf("\n\n\t Empty Queue..");
return;
}
temp = front;
front = front -> next;
printf("\n\n\t Deleted element from queue is %d ", temp ->data);
free(temp);
}
void displayQ()
{
node *temp;
if(front == NULL)
{
printf("\n\n\t\t Empty Queue ");
}
else
{
temp = front;
printf("\n\n\n\t\t Elements in the Queue are: ");
while(temp != NULL )
{
printf("%5d ", temp -> data);
temp = temp -> next;
}
}
}
11
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
char menu()
{
char ch;
clrscr();
printf("\n \t..Queue operations using pointers.. ");
printf("\n\t -----------**********-------------\n");
printf("\n 1. Insert ");
printf("\n 2. Delete ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice: ");
ch = getche();
return ch;
}
int main(void)
{
char ch;
do
{
ch = menu();
switch(ch)
{
case '1' :
insertQ();
break;
case '2' :
deleteQ();
break;
case '3' :
displayQ();
break;
case '4':
12
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
return;
}
} while(ch != '4');
return 0;
}
APPLICATIONS OF QUEUES
Queues are widely used as waiting lists for a single shared resource
like printer, disk, CPU.
Queues are used to transfer data asynchronously (data not
necessarily received at same rate as sent) between two processes (IO
buffers), e.g., pipes, file IO, sockets.
Queues are used as buffers on MP3 players and portable CD players,
iPod playlist.
Queues are used in Playlist for jukebox to add songs to the end, play
from the front of the list.
Queues are used in operating system for handling interrupts. When
programming a real-time system that can be interrupted, for example,
by a mouse click, it is necessary to process the interrupts
immediately, before proceeding with the current job. If the interrupts
have to be handled in the order of arrival, then a FIFO queue is the
appropriate data structure.
When multiple users send print jobs to a printer, each printing job is
kept in the printing queue. Then the printer prints those jobs
according to first in first out (FIFO) basis.
Breadth first search uses a queue data structure to find an element
from a graph.
13
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
process continues for all vertices. The data structures used for keeping all
vertices and their adjacent vertices is Queue. We also use array for visited
vertices. The nodes that are visited are marked as 1. In BFS, traversing is
done in breadth wise fashion.
Algorithm
1. Create a graph, depending on the type of graph either it may be directed
or undirected set the value of flag to 1 or 0.
2. Read the vertex from which you want to traverse the graph say V i
3. Initialize the visited array to 1 at the index of V i
4. Insert the visited vertex Vi in the queue
5. Visit the vertex which is at the front of the queue, delete it from the queue
and place the adjacent vertices in the queue.
6. Repeat the step5 till queue is not empty.
14
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Step 2: Start with the vertex from visiting S (starting node), and mark it as
visited.
15
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
16
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
SCHEDULING
“Scheduling is the process of arranging, controlling and optimizing
work and workloads in a production process or manufacturing process”.
It is sometimes called as CPU scheduling. The process of allocating
the amount of CPU time for the process to execute or run for its completion
is called CPU Scheduling. There are two types of scheduling.
Preemptive Scheduling - Preemptive scheduling is used when a process
switches from running state to ready state or from waiting state to ready
state. The resources (mainly CPU cycles) are allocated to the process for the
limited amount of time and then is taken away, and the process is again
placed back in the ready queue if that process still has CPU burst time
remaining. That process stays in ready queue till it gets next chance to
execute.
Non-Preemptive Scheduling - Non-preemptive Scheduling is used when a
process terminates, or a process switches from running to waiting state. In
this scheduling, once the resources (CPU cycles) is allocated to a process,
the process holds the CPU till it gets terminated or it reaches a waiting state.
17
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Scheduling Criteria
There are several different criteria to consider when trying to select the
"best" scheduling algorithm for a particular situation and environment,
including:
CPU utilization - Ideally the CPU would be busy 100% of the time, so as to
waste 0 CPU cycles. On a real system CPU usage should range from 40%
(lightly loaded) to 90% (heavily loaded)
Throughput - Number of processes completed per unit time. May range
from 10 / second to 1 / hour depending on the specific processes.
Turnaround time - Time required for a particular process to complete, from
submission time to completion.
Waiting time - How much time processes spend in the ready queue waiting
their turn to get on the CPU.
Response time - The time taken in an interactive program from the
issuance of a command to the commence of a response to that command.
Advantages
It is simple and easy to understand.
18
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Disadvantages
It does not consider the priority or burst time of the processes.
It suffers from convoy effect i.e. processes with higher burst time
arrived before the processes with smaller burst time.
DEQUE
A deque (pronounced as ‘deck’ or ‘dequeue’) is a list in which the
elements can be inserted or deleted at both ends.
It is also known as a head-tail linked list because elements can be
added to or removed from both the front (head) or the back (tail) end.
No element can be added and deleted from the middle.
In the computer’s memory, a deque is implemented using either a
circular array or a circular doubly linked list.
19
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
In a deque, two pointers are maintained, front and rear, which point
to either end of the deque.
The elements in a deque extend from the front end to the rear end
and since it is circular, Dequeue[N–1] is followed by Dequeue[0].
There are two variants of a double-ended queue. They include
Input restricted deque:- In this dequeue, insertions can be done only
at one of the ends, while deletions can be done from both ends.
Output restricted deque:- In this dequeue, deletions can be done only
at one of the ends, while insertions can be done on both ends.
else
return false
}
isfull()
{
if((rear+1)%N == front)
return true
else
return false
}
enqueuerear(x)
{
if(isfull())
printf(“\n queue is full”);
else if(isempty())
front = rear = 0
else
rear = rear + 1 % N
a[rear]=x
}
dequefront()
{
if(isempty())
printf(“\n queue is empty”);
else if(front == rear)
front = rear = -1
else
front = front +1 % N
}
enqueuefront(x)
{
if(isfull())
21
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
APPLICATIONS OF DEQUUE
Applied as both stack and queue, as it supports both operations.
In O(1) time we can perform clock-wise and anti-clockwise rotational
operations.
For undo operation in sorting.
Storing a web browser’s history.
Storing a software application’s list of undo operations.
Job scheduling algorithm
22
Dr. Ratna Raju Mukiri M.Tech(CSE)., S.E.T., Ph.D.,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING