Professional Documents
Culture Documents
Data Structures - 2
Data Structures - 2
Applications of Queue:
Queue is used when things don’t have to be processed immediatly, but
have to be processed in First InFirst Out order like Breadth First Search.
This property of Queue makes it also useful in following kind of
scenarios.
1) When a resource is shared among multiple consumers. Examples
include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily
received at same rate as sent) between two processes. Examples
include IO Buffers, pipes, file IO, etc.
Data Structure : Queue
Array implementation Of Queue
For implementing queue, we need to keep track of two indices, front and rear.
We delete an item at the front and insert an item from the rear. If we simply
increment front and rear indices, then there may be problems, the front may
reach the end of the array.
class Queue {
int front, rear;
int capacity;
int array[];
public Queue(int capacity)
{
this.capacity = capacity;
front = -1;
rear = 0;
array = new int[this.capacity];
}
Data Structure : Queue
void push (int item)
{
rea++;
if (rear == capacity)
{
System.out.println((“Overflow” );
}
else
{
array[rear] = item;
}
}
Data Structure : Queue
// Method to remove an item from queue.
int pop()
{
if (front > rear) {
System.out.println(“Overflow”);
front = -1; rear = 0;
return -9999;
}
else {
int item = array[front++];
return item;
}
}
Data Structure : De-Queue
A double-ended queue (dequeue or deque) is an abstract data
type that generalizes a queue, for which elements can be added
to or removed from either the front or rear. Deque differs from
the queue abstract data type or First-In-First-Out List (FIFO),
where elements can only be added to one end and removed
from the other. This general data class has some possible sub-
types: