De-Que

#include <cstdio> #include <cstdlib> #define MAX 5 int Q[MAX]; int front = -1; int rear = -1; int Rfront = MAX; int Rrear = MAX; int count = 0; int isEmpty() { if (count == 0) return 1; else return 0; } int isFull() { if (count == MAX) return 1; else return 0; } void display() { int i, j; if (count) { for (i = 0; i <= rear; i++) printf("%d ", Q[i]); printf("\n"); for (i = MAX - 1; i >= Rrear; i--) printf("%d ", Q[i]); printf("\n\n"); } else printf("Queue is Empty\n\n"); } void enqueue(int x) { int choice; printf("1. Front-End\n"); printf("2. Back-End\n"); printf("Choice: "); scanf("%d", &choice); if (isFull()) printf("Cannot Enqueue.\nOverflow\n\n"); else { if (choice == 1) { Q[++rear] = x; if (front == -1) front++; } else if (choice == 2) { Q[--Rrear] = x; if (Rfront == MAX) Rfront--; } count++; } display(); } void dequeue() { int choice; printf("1. Front-End\n"); printf("2. Back-End\n"); printf("Choice: "); scanf("%d", &choice); if (choice == 1) { if (rear >= 0) { rear--; count--; } else { printf("Cannot Dequeue\n"); printf("Queue is Empty/Underflow\n\n"); } } else if (choice == 2) { if (Rrear < MAX) { Rrear++; count--; } else { printf("Cannot Dequeue\n"); printf("Queue is Empty/Underflow\n\n"); } } if (isEmpty()) { front = -1; Rfront = MAX; } display(); } int main() { int choice, value; while (1) { printf("1 En-queue\n"); printf("2 De-queue\n"); printf("3 Is-Empty\n"); printf("4 Is-Full\n"); printf("5 Exit\n"); printf("Choice: "); scanf("%d", &choice); switch (choice) { case 1: { printf("Enter Value to En-queue: "); scanf("%d", &value); enqueue(value); break; } case 2: { dequeue(); break; } case 3: { if (isEmpty()) printf("True\n\n"); else printf("False\n\n"); break; } case 4: { if (isFull()) printf("True\n\n"); else printf("False\n\n"); break; } case 5: { exit(0); break; } default: { printf("Invalid Choice\nTry Again\n\n"); } } } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.