Insertion in a Queue in C

Insertion in a Queue in C

How to insert an element in a Queue in C programming?

Insertion in a Queue in C is also known as enqueuing so to enqueue an element certain steps are followed which will be discussed in this article. Inserting an element in a queue is one the simplest operation that is performed on a queue which is FIFO – First in First out structure, i.e. the element added first (Enqueued) will go out of the queue first (Dequeued).

Learn more about Queue in C programming by clicking the button below:-

Step for inserting an element in a Queue in C

To insert an element in a queue that is for enqueuing we proceed using following algorithm:-
  1. Define the maximum size of queue and initialize front and rear as -1.
  2. In the main function we will initialize two variables that will store the data and the size of the queue.
  3. Accept the data that we want to enter in a queue using a for loop.
  4. After accepting the data use enqueue() function to insert the data in a queue.
  5. In this function return queue id full , if the value of rear is equal to max-1.
  6. Else increase the value of rear by 1.
  7. After this insert the data that we have accepted earlier.
  8. Now display the data of the queue using a disp() function.
  9. In this function if the value of front is equal to -1 then it means list is empty.
  10. Else initialize a for loop from front to rear and print the data that queue is holding.
Insertion in a queue in C 1

Algorithm for enqueuing in C

As we know that Queue is a FIFO type data structure so we will be using following algorithm for insertion of an element in a Queue :-
  • IF (REAR==MAX-1)
  • RETURN QUEUE FULL
  • ELSE
  • REAR+REAR+1
  • QUEUE[REAR]=DATA
  • IF(FRONT== -1)
  • FRONT = 0

Code for inserting an element in a queue in C (Enqueuing)

Run
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 6

int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;

int peek ()
{
  return intArray[front];
}

bool isEmpty ()
{
  return itemCount == 0;
}

bool isFull ()
{
  return itemCount == MAX;
}

int size ()
{
  return itemCount;
}

void enqueue(int data)
{

  if (!isFull ())
    {

      if (rear == MAX - 1)
	{
	  rear = -1;
	}

      intArray[++rear] = data;
      itemCount++;
    }
}

int dequeue()
{
  int data = intArray[front++];

  if (front == MAX)
    {
      front = 0;
    }

  itemCount--;
  return data;
}

int main ()
{

  enqueue (3);
  enqueue (5);
  enqueue (9);
  enqueue (1);
  enqueue (12);
  enqueue (15);

  if (isFull ())
    {
      printf ("Queue is full!\n");
    }

  int num = dequeue ();

  printf ("Element removed: %d\n", num);

  enqueue (16);
  enqueue (17);
  enqueue (18);

  printf ("Element at front: %d\n", peek ());

  printf ("----------------------\n");
  printf ("index : 5 4 3 2  1  0\n");
  printf ("----------------------\n");
  printf ("Queue:  ");

  while (!isEmpty ())
    {
      int n = dequeue();
      printf ("%d ", n);
    }
}

Output

Queue is full!
Element removed: 3
Element at front: 5
----------------------
index : 5 4 3 2 1 0
----------------------
Queue: 5 9 1 12 15 16

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java