Wednesday, 12 February 2014

Circular Queue using array cprogram

Aim: To Implement Circular Queue using array in C program

program:

#include<stdio.h>

#include<conio.h>

#define maxsize 5

void insertion();

int deletion();

void display();

int rear=0,front=0;

int cqueue[maxsize];

void main()

{

int item,n,ch;

clrscr();

while(1)

{

printf("1.insertion\t 2.deletion 3.display\t 4.exit\n");

printf("enter your choice");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("enter the item to insert:");

scanf("%d",&item);

insertion(item);

break;

case 2:

item=deletion();

printf("item  deletion from the cqueue is %d\n",item);

break;

case 3:

display();

break;

default :

exit(0);

}

}

}

void insertion(int item)

{

if(front==0)

{

front=1;

rear=1;

cqueue[rear]=item;

}

else

{

rear=(rear%maxsize)+1;

if(rear==front)

printf("overflow");

else

cqueue[rear]=item;

}

}

int deletion(int item)

{

if(rear==0)

printf("under flow");

else

{

item=cqueue[front];

if(front==rear)

{

front=0;

rear=0;

}

else

front=(front%maxsize)+1;

return item;

}

}

void display()

{

int i;

printf("elements of cqueue are\n");

for(i=front;i<=rear;i++)

printf("%d ",cqueue[i]);

printf("\n");

}

output:


No comments:

Post a Comment