DS - Queue Best
DS - Queue Best
Algorithms - Queue
Queue Representation
As we now understand that in queue, we
access both ends for different reasons. The
following diagram given below tries to explain
queue representation as data structure −
peek()
This function helps to see the data at the front
of the queue. The algorithm of peek() function
is as follows −
Algorithm
return queue[front]
end procedure
Example
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to
implement queue, we just check for the rear
pointer to reach at MAXSIZE to determine that
the queue is full. In case we maintain the queue
in a circular linked-list, the algorithm will differ.
Algorithm of isfull() function −
Algorithm
return true
else
return false
endif
end procedure
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
Algorithm
end procedure
Example
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Enqueue Operation
Queues maintain two data pointers, front and
rear. Therefore, its operations are
comparatively difficult to implement than that
of stacks.
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Example
int enqueue(int data)
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
Dequeue Operation
Accessing data from the queue is a process of
two tasks − access the data where front is
pointing and remove the data after access. The
following steps are taken to perform dequeue
operation −
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
Example
int dequeue() {
if(isempty())
return 0;
return data;
}
Print Page