Advertisement
skb50bd

De-Que

May 31st, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #define MAX 5
  4.  
  5. int Q[MAX];
  6. int front = -1;
  7. int rear = -1;
  8. int Rfront = MAX;
  9. int Rrear = MAX;
  10. int count = 0;
  11.  
  12. int isEmpty() {
  13.     if (count == 0)
  14.         return 1;
  15.     else
  16.         return 0;
  17. }
  18.  
  19. int isFull() {
  20.     if (count == MAX)
  21.         return 1;
  22.     else
  23.         return 0;
  24. }
  25.  
  26. void display() {
  27.     int i, j;
  28.     if (count) {
  29.         for (i = 0; i <= rear; i++)
  30.             printf("%d ", Q[i]);
  31.         printf("\n");
  32.         for (i = MAX - 1; i >= Rrear; i--)
  33.             printf("%d ", Q[i]);
  34.         printf("\n\n");
  35.     }
  36.     else printf("Queue is Empty\n\n");
  37. }
  38.  
  39.  
  40. void enqueue(int x) {
  41.     int choice;
  42.     printf("1. Front-End\n");
  43.     printf("2. Back-End\n");
  44.     printf("Choice: ");
  45.     scanf("%d", &choice);
  46.     if (isFull())
  47.         printf("Cannot Enqueue.\nOverflow\n\n");
  48.     else {
  49.         if (choice == 1) {
  50.             Q[++rear] = x;
  51.             if (front == -1) front++;
  52.         }
  53.         else if (choice == 2) {
  54.             Q[--Rrear] = x;
  55.             if (Rfront == MAX) Rfront--;
  56.         }
  57.         count++;
  58.     }
  59.     display();
  60. }
  61.  
  62. void dequeue() {
  63.     int choice;
  64.     printf("1. Front-End\n");
  65.     printf("2. Back-End\n");
  66.     printf("Choice: ");
  67.     scanf("%d", &choice);
  68.  
  69.     if (choice == 1) {
  70.         if (rear >= 0) {
  71.             rear--;
  72.             count--;
  73.         }
  74.         else {
  75.             printf("Cannot Dequeue\n");
  76.             printf("Queue is Empty/Underflow\n\n");
  77.         }
  78.     }
  79.     else if (choice == 2) {
  80.         if (Rrear < MAX) {
  81.             Rrear++;
  82.             count--;
  83.         }
  84.         else {
  85.             printf("Cannot Dequeue\n");
  86.             printf("Queue is Empty/Underflow\n\n");
  87.         }
  88.     }
  89.     if (isEmpty()) {
  90.         front = -1;
  91.         Rfront = MAX;
  92.     }
  93.     display();
  94. }
  95.  
  96.  
  97. int main() {
  98.     int choice, value;
  99.     while (1) {
  100.         printf("1 En-queue\n");
  101.         printf("2 De-queue\n");
  102.         printf("3 Is-Empty\n");
  103.         printf("4 Is-Full\n");
  104.         printf("5 Exit\n");
  105.  
  106.         printf("Choice: ");
  107.         scanf("%d", &choice);
  108.  
  109.         switch (choice) {
  110.             case 1: {
  111.                 printf("Enter Value to En-queue: ");
  112.                 scanf("%d", &value);
  113.                 enqueue(value);
  114.                 break;
  115.             }
  116.             case 2: {
  117.                 dequeue();
  118.                 break;
  119.             }
  120.             case 3: {
  121.                 if (isEmpty())
  122.                     printf("True\n\n");
  123.                 else
  124.                     printf("False\n\n");
  125.                 break;
  126.             }
  127.             case 4: {
  128.                 if (isFull())
  129.                     printf("True\n\n");
  130.                 else
  131.                     printf("False\n\n");
  132.                 break;
  133.             }
  134.             case 5: {
  135.                 exit(0);
  136.                 break;
  137.             }
  138.             default: {
  139.                 printf("Invalid Choice\nTry Again\n\n");
  140.             }
  141.         }
  142.     }
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement