Queue in C Programming

queue

Queue Implementation

A Queue, generally implemented using structures. Generally, the implementation is FIFO i.e. First in First out. A good example would be people standing at a billing counter in a queue, the person who comes first, will be served first

Basic Information about Queues

Queue is FIFO type i.e. First in First out. In queues inserting an item is called as Enqueue and removing an item is called as Dequeue.

As given in the image the following about queues is true – 

Terminologies

  • Enqueue means adding one item to the queue and happens at the rear end of queue
  • Dequeue means removal of one item from the queue and happens at the front end of the queue
  • If the max size of queue is reached then the queue is said to be in Overflow condition
  • If the queue has no items then queue is said to be in Underflow condition
Queue in C Programming
How Queue in C Programming works
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 insert(int data) {

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

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

int removeData() {
   int data = intArray[front++];
	
   if(front == MAX) {
      front = 0;
   }
	
   itemCount--;
   return data;  
}

int main() {
   
   insert(3);
   insert(5);
   insert(9);
   insert(1);
   insert(12);
   insert(15);

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

   int num = removeData();
	
   printf("Element removed: %d\n",num);
   
   insert(16);
   insert(17);
   insert(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 = removeData();           
      printf("%d ",n);
   }   
}

Limitations with Queues in C

Queues in C Problem or limitations with Queue

How to solve the above issue ?

The above issue can be solved in the following ways –

  • Either we can use Circular queues
  • Or modify the code as given below –

The modification in the below code is purely because we are applying remainder operation to size.

That is, for most operations we are doing (value)%size

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