Some of the Primitive Operation in Queue are as given below:
- enqueue() – Insertion of elements to the queue.
- dequeue() – Removal of elements from the queue.
- peek() or front()- Acquires the data element available at the front node of the queue without deleting it.
- rear() – This operation returns the element at the rear end without removing it.
- isFull() – Validates if the queue is full.
- isEmpty() – Checks if the queue is empty.
- size(): This operation returns the size of the queue i.e. the total number of elements it contains.
Operation 1: enqueue()
Inserts an element at the end of the queue i.e. at the rear end.
The following steps should be taken to enqueue (insert) data into a queue:
- Check if the queue is full.
- If the queue is full, return overflow error and exit.
- If the queue is not full, increment the rear pointer to point to the next empty space.
- Add the data element to the queue location, where the rear is pointing.
- return success.
Below is the Implementation of the above approach:
void queueEnqueue(int data)
{
// Check queue is full or not
if (capacity == rear) {
System.out.println("\nQueue is full\n");
return;
}
// Insert element at the rear
else {
queue[rear] = data;
rear++;
}
return;
}
Operation 2: dequeue()
This operation removes and returns an element that is at the front end of the queue.
The following steps are taken to perform the dequeue operation:
- Check if the queue is empty.
- If the queue is empty, return the underflow error and exit.
- If the queue is not empty, access the data where the front is pointing.
- Increment the front pointer to point to the next available data element.
- The Return success.
Below is the Implementation of above approach:
void queueDequeue()
{
// If queue is empty
if (front == rear) {
System.out.println("\nQueue is empty\n");
return;
}
// Shift all the elements from index 2
// till rear to the left by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// decrement rear
rear--;
}
return;
}
Operation 3: front()
This operation returns the element at the front end without removing it.
The following steps are taken to perform the front operation:
- If the queue is empty return the most minimum value.
- otherwise, return the front value.
Below is the Implementation of the above approach:
// Function to get front of queue
int front(Queue queue)
{
if (isempty(queue))
return Integer.MIN_VALUE;
return queue.arr[queue.front];
}
Operation 4 : rear()
This operation returns the element at the rear end without removing it.
The following steps are taken to perform the rear operation:
- If the queue is empty return the most minimum value.
- otherwise, return the rear value.
Below is the Implementation of the above approach:
// Function to get rear of queue
int rear(Queue queue)
{
if (isEmpty(queue))
return Integer.MIN_VALUE;
return queue.arr[queue.rear];
}
Operation 5: isEmpty():
This operation returns a boolean value that indicates whether the queue is empty or not.
The following steps are taken to perform the Empty operation:
- check if front value is equal to -1 or not, if yes then return true means queue is empty.
- Otherwise return false, means queue is not empty
Below is the implementation of the above approach:
// This function will check whether
// the queue is empty or not:
boolean isEmpty()
{
if (front == -1)
return true;
else
return false;
}
Operation 6 : isFull()
This operation returns a boolean value that indicates whether the queue is full or not.
The following steps are taken to perform the isFull() operation:
- Check if front value is equal to zero and rear is equal to the capacity of queue if yes then return true.
- otherwise return false
Below is the Implementation of the above approach:
// This function will check
// whether the queue is full or not.
bool isFull()
{
if (front == 0 && rear == MAX_SIZE - 1) {
return true;
}
return false;
}
Operation 7: size()
This operation returns the size of the queue i.e. the total number of elements it contains.
// Java program to illustrate implementation of size()
// function
import java.util.*;
public class Main {
public static void main(String[] args)
{
int sum = 0;
Queue<Integer> myqueue = new LinkedList<>();
myqueue.add(1);
myqueue.add(8);
myqueue.add(3);
myqueue.add(6);
myqueue.add(2);
// Queue becomes 1, 8, 3, 6, 2
System.out.println(myqueue.size());
}
}