Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Enqueue?
3.
What is Dequeue?
4.
Enqueue and Dequeue in C
4.1.
Algorithm
4.2.
Code
4.3.
Output
5.
Frequently Asked Questions
5.1.
What is a queue?
5.2.
What are the different types of queues?
5.3.
What is enqueue?
5.4.
What is dequeue?
5.5.
What is the difference between enqueue-dequeue and push-pop operations?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Enqueue and Dequeue in C

Introduction

Queue is a data structure about which probably all of us already know.

It is a data structure in which elements are added to the end and removed from the front. In other words, a queue is a Data Structure that follows the first in first out (FIFO) method. 

Apart from what a queue is, we also know that there are different types of queues, like linearcirculardouble-ended, and priority queues. But, one thing common to all of these queues is their different operations. 

Coming to the very basic operations, elements can be added and removed from a queue. These two operations are generally called pushing and popping operations, but did you know that this terminology is used only for stacks?

Probably not, right? So now the question arises, what are they called in case of a queue?

In queues, the pushing operation is called enqueue, while the popping operation is called dequeue

Isn’t this interesting? If your answer was yes, then let’s not wait any longer and learn about enqueue and dequeue in C.

Enqueue and Dequeue in C

What is Enqueue?

To begin our discussion on enqueue and dequeue in C, we must first understand the operations.
Let us first learn about enqueue.

Enqueue is the operation of adding elements to a queue. It is the same thing as what we popularly know as pushing elements. To understand the idea properly, let us consider an example.
A queue is a data structure that works like a line at, let’s say, McDonald’s. There, new customers join the end of the line. Similarly, in a queue, the elements are added to the end of the queue. 

5 people standing in a line in front of McDonald's

Let us now consider a queue of size, say, n=5. The elements are entered at the end, so we need a pointer pointing to the last element. This pointer is initialized to position -1. 

An empty queue of size n=5 with back=-1

Now, while adding elements to a queue, that is, during enqueue, the value of back is increased as a number is added. So the first element ‘1’ is added as follows. 

1 is being added to an empty queue of size n=5. Back=-1
1 has been added to a queue of size n=5 and back=0

The enqueue process continues in a similar manner to add all the elements to the queue.

2 is being added to an empty queue of size n=5. Back=0
2 has been added to a queue of size n=5 and back=1
3 is being added to an empty queue of size n=5. Back=1
3 has been added to a queue of size n=5 and back=2
4 is being added to an empty queue of size n=5. Back=2
4 has been added to a queue of size n=5 and back=3
5 is being added to an empty queue of size n=5. Back=3
5 has been added to a queue of size n=5 and back=4

Now that we know what enqueue is, our next step is to learn about dequeue. 

Let’s learn about it in the next section. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Dequeue?

From the name, can you guess what dequeue is?

If you’ve guessed deleting elements from a queue, you are right, but we must keep a crucial point in mind here.

A queue is a data structure that follows the FIFO principle. So, while removing elements from a queue, they are removed from the front. 

Considering the same example of the queue above, if we run the dequeue for the first time, the element at the top of the queue, here, 1, will be removed. Now, to keep track of which element is at the top, a variable, let’s say, “front” is. “Front” is incremented each time a variable is removed.

Thus, the dequeue operation in queues can be visualized as shown below.

1 is being removed from the queue of size n=5. Front=0
1 has been removed from a queue of size n=5 and front=1
2 is being removed from the queue of size n=5. Front=1
2 has been removed from a queue of size n=5 and front=2
3 is being removed from the queue of size n=5. Front=2
3 has been removed from a queue of size n=5 and front=3
4 is being removed from the queue of size n=5. Front=3
4 has been removed from a queue of size n=5 and front=4
5 is being removed from the queue of size n=5. Front=4
5 has been removed from a queue of size n=5 and front=5

Must Read Passing Arrays to Function in C

Enqueue and Dequeue in C

Now that we know what enqueue and dequeue in C are, we should try to put them into code. 

To do that, let’s first see the algorithm. 

Also read - Data Structure MCQ

Algorithm

Step 1: Start

Step 2: Declare an array and two global integer type variables (front and back). We must initialize the array with a size and the variables with 0.

Step 3: Declare three methods for the enqueue, dequeue and print operations.

Step 4: Create the main method and declare two integer variables.

Step 5: Create an infinite loop in which the user is given a choice to select enqueue, dequeue, print, and quit.

Step 6: Use a switch statement to run the user’s choice. 

Step 7: In case 1, take the number to be added to the queue as input. Call the enqueue method with that number as a parameter and check what the function returns. If it return one, print a success message, else print the queue is full.

Step 8: In case 2, call the dequeue method and check what it returns. If it returns any number other than 0, print the number. Otherwise, print a message that the queue is empty.

Step 9: In case 3, call the print method.

Step 10: In the default case, print an error message.

Step 11: In the enqueue method, check if the queue is full. If it is, return 0. Otherwise, add the data to the queue, increment the “back” variable and return 1.

Step 12: Check if the queue is empty in the dequeue method. If it is, return 0. Else, increment the “front” variable and return the data removed.

Step 13: In the print method, run a loop from “front” to “back” and print the elements in the queue array at each index position.

Step 14: End


Now that we know how to proceed with the logic let’s start coding! 

The solution has been given below for your reference, but do try it out yourself first.

Must Read  C Program to Reverse a Number

Code

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

// To define the queue size
#define n 5

// The queue is created and front and back are initialised
int queue[n];
int back  = 0;
int front = 0;
 
int enqueue(int data);
int dequeue();
void print();
 
int main()
{
    int ch, data;
    // A loop to run the program while the user wants
    while (1)
    {
        printf("1. Enqueue 2. Dequeue 3. Print 0. Quit\n");
        printf("Give your choice: ");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
                // The number added to the queue is taken as input
                printf("Enter number to enqueue: ");
                scanf("%d", &data);
                if (enqueue(data))
                    printf("Enqueue operation successful");
                else
                    printf("Queue is full");
                break;
 
            case 2:
                data = dequeue();
                if(data)
                    printf("Data => %d", data);
                else
                    printf("Queue is empty");
                break;
 
            case 3:
                print();
                break;
            
            case 0:
                exit(0);
        
            default:
                printf("Invalid choice");
        }
        printf("\n");
    }
}

int enqueue(int data)
{
    // Checks if queue is full
    if (back==n)
    {
        return 0;
    }
    queue[back] = data;
    back = back + 1;
    return 1;
}
 
int dequeue()
{
    // Checks if queue is empty
    if (front==back)
    {
        return 0;
    }
    else
    {
        int data = queue[front];
        front = front + 1;
        return data;
    }
}

void print()
{
    if(front!=back)
    {
        for(int i=front;i<back;i++)
        {
            printf("%d ",queue[i]);
        }
    }
}

Must Read Algorithm Types

Output

Output terminal showing the numbers from 1-5 are being added to a queue
Output terminal showing 2 dequeue operations printing 1 and 2, and then the queue with the remaining elements from 3-5

Read about Application of Queue in Data Structure here.

Must Read Decision Making in C

Frequently Asked Questions

What is a queue?

A queue is a data structure that follows the first in first out (FIFO) method. 

What are the different types of queues?

The different types of queues are linear, circular, double-ended, and priority queues.

What is enqueue?

Enqueue is the operation of adding elements to a queue.

What is dequeue?

Dequeue is the operation of removing elements from the front of a queue, following the FIFO principle.

What is the difference between enqueue-dequeue and push-pop operations?

Push-pop operations are used in the case of stacks, while enqueue-dequeue operations are used for queues. This is the difference between them.

Conclusion

In this article, we learned about enqueue and dequeue in C. We learned what these operations are, saw how they work, and even implemented them in C. With this, we completed a small but essential topic on queues in data structures

But, as said, enqueue and dequeue in C is a very small topic on queues, so we need to learn about the different types of queues like linearcirculardouble-ended, and priority queues, learn how to reverse a queueApplication of Priority Queue etc. Apart from queues, there are other data structures to learn about like, expression evaluation using stackreversing a stacksorting queues using stack, and reversing a number using stack

Also read, Difference Between Compiler and Interpreter and Assembler

To better understand other data structures and algorithms topics, you may check out Coding Ninjas' Coding Ninjas Studio.

Also, don’t miss the Interview guide for Product Based Companies and popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc., on Coding Ninjas Studio. Besides, some of the Guided Paths on topics such as Data Structures and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts are available only on Coding Ninjas Studio.

Happy Coding!

Previous article
Difference Between Queue and Deque in C++
Next article
Advantages of Circular Queue Over Linear Queue
Live masterclass