Queue Is An Abstract Data Structure
Queue Is An Abstract Data Structure
Unlike
stacks, a queue is open at both its ends. One end is always used to insert
data (enqueue) and the other is used to remove data (dequeue). Queue
follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.
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
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it,
and then completely erasing it from the memory. Here we shall try to
understand the basic operations associated with queues
peek() Gets the element at the front of the queue without removing it.
In queue, we always dequeue (or access) data, pointed by front pointer and
while enqueing (or storing) data in the queue we take help of rear pointer.
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
end procedure
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function
Algorithm
If the value of front is less than MIN or 0, it tells that the queue is not yet
initialized, hence empty.
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.
The following steps should be taken to enqueue (insert) data into a queue
Step 3 If the queue is not full, increment rear pointer to point the next empty
space.
Step 4 Add data element to the queue location, where the rear is pointing.
procedure enqueue(data)
if queue is full
return overflow
endif
rear rear + 1
queue[rear] data
return true
end procedure
Example
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
Step 3 If the queue is not empty, access the data where front is pointing.
Step 4 Increment front pointer to point to the next available data element.
procedure dequeue
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;
int data = queue[front];
front = front + 1;
return data;
}