Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Part - 2

Data Structure : Queue


Like Stack, Queue is a linear structure which follows a particular order
in which the operations are performed. The order is First In First Out
(FIFO). A good example of queue is any queue of consumers for a
resource where the consumer that came first is served first.
The difference between stacks and queues is in removing. In a stack
we remove the item the most recently added; in a queue, we remove
the item the least recently added.
Operations on Queue:
Mainly the following two basic operations are performed on queue:
Enqueue(push) : Adds an item to the queue. If the queue is full, then it
is said to be an Overflow condition.
Dequeue (pop) : Removes an item from the queue. The items are
popped in the same order in which they are pushed. If the queue is
empty, then it is said to be an Underflow condition.
Front: denotes the front index.
Rear: denotes the last index.
Data Structure : Queue

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:

1) An input-restricted deque is one where deletion can be made


from both ends, but insertion can be made at one end only.
2) An output-restricted deque is one where insertion can be
made at both ends, but deletion can be made from one end
only.
Data Structure : De-Queue
Input Restricted Deque

Output Restricted Deque


Data Structure : De-Queue
There are four basic operations in Deque
1. pushFront(),
2. pushRear()
3. popFront()
4. popRear()
As the naming specify these functions add or delete to the
corresponding sides.
Data Structure : De-Queue
class Deque
{
int arr[];
int front;
int rear;
int size;

public Deque(int size)


{
arr = new int[size];
front = -1;
rear = 0;
this.size = size;
}
Data Structure : De-Queue
// Inserts an element at front
void pushfront(int key)
{
// check whether Deque if full or not
if ((front < 0))
{
System.out.println("Overflow from front");
return;
}
else
{
arr[front] = key;
front--;
}
}
Data Structure : De-Queue
// Inserts an element at rear
void pushrear(int key)
{
rear++;
if (rear==size)
{
System.out.println(" Overflow from rear ");
return;
}
else
{
arr[rear] = key;
}
}
Data Structure : De-Queue
// delete an element from front
int popfront()
{
if(front == -1)
front++;
if (front > rear)
{
front = -1;
rear = 0;
System.out.println(“Underflow”);
return -9999;
}
else
{
return arr[++front];
}
}
Data Structure : De-Queue
// delete an element from rear
int poprear()
{
if (front == rear)
{
front = -1;
rear = 0;
System.out.println(“Underflow”);
return -9999;
}
else
{
return arr[rear- -];
}
}

You might also like