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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • One Query, Four GPUs: Tracing a Distributed Training Stall Across Nodes
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • The Hidden Cost of Overprivileged Tokens: Designing Messaging Platforms That Assume Compromise
  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
5.6K 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

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook