Circular queue in C (program)
========================
#include <stdio.h>
#include <conio.h>
#define size 10
int queue[size];
int front = 0;
int rear = 0;
int flag = 0;
int insert (int []);
int del (int []);
void display (int []);
void main ()
{
char ch;
int insert_element,del_element;
clrscr ();
do
{
fflush (stdin);
printf("\n\ni<-insert, d<-delete, q<-quit\n");
printf("\n\nEnter your choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
if (flag == 1)
printf("\nOVERFLOW\n");
else
{
insert_element = insert(queue);
printf("\n\nElement inserted is %d",insert_element);
printf("\n\nAfter the insertion the contents of the queue are:\n");
display (queue);
}
break;
}
case 'd':
{
del_element = del (queue);
if (flag == 1)
printf("\nUNDERFLOW\n");
else
{
printf("\n\nThe element deleted is %d",del_element);
printf("\n\nAfter the deletion the contents of the queue are:\n");
display (queue);
}
break;
}
case 'q':
exit (0);
}
}while (ch != 'q');
getch ();
}
int insert (int cq[])
{
int info;
printf("Enter the information you want to insert: ");
scanf("%d",&info);
if (rear == size)
rear = 1;
else
rear = rear + 1;
if (front == rear)
{
flag = 1;
return;
}
cq[rear] = info;
if (front == 0)
front = 1;
return (cq[rear]);
}
int del (int cq[])
{
int info;
if (front == 0)
{
flag = 1;
return;
}
info = cq[front];
if (front == rear)
{
front = rear = 0;
return (info);
}
if (front == size)
front = 1;
else
front = front + 1;
return (info);
}
void display (int cq[])
{
int i;
for (i = front ; i <= rear ; i++)
printf("%d ",cq[i]);
}
No comments:
Post a Comment