// Queue implementation in C #include <stdio.h> #include <stdlib.h> #define SIZE 5 void enqueue(); void dequeue(); void display(); int items[SIZE], front = -1, rear = -1; int main() { //deQueue is not possible on empty queue int x; printf("1 for enqueue\n"); printf("2 for dequeue\n"); printf("3 for display\n"); printf("0 for exit\n"); while(1) { printf("enter choise:"); scanf("%d",&x); switch(x) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 0: exit(1); printf("EXIT"); default: printf("plz enter valid number\n"); } } return 0; } void enqueue(){ int value; printf("enter value to enqueue:"); scanf("%d",&value); if (rear == SIZE - 1) printf("\nQueue is Full!!"); else { if (front == -1) front = 0; rear++; items[rear] = value; printf("\nInserted -> %d\n", value); } } void dequeue(){ if (front == -1) printf("\nQueue is Empty!!"); else { printf("\nDeleted : %d\n", items[front]); front++; if (front > rear) front = rear = -1; } } // Function to print the queue void display(){ if (rear == -1) printf("\nQueue is Empty!!!"); else { int i; printf("\nQueue elements are:\n"); for (i = front; i <= rear; i++) printf("%d ", items[i]); } printf("\n"); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter