Queue using Array - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Queue using Array

Hello guys,

#include<stdio.h>
#define MAX 5
int front= -1,rear = -1, queue[MAX];
void disp()
{
 int i;
 if(front==-1&&rear==-1)
 {
  printf("\n Queue is empty!!!");
 }
 else
 {
  printf("\n Queue elements are : ");
  for(i=front;i<=rear;i++)
   printf("%d ",queue[i]);  
 }
}
void enqueue(int x)
{
 if(rear == MAX-1)
  printf("\nQueue overflow");
 else
 {
  if(front==-1&&rear==-1)
  {
   front = 0;
   rear = 0;
   queue[rear] = x;
   printf("\n %d successfully enqueued!!!",x);
   disp();
  }
  
  else
  {
   rear++;
   queue[rear] = x;
   printf("\n %d successfully enqueued!!!",x);
   disp();
  }
 }
}
void dequeue()
{
 //front
 int a;
 a=queue[front];
 if(front==-1&&rear==-1)
 {
  printf("\n Queue underflow!!!");
 }
 else if(front == rear)
 {
  front = -1;
  rear = -1;
 } 
 else
 {
  front++;
  printf("\n %d successfully dequeued",a);
  disp();
 }
}
void main()
{
 int ch,e;
 do
 {
  printf("\n===MENU===");
  printf("\n 1. ENQUEUE");
  printf("\n 2. DEQUEUE");
  printf("\n 3. DISPLAY");
  printf("\n 4. EXIT");
  printf("\n Enter your choice : ");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1 : if(rear == MAX-1)
      printf("\n Element cannot be inserted \n Queue overflow");
      else{
      printf("\n Enter the element : ");
      scanf("%d",&e);
      enqueue(e);}
      break;
   case 2 : dequeue();
      break;
   case 3 : disp();
      break;
   case 4 : printf("\n Thank you!!! \n ");
      break;
   default : printf("\n Invalid Input ");
      break;
  }
 
 }while(ch!=4);

}
/*
Output : 

===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 56

 56 successfully enqueued!!!
 Queue elements are : 56 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 27

 27 successfully enqueued!!!
 Queue elements are : 56 27 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 44

 44 successfully enqueued!!!
 Queue elements are : 56 27 44 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 48

 48 successfully enqueued!!!
 Queue elements are : 56 27 44 48 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 29

 29 successfully enqueued!!!
 Queue elements are : 56 27 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Element cannot be inserted 
 Queue overflow
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 56 successfully dequeued
 Queue elements are : 27 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 27 successfully dequeued
 Queue elements are : 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 44 successfully dequeued
 Queue elements are : 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 48 successfully dequeued
 Queue elements are : 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 Queue underflow!!!
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 3

 Queue is empty!!!
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 4

 Thank you!!! 
 lobo@lobo-Compaq-Presario-CQ60-Notebook-PC:~/Desktop$ 

*/

PropellerAds