DS Menu


Queues using array




Definition of Queue

A queue is a linear data structure that follows a particular order for adding and removing elements. This order is FIFO (First In, First Out), which means the first element added to the queue will be the first one to be removed. It's analogous to a line of people waiting for service where the first person in line is the first to be served and leave.

Key Operations of Queue

  • Enqueue: Adds an element to the end (rear) of the queue.
  • Dequeue: Removes an element from the front of the queue.
  • IsEmpty: Checks if the queue is empty.

Queue Implementation Using an Array

1: Initialize the Queue

  • Define the Queue: Use an array to store the elements of the queue. You also need two pointers (or indices): one for the front of the queue and one for the rear.
  • Specify Queue Capacity: Decide the maximum number of elements the queue can hold (the size of the array).
  • Initial Pointers: Set the front and rear pointers to -1 to indicate the queue is empty.
Pseudocode
maxSize := maxValue
queueArray := array of size maxSize
front := -1
rear := -1

2: Enqueue Operation

  • Check for Overflow: Before adding a new element, check if the queue is already full. If the rear pointer is at the last index, the queue is full.
  • Add Element: If the queue is empty, set both front and rear to 0. Otherwise, increment the rear pointer and insert the new element at the rear position in the array.
Pseudocode
// Add an item to the rear of the queue
function enqueue(element)
    if rear = maxSize-1 
        display "Queue Overflow"
    else:
        rear := rear + 1
        queueArray[rear] := element
        if front = -1
            front := 0

3: Dequeue Operation

  • Check for Underflow: Before removing an element, check if the queue is empty. If the front pointer is -1, the queue is empty.
  • Remove Element: Remove the element at the front of the queue. If the queue becomes empty after this operation, reset front and rear to -1. Otherwise, increment the front pointer.
Pseudocode
// Remove an item from the front of the queue
function dequeue():
    if front = -1
        display "Queue Underflow"
        return null
    else:
        item := queueArray[front]
        front := front + 1
        if front > rear  // Queue is now empty
		    front := -1
            rear := -1
        return item

C Program to implement Queue uing array

Program:

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 10

int queue[MAX_SIZE],front=-1, rear=-1;

// Function to add an element to the queue
void enqueue(int element) 
{
    if (rear == MAX_SIZE - 1) 
	{
        printf("Queue Overflow\n");
    } 
	else 
	{
		if(front == -1) 
			front = 0;
		queue[++rear] = element;
    }
}

// Function to remove an element from the queue
int dequeue() 
{
	int item;
    if (front == -1) 
	{
		printf("Queue Underflow\n");
        return -1; // Indicating underflow
    } 
	else 
	{
        int item = queue[front++];
        if (front > rear)   // Queue is now empty
		{ 
            front = -1;
            rear = -1;
        } 
        return item;
    }
}

// Function to display all the items from Queue
void display()
{
    int i;
    if (front == -1) 
	{
		printf("Queue is Empty\n");
    } 
    else
    {
        for(i=front;i<=rear;i++)
            printf("%d\t",queue[i]);
    }
}

// Main function
int main() {
    int ch,data;
    do{
        printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
        printf("\nEnter your choice:   ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("enter data to insert:  ");
                    scanf("%d",&data);
                    enqueue(data);
                    break;
            case 2: printf("Deleted: %d\n", dequeue());
                    break;
            case 3: display();
                    break;
            case 4: break;
            default: printf("Enter valid choice");
        }
    }while(ch!=4);
    return 0;
}


Next Topic :Queues using linkedlist