#include<stdio.h> #define SIZE 5 int queue[SIZE]= {}; int rear=0, front=0,i; void enqueue(int m) { if ((rear+1)%SIZE == front) { printf("queue is a full.\n"); return ; } else { rear=(rear+1)%SIZE;//rear++; queue[rear] = m; printf("%d QUEUE okay\n",m); view(); } } void dequeue() { if (front == rear) { printf("rear == front ! empty!"); } else { front = (front+1)%5; //front++; printf("%d dequeue okay.\n",queue[front]); queue[front]=0; view(); } } void view() { for (int i=0; i<5; i++) { printf("QUENE[%d] >> %d ",i,queue[i]); if(i==front) printf(" front"); if(i==rear) printf(" rear"); printf("\n"); } } int main() { int i,j,k,n,m,q=1; while(q) { printf("\n1.ENQUEUE 2.DEQUEUE 3.VIEW 4.EXIT : "); scanf("%d",&n); if (n == 1) { printf("DATA : "); scanf("%d",&m); enqueue(m); } if (n == 2) { dequeue(); } if (n == 3) { view(); } if (n == 4) { q=0; } } }
top of page
bottom of page