Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created October 3, 2016 19:00
Show Gist options
  • Save priyadarshitathagat/0de4531d05557ffa118147f126a4bbd8 to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/0de4531d05557ffa118147f126a4bbd8 to your computer and use it in GitHub Desktop.
Implementation of circular queue
#include<stdio.h>
#include<stdlib.h>
#define max 25
int front=0,rear=-1,c=0; //c to count the number of inputs in the queue
struct stack{
int a;
};
push(struct stack *st,int x,int n)
{
if(c==n)
{printf("overflow\n"); return;}
rear++; rear=rear%n;
if(c!=n)
c++;
(st+rear)->a=x;
}
int pop(struct stack *st,int n)
{
if(c==0)
{printf("underflow\n"); return -9999;}
c--; front=front%n;
int x=(st+front)->a;
printf("\nThe popped out element is: %d\n",x); front++;
}
display(struct stack *st,int n)
{if(c==0)
{printf("Stack-Empty\n"); return; }
int i=front,j=0;
if(front>rear)
for(i=front; i<n; i++)
{printf("%d",(st+i)->a); j++;}
else
for(i=front; i<=rear; i++)
{printf("%d",(st+i)->a); j++;}
if(j!=c)
{for(i=0; i<c-j; i++)
{printf("%d",(st+i)->a);}
}
printf("\n");
}
main()
{
struct stack *st;
int x,n,p,ch;
printf("size of circular_queue\n");
scanf("%d",&n);
st=(struct stack *)malloc(n*(sizeof(struct stack)));
while (1)
{printf(" enter 1 to push 2 to pop 3 to display 4 to break\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter element to add\n");
scanf("%d",&x);
push(st,x,n);
break;
case 2:{pop(st,n);break;}
case 3:display(st,n);break;
case 4:break;
}
printf(" to continue press 1 else 0\n");
scanf("%d",&x);
if(x==0)
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment