Enqueue and Dequeue in Data Structure


Given N numbers, your task is to implement Enqueue and Dequeue operation of the Queue Data structure.

See original problem statement here

Understand Enqueue & Dequeue with Example:

Print the queue each time an element is inserted and deleted.
If input is 1,2,3,4,5

Enqueue
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Dequeue
2 3 4 5
3 4 5
4 5
5

How to Solve Enqueue & Dequeue:

  1. This is the basic implementation question of queues.
  2. It can be done using two queues(one to transfer the content of the original queue to get the front element everytime) or use an array and simple print the element at required index.
  3. You can do it either way.
  4. Refer to the C++ code for queues implementation and C for arrays.

Solutions for Enqueue & Dequeue in Data Structure:

#include 
int Queue[100];
int front=-1,rear=-1;
void enqueue(int item,int N)
{
  rear=(rear+1)%N;
  if(front==rear)
  {
    return;
  }
  else
  {
    Queue[rear]=item;
    if(front==-1)
    front=rear;
  }
}
void dequeue(int N)
{
  int temp=Queue[front];
  if(front==rear)
  front=rear=-1;
  front=(front+1)%N;
  return;
}
void printQueue()
{
  int i=front;
  for(i;i<=rear;i++)
  {
    printf("%d ",Queue[i]);
  }
}
  
  int main()
  {
    // write your code here
    int N;
    scanf("%d",&N);
    int item;
    for(int i=0;i



						 
#include 
using namespace std;
int main()
{
int n;cin>>n;
vectorv(n);
for(int i=0;i>v[i];
queueq;
for(int i=0;itemp;
  while(!q.empty())
  {
    cout< temp;
  while(!q.empty())
  {
    cout<

						 
import java.util.*;
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
    Scanner myObj = new Scanner(System.in);
    int n = myObj.nextInt();
    int a[] = new int[n];
    for(int i=0;i

						 

[forminator_quiz id="1843"]

So, in this blog, we have tried to Queue data structure. If you want to solve more questions on queue data structure, which are curated by our expert mentors at PrepBytes, you can follow this link Queue.

Leave a Reply

Your email address will not be published. Required fields are marked *