Program to implement Circular Queue - TechnoHindi.Org

Latest

Saturday, August 8, 2020

Program to implement Circular Queue














#include <stdio.h> 
#include <conio.h> 
#include <stdlib.h> 
#define MAX 5 
void insertq (); 
void deleteq (); 
void display (); 
int queue[MAX], front = -1, rear = -1; 
void main() 
int ch; 
clrscr(); 
while(1) 
printf ("\n*****MENU*****\n"); 
printf ("1. Insert\n"); 
printf ("2. Deletion\n"); 
printf ("3. Display\n"); 
printf ("4. Exit\n"); 
printf ("Enter your choice: "); 
scanf("%d", &ch); 

switch (ch) 
case 1: insertq (); 
break; 

case 2: deleteq (); 
break; 

case 3: display (); 
break; 

case 4: exit (0); 
break; 

default: 
printf ("Wrong Choice.\n"); 

void insertq () 
int data; 

printf ("Enter the element: "); 
scanf("%d", &data); 

if (( front==0 && rear == MAX-1) || (front == rear+1 )) 

printf ("Queue is full."); 
return; 

else if (rear == -1) 
rear++; 
front++; 

else if ( rear == MAX-1 && front > 0 ) 
rear = 0; 

else 

rear++; 
queue[rear]=data; 

void deleteq () 
if (front == -1) 
printf ("Queue is empty.\n"); 
else if (front == rear) 
printf ("%d is deleted.\n", queue[front]); 
front = -1; 
rear = -1; 

else 
printf ("%d deleted\n", queue[front]); 
front ++; 

void display() 
int i; 

if (front>rear) 
for (i=front; i<MAX; i++) 
printf ("%d\t", queue[i]); 

for (i=0; i<=rear; i++) 
printf ("%d\t", queue[i]); 

else 
for (i=front; i<=rear; i++) 
printf ("%d\t", queue[i]); 
}  
  1. Output :-

No comments:

Post a Comment