Skip to content

Instantly share code, notes, and snippets.

@jamesgeorge007
Last active September 30, 2018 13:49
Show Gist options
  • Save jamesgeorge007/bff695c0f1989f4edd2ffbaf3e7b8daf to your computer and use it in GitHub Desktop.
Save jamesgeorge007/bff695c0f1989f4edd2ffbaf3e7b8daf to your computer and use it in GitHub Desktop.
Insertion and deletion operations on a circular Queue.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 15
int Queue[SIZE], front = -1, rear = -1;
void display()
{
int i;
if(front == -1)
{
printf("\nEmpty!");
exit(1);
}
else if(front <= rear)
{
printf("\nState of the Queue is: \n");
for(i = front; i <= rear; i++)
{
printf("%d\t", Queue[i]);
}
}
else
{
printf("\nState of the Queue is: \n");
for(i = front; i <= SIZE-1; i++)
{
printf("%d\t", Queue[i]);
}
for(i = 0; i <= rear; i++)
{
printf("%d\t", Queue[i]);
}
}
printf("\n");
}
void insert(int el)
{
if(front == (rear + 1) % SIZE)
{
printf("\nOverflow!\n");
exit(1);
}
else
{
if(front == -1 && rear == -1)
{
front = 0;
}
rear = (rear + 1) % SIZE;
Queue[rear] = el;
}
display();
}
void delete()
{
int item = Queue[front];
if(front == -1)
{
printf("\nUnderflow!\n");
exit(1);
}
else
{
if(front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % SIZE;
}
}
printf("\nThe item removed was %d\n", item);
display();
}
void main()
{
char option, choice;
int el;
do{
printf("\n\n1.Insert\n2.Delete\n3.Display\nEnter your choice: ");
option = getchar();
switch(option)
{
case '1':
printf("\nEnter the element to be inserted: ");
scanf("%d", &el);
insert(el);
break;
case '2':
delete();
break;
case '3':
display();
break;
default:
printf("\nInvalid option!");
}
}while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment