What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained

46 views 10 minutes read
A+A-
Reset

First of all we have to understand what is queue? Queue in Data Structure and Algorithm are collection of data items and can be denied as below.

A queue is an ordered collection of data items into which a data item can be inserted from one end (called rear) and deleted from other end (called the front).

Working Principle of Queue

A queue follows the “First In, First Out” (FIFO) principle, where elements are added at the rear and removed from the front.

Queue in Data Structure
Working Principle of Queue

Understanding Queues

At its core, a queue is a linear data structure that follows the First In, First Out (FIFO) principle. Think of it as a line of people waiting for service at a ticket counter or a queue of tasks waiting to be processed by a computer.

Elements are added to the rear (enqueue operation) and removed from the front (dequeue operation) of the queue. This simple yet powerful concept forms the basis for a wide range of applications across various domains.

Queue in Data Structure and Algorithm Example

Queue operations

Two main operations on Queue are enqueue() and dequeue()

Enqueue() operation

This operation inserts a data item at the rear of the queue. The following steps should be taken to enqueue (insert) data into a queue.

  • Step 1 − Check if the queue is full.
  • Step 2 − If the queue is full, produce overflow error and exit.
  • Step 3 − If the queue is not full, move the rear pointer to point the next empty space.
  • Step 4 − Add data element to the queue location, where the rear is pointing
What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained 2
Enqueue operation

Dequeue() operation

This operation deletes data item at the front of the queue. The following steps are taken to perform dequeue operation:

  • Step 1 − Check if the queue is empty.
  • Step 2 − If the queue is empty, produce underflow error and exit.
  • Step 3 − If the queue is not empty, access the data where front is pointing.
  • Step 4 − Move front pointer to point to the next available data element.
What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained 3
Dequeue operation

Implementing Queue in C using linear array

Defining Queue

We can define queue using structure in C.

struct queue
{
    int items[max];   //data are stored in array
    int front;
    int rear;
}
struct queue q;
q.front = -1 //initially queue is empty
q.rear = -1

Defining Enqueue operation

void enqueue(struct queue q, int data)
{ 
    if(q.rear >= max-1)
        printf(“queue is full”);
    else
    {
        q.rear++;
        q.items[q.rear] = data;
        if(front==-1) //for inserting first data
        front = 0;
    }
}

Defining Dequeue operation

int dequeue(struct queue q)
{
    if(q.front == -1)
        printf(“queue is empty”);
    else
    {
        data = q.items[q.front];
        s.front++;
        return(data);
    }
}

Displaying queue data

void show(struct queue q)
{
    if(q.rear == -1)
        printf(“queue is empty”);
    else
        for(int i = q.front; i<=q.rear;i++)
            printf(“%d ” ,q.items[i];
}

Example Program of Queue in Data Structure using C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
 struct Queue {
    int items[max];
    int front;
    int rear;
};
// Global queue variable
struct Queue q;
//function declaration
void enqueue();
void dequeue();
void display();
 main()
    { 
	    int choice;
	    char ch;
	    q.front=-1;
        q.rear=-1;
	    do
        {
            system("cls");        //clear the prevoius screen
            printf("This is menu driven program for stack:\n");
            printf("Press 1 to enqueue data \nPres 2 to dequeue data \nPress 3 to display data\nPress 4 to exit application");
            printf("\nEnter your choice: ");
            scanf("%d",&choice);
            switch(choice)
                {
                    case 1:enqueue();
                    break;
                    case 2:dequeue();
                    break;
                    case 3:display();
                    break;
                    case 4:printf("application is exited.");
                    exit(0);
                    break;
                    default:printf("\nInvalid choice!!!plaese enter your choice form menu");
                }
            printf("\nDo you want to continue or not(y/n)");
            fflush(stdin);
            scanf("%c",&ch);
    	}
	    while(ch=='y' || ch=='Y')  
    }
	 // Function to enqueue an element into the queue
void enqueue() 
{
    int value;
    printf("Enter value to enqueue: ");
    scanf("%d", &value);
    if (q.rear == max - 1) 
    {
        printf("Queue is full. Cannot enqueue.\n");
    } 
    else 
    {
        if (q.front == -1) {
            q.front = 0;
        }
        q.rear++;
        q.items[q.rear] = value;
        printf("%d enqueued to the queue.\n", value);
    }
}
// Function to dequeue an element from the queue
void dequeue() {
    int item;
    if (q.front == -1) 
    {
        printf("Queue is empty. Cannot dequeue.\n");
    }
    else 
    {
        item = q.items[q.front];
        if (q.front >= q.rear) {
            q.front = -1;
            q.rear = -1;
        } else {
            q.front++;
        }
        printf("%d dequeued from the queue.\n", item);
    }
}
// Function to display the elements of the queue
void display() 
{
	int i;
    if (q.front == -1) 
    {
        printf("Queue is empty.\n");
    } else 
    {
        printf("Queue elements are:\n");
        for ( i = q.front; i <= q.rear; i++) {
            printf("%d ", q.items[i]);
        }
        printf("\n");
    }
}

Output of the Above Program

We will interface like this to learn Queue in Data Structure:

What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained 4
Application Interface – Queue in Data Structure

Now we can enqueue data by pressing 1, we can dequeue data by pressing 2 and press 3 to display data. Screenshots below.

What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained 5
Enqueue Data – Option 1 – Queue in Data Structure
What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained 6
Dequeue Data – Options 2 – Queue in Data Structure
What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained 7
Display Data – Option 3 – Queue in Data Structure

Conclusion – Queue in Data Structure

Queues represent a cornerstone of data structures and algorithms, offering a simple yet powerful mechanism for managing data in a structured manner. From task scheduling in operating systems to packet transmission in networking, queues find applications across a wide spectrum of domains.

By understanding the principles and algorithms behind queues, developers can design efficient and scalable systems that meet the demands of modern computing. As technology continues to evolve, queues will remain an indispensable tool in the arsenal of every software engineer and computer scientist.

We hope this journey into the realm of queues has provided valuable insights and inspired further exploration. Embrace the power of queues in your coding endeavors, and unlock new possibilities in software development and problem-solving.

Thank you for joining us on this exploration of queues in data structures and algorithms. To learn more such articles in audio visual format, subscribe us on YouTube. Happy learning!

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.