/*
#include <stdio.h>
#define SIZE 201
int stack [SIZE]={};
int top=-1;
void push (int data)
{
stack [++top]=data;
}
int pop ()
{
if(top==-1) return 0;
return stack [top--];
}
void empty()
{
if(top==-1) printf("true\n");
else printf("false\n");
}
int main ()
{
int n,i,j,c=0,a=0;
char str[SIZE];
scanf ("%d ",&n);
for (i=0;i<n;i++)
{
gets(str);
if(str[1]=='u')
{
for (j=6;str[j]!=' ';j++)
{
a=a*10+str[j]-'0';
}
push (a);
a=0;
}
else if(str[0]=='t')
{
if(top==-1) printf("-1\n");
else printf ("%d\n",stack[top]);
}
else if (str[2]=='p'&&str[0]=='p')
{
pop ();
}
else if (str[0]=='s')
{
printf ("%d\n",top+1);
}
else if(str[0]=='e')
{
empty();
}
}
return 0;
}
*/
/*
#include <stdio.h>
#define SIZE 201
#include <string.h>
int stack[SIZE]={};
int top=-1;
void push (int data)
{
stack [++top]=data;
}
int pop
{
return stack[top--];
}
int main ()
{
char str[SIZE];
gets (str);
for (i=1;i<strlen(str);i+2)
{
if (str[i]=='*'||str[i]=='+'||str[i]=='-')
{
str[i]\\
}
#include <stdio.h>
#define SIZE 201
#include <string.h>
int stack[SIZE]={};
int top=-1;
void push (int data)
{
stack [++top]=data;
}
int pop
{
return stack[top--];
}
int main ()
{
char str[SIZE]={};
gets (str);
for (i=0;i<strlen(str);i++)
{
}
}
*/
/* basic queue
#include <stdio.h>
#define SIZE 5
int queue[SIZE]={};
int front=-1,rear=-1;
void enqueue (int data)
{
if (rear==SIZE-1) {
printf("queue is full!!\n");
return ;
}
queue [++rear]=data;
}
int dequeue()
{
int data;
if (front==rear)
{
printf("queue is empty!!\n");
return 0;
}
front++;
data=queue[front];
queue[front]=0;
printf("deq data is %d\n",data);
return data;
}
void view()
{
printf("-------------------------\n");
for(int i=front+1;i<=rear;i++)
{
printf("%d ",queue[i]);
}
printf("\n-----------------------\n");
}
*/
//circular queue
#include <stdio.h>
#define SIZE 5
int queue[SIZE]={};
int front=0,rear=0;
void enqueue (int data)
{
if((rear+1)%SIZE==front)
{
printf("queue is full!!\n");
return ;
}
rear=(rear+1)%SIZE;
queue [rear]=data;
}
int dequeue()
{
int data;
if (front==rear)
{
printf("queue is empty!!\n");
return 0;
}
front=(front+1)%SIZE;
data=queue[front];
queue[front]=0;
printf("deq data is %d\n",data);
return data;
}
void view()
{
printf("-------------------------\n");
for(int i=0;i<SIZE;i++)
{
printf("%d ",i);
}
printf("\n");
for(int i=0;i<SIZE;i++)
{
printf("%d ",queue[i]);
}
printf("\n-----------------------\n");
}
int main()
{
int n, c;
while(1)
{
printf("1.enq 2.deq 3.view >>");
scanf("%d",&n);
if(n==1)
{
printf("enq data is ");
scanf("%d",&c);
enqueue(c);
view();
}
else if(n==2)
{
dequeue();
view();
}
else
{
view();
}
}
}