#include <stdio.h>
#include <stdlib.h>
int* queue;
int front = 0;
int rear = 0;
int MAX_SIZE;
int is_empty(){
if(front == rear){
return 1;
}
else{
return 0;
}
}
int is_full(){
if(front == (rear + 1) % MAX_SIZE){
return 1;
}
else{
return 0;
}
}
void enqueue(int e){
if(is_full()){
printf("overflow\n");
exit(0);
}
else{
rear = (rear+1)%MAX_SIZE;
queue[rear]=e;
}
}
int dequeue(){
if(is_empty()){
printf("underflow\n");
exit(0);
}
else{
front = (front + 1)%MAX_SIZE;
return queue[front];
}
}
int peek(){
if(is_empty()){
printf("underflow\n");
exit(0);
}
else{
return queue[(front+1)%MAX_SIZE];
}
}
int main(void){
int N;
scanf("%d", &N);
MAX_SIZE = N+1;
queue = (int*)malloc(sizeof(int) * MAX_SIZE);
int a;
while(1){
scanf("%d", &a);
if(a == -1){
printf("%d\n", dequeue());
}
else if(a == -2){
printf("%d\n", peek());
}
else if(a == 0){
free(queue);
break;
}
else if(a > 0){
enqueue(a);
}
}
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: