Do you think IIT Guwahati certified course can help you in your career?
No
Queue is a Data Structure that follows the first in first out (FIFO) method. There are different types of queues, like linear, circular, double-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(). enqueue() adds an item to the end (or rear) of the queue. dequeue()removes an item from the front of the queue.
Isn’t this interesting? If your answer was yes, then let’s not wait any longer and learn about enqueue and dequeue in C.
What is 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.
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.
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.
The enqueue process continues in a similar manner to add all the elements to the queue.
Now that we know what enqueue is, our next step is to learn about dequeue.
Let’s learn about it in the next section.
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.
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.
#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]);
}
}
}
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.