Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, January 28, 2017

Circular Queue DS in C


#include<stdio.h>
#include<conio.h>
#define max 6
int arr[max],front=-1,rear=-1,count=0;
void insert()
{
// Check if it is not full then increment top and assign value in it.
if(!isfull())
{
int item;
printf("Enter Element\n");
scanf("%d",&item);
rear=(rear+1)%max; // round rear if it is having max size
arr[rear]=item;
count++;
}
else
printf("Full");
}
int isfull()
{
if(count==max)
return 1;
else
return 0;
}
int isempty()
{
if(count==0)
return 1;
else
return 0;
}
void pop()
{
if(!isempty())
{
front=(front+1)%max; //if front =rear then q is empty
printf(" No. Deleted :%d",arr[front]);
count--;
}
else
printf("Empty");
}
void peep()
{
if(!isempty())
{
int temp=front+1,temp2=count; // front+1 bcz front is -1 so need to incrment
while(temp2--) //work till temp2 comes to 0
{
printf("%d",arr[temp]);
temp=(temp+1)%max;  //Printing Value from front
}}
}
void main()
{
int ch;
while(1)
{
printf("\n1 For Insert \n 2 For delete \n 3 to traverse \n 4 : Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
exit(1);
}
}
getch();
}

0 Comments:

Post a Comment

Stats