Queue Data Structure algorithm by developerIndian

Updated: 13/01/2024 by Computer Hope

Go Back


A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
In this tutorial, you will learn what a Circular queue is. Also, you will find implementation of queue in C, C++, Java and Python.

#include<stdio.h>
#include<conio.h>
struct queue
 {   int no , pri ;
    struct queue *next;
 };
struct queue *front=NULL,*rear=NULL;

void insert(int val,int pr)
{  struct queue *p,*q;
  q=(struct queue *)malloc(sizeof(struct queue));
  q->no=val;
  q->pri=pr;
  if(front==NULL)
   {  q->next=NULL;
      front=rear=q;
      return;  }
  p=front;
  while((p->next->pri<=q->pri)&&(p->next!=NULL))
    {   p=p->next;
        if(p==NULL)
            break;  }
  if(q->pri<p->pri)
    { q->next=p;
      front=q;  }
  else
    { q->next=p->next;
       p->next=q; }
  }
int deletefront()
 {
int val;
   struct queue *q;
   if(front==NULL)
     { printf("\nQueue is Empty");
       return -999;
     }
   q=front;
   val=front->no;
   if(front==rear)
      front=rear=NULL;
   else
     front=front->next;
   free(q);
   return val;
}
void show()
{
  struct queue *q=front;
   if(front==NULL)
    { printf("\nQueue is Empty");
      return; }
  q=front;
  while(q!=NULL)
   { printf("%d\t",q->no);
     q=q->next;
   }
}

void main()
{
  int  ch,n,pr;
  do
  {
    clrscr();
    printf("\n1. Insert");
    printf("\n2. Delete from Front");
    printf("\n3. Show");
    printf("\n0. Exit");
    printf("\n\nEnter Your Choice");
    scanf("%d",&ch);
    switch(ch)
    {
      case 1:printf("\nEnter Number ");
	     scanf("%d",&n);
	     printf("\nEnter Priority");
	     scanf("%d",&pr);
	     insert(n,pr);
	     break;
      case 2:n=deletefront();
	     if(n!=-999)
	       printf("\n%d deleted",n);
	     break;
      case 3:show();
	     break;
      case 0:break;
      default:printf("\nWrong Choice");
    }
    if(ch!=0)
    {
      printf("\n\nPress any key to continue");
      getch();
    }
  }while(ch!=0);
}


        

Conclusion

We learn abstract data structure akin to stacks is called a queue. A queue is open at both ends, in contrast to stacks. Data is always inserted (enqueued) using one end and removed (dequeued) using the other. The data item that is saved first will be accessed first in a queue that uses the First-In-First-Out technique.