DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Universal Implementation of BFS, DFS, Dijkstra, and A-Star Algorithms
  • Doubly Linked List in Data Structures and Algorithms
  • Linked List in Data Structures and Algorithms
  • Stack in Data Structures

Trending

  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • A Complete Guide to Modern AI Developer Tools
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Internal Developer Portals: Modern DevOps's Missing Piece
  1. DZone
  2. Data Engineering
  3. Data
  4. Queue in Data Structures and Algorithms

Queue in Data Structures and Algorithms

In this blog, we further discuss queue implementation, how it is related to data structures, and its real-time applications.

By 
Amrutha TESR user avatar
Amrutha TESR
·
Jun. 14, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

Queue, for example, is a sequence of people who are standing for buying a metro ticket or ordering food at a store. The first person entering the queue leaves first. Similarly, the last person entering the queue leaves at the last. These queues help in managing the flow of customers and lower the chances of rush while buying. 

In this blog, we further discuss queue implementation, how it is related to data structures, and its real-time applications.

What Is Queue in DS?

A Queue is a linear type data structure that follows the principle of FIFO ( First-in-First-out ). The elements are added to the queue from one side and deleted from the other side. The side where elements are added is called the rear, and where deleted is called the front. Queue


Firstly, the initial condition for the queue is front and rear should be equal to -1.

As discussed in the previous blog on the stack, let us understand the ADT of the queue now. 

1. enqueue(): We can add or insert elements from the rear side.
To implement this operation, first, we must check whether the queue is full or not.

The pseudo-code is shown below:

C
 
void enqueue()
{
    if(rear == MAXSIZE -1)       //initially rear = -1
        //Overflow!
    else{
        rear = rear + 1;
        //store element
        }
}

      

2. dequeue(): We can delete or remove elements from the front side.

To implement this operation, first, we must check, the queue is not empty.

The pseudo-code for dequeue is:

C
 
void dequeue()
{
    if(front == -1 || front > rear)
        //Underflow!
    else{
        //store element
        front = front + 1;
        }
}

      

3. peek(): We can access the value pointed by the front.

Implementation of queue using c program:

C
 
#include<stdio.h>
#define MAXSIZE 20
int queue[MAXSIZE];
int front =-1,rear =-1,val,i;
void enqueue();
void dequeue();
void display();
void main()
{
    int choice;
    while(choice != 4)
    {
        printf("Enter a choice\n");
        printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
        scanf("%d", &choice);
        switch(choice)
           {
               case 1:
                 enqueue();
                 break;
               case 2:
                 dequeue();
                 break;
               case 3:
                 display();
                 break;
               case 4:
                 printf("Exiting...\n");
                 break;
               default:
                 printf("Invalid\n");
        }
    }
}
void enqueue()
{
    printf("Enter val:");
    scanf("%d", &val);
    if(rear == MAXSIZE -1)
        printf("Overflow\n");
    else if(front == -1 && rear == -1)
       {
           front = 0;
           rear = 0;
           queue[rear] = val;
       }
    else
       {
           rear = rear + 1;
           queue[rear] = val;
       }
    }
}
void dequeue()
{
    if(front == -1 || front > rear)
            printf("Underflow\n");
    else if(front == rear)
        {
            val = queue[front];
            front = -1;
            rear = -1;
        }
    else
        {
            val = queue[front];
            front = front + 1;
        }
    }
}
void display()
{
    if(rear == -1)
        printf("queue is empty");
    else
    {
        printf("values in queue:\n");
        for(i=front;i<=rear;i++)
        {
            printf("%d", queue[i]);
        }
    }
}


When we run the program, we get the output:

Types of Queues

We can find different types of queues, and they are as followed:

  • Linear queues: These are linear queues that are discussed already in the blog.
  • Circular queues: In these, the last element is connected to the first element and forms a circular loop.
  • Double-ended queues: These are also called Deque, where insertion and deletion can take place on both sides, i.e., both rear and front.

Conclusion

In the end, queues are one of the important concepts in data structures. I hope you like this blog, do leave a comment and share it.

Also, visit my other blog on Stack in data structures and algorithms.

Thank you.

Abstract data type Data structure Implementation Algorithm Data (computing) Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Universal Implementation of BFS, DFS, Dijkstra, and A-Star Algorithms
  • Doubly Linked List in Data Structures and Algorithms
  • Linked List in Data Structures and Algorithms
  • Stack in Data Structures

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!