C examples for Data Structure:Queue
Implement a Circular Queue as an Array
#include <stdio.h> #define SIZE 8/*from ww w . j ava2 s . c o m*/ int circQue[SIZE]; int frontCell = 0; int rearCell = 0; int count = 0; void insertCircQue() { int num; if (count == SIZE) { printf("\nCircular Queue is Full. Enter Any Choice Except 1.\n "); } else { printf("\nEnter data, i.e, a number N (0 <= N : 30000): "); scanf("%d", &num); circQue[rearCell] = num; rearCell = (rearCell + 1) % SIZE; count++; printf("\nData Inserted in the Circular Queue. \n"); } } void deleteCircQue() { if (count == 0) { printf("\nCircular Queue is Exhausted!\n"); } else { printf("\nElement Deleted from Cir Queue is %d \n", circQue[frontCell]); frontCell = (frontCell + 1) % SIZE; count--; } } void displayCircQue() { int i, j; if (count == 0) { printf("\nCircular Queue is Exhausted!\n "); } else { printf("\nElements in Circular Queue are given below: \n"); j = count; for (i = frontCell; j != 0; j--) { printf("%d ", circQue[i]); i = (i + 1) % SIZE; } printf("\n"); } } int displayMenu() { int choice; printf("\nEnter 1 to Insert Data."); printf("\nEnter 2 to Delete Data."); printf("\nEnter 3 to Display Data."); printf("\nEnter 4 to Quit the Program. "); printf("\nEnter Your Choice: "); scanf("%d", &choice); return choice; } int main() { int choice; do { /* do-while loop begins. */ choice = displayMenu(); switch (choice) { /* switch statement begins. */ case 1: insertCircQue(); break; case 2: deleteCircQue(); break; case 3: displayCircQue(); break; case 4: return(0); default: printf("\nInvalid Choice. Please enter again. \n "); } /* switch statement ends. */ } while (1); /* do-while loop ends. */ }