bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc064.c

luc064.c (3775B)


      1 /* Simulate a Dequeue (Double Ended Queue) using an array. Support: retrieve left, retrieve right, insert left, insert right. Use pointers left and right.
      2 */
      3 
      4 /* Let Us C, Chap- 13 (Arrays), Qn No.: B(j) */
      5 
      6 /* This file is auto-generated by a bot. */
      7 /* This code is not compiled; it is for reference only. */
      8 
      9 
     10 #include <stdio.h>
     11 #include <math.h>
     12 #include <stdlib.h>
     13 
     14 #define MAX 10
     15 
     16 void insert_left(int *, int *, int *, int);
     17 void insert_right(int *, int *, int *, int);
     18 void retrieve_left(int *, int *, int *);
     19 void retrieve_right(int *, int *, int *);
     20 void display(int *, int, int);
     21 
     22 int main()
     23 {
     24     int dq[MAX];
     25     int left = -1, right = -1;
     26     int choice, val;
     27 
     28     while (1)
     29     {
     30         printf("\n--- Dequeue Menu ---\n");
     31         printf("1. Insert Left\n2. Insert Right\n");
     32         printf("3. Retrieve Left\n4. Retrieve Right\n");
     33         printf("5. Display\n6. Exit\n");
     34         printf("Enter choice: ");
     35         scanf("%d", &choice);
     36 
     37         switch (choice)
     38         {
     39         case 1:
     40             printf("Enter value: ");
     41             scanf("%d", &val);
     42             insert_left(dq, &left, &right, val);
     43             break;
     44         case 2:
     45             printf("Enter value: ");
     46             scanf("%d", &val);
     47             insert_right(dq, &left, &right, val);
     48             break;
     49         case 3:
     50             retrieve_left(dq, &left, &right);
     51             break;
     52         case 4:
     53             retrieve_right(dq, &left, &right);
     54             break;
     55         case 5:
     56             display(dq, left, right);
     57             break;
     58         case 6:
     59             exit(0);
     60         default:
     61             printf("Invalid choice!\n");
     62         }
     63     }
     64     return 0;
     65 }
     66 
     67 void insert_left(int *dq, int *left, int *right, int val)
     68 {
     69     // Check if full
     70     if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
     71     {
     72         printf("Overflow! Dequeue is full.\n");
     73         return;
     74     }
     75 
     76     if (*left == -1) // Initially empty
     77     {
     78         *left = 0;
     79         *right = 0;
     80     }
     81     else if (*left == 0) // Wrap around
     82         *left = MAX - 1;
     83     else
     84         (*left)--;
     85 
     86     dq[*left] = val;
     87     printf("Inserted %d at Left.\n", val);
     88 }
     89 
     90 void insert_right(int *dq, int *left, int *right, int val)
     91 {
     92     if ((*left == 0 && *right == MAX - 1) || (*left == *right + 1))
     93     {
     94         printf("Overflow! Dequeue is full.\n");
     95         return;
     96     }
     97 
     98     if (*left == -1) // Initially empty
     99     {
    100         *left = 0;
    101         *right = 0;
    102     }
    103     else if (*right == MAX - 1) // Wrap around
    104         *right = 0;
    105     else
    106         (*right)++;
    107 
    108     dq[*right] = val;
    109     printf("Inserted %d at Right.\n", val);
    110 }
    111 
    112 void retrieve_left(int *dq, int *left, int *right)
    113 {
    114     if (*left == -1)
    115     {
    116         printf("Underflow! Dequeue is empty.\n");
    117         return;
    118     }
    119 
    120     printf("Retrieved from Left: %d\n", dq[*left]);
    121 
    122     if (*left == *right) // Only one element was present
    123     {
    124         *left = -1;
    125         *right = -1;
    126     }
    127     else if (*left == MAX - 1)
    128         *left = 0;
    129     else
    130         (*left)++;
    131 }
    132 
    133 void retrieve_right(int *dq, int *left, int *right)
    134 {
    135     if (*left == -1)
    136     {
    137         printf("Underflow! Dequeue is empty.\n");
    138         return;
    139     }
    140 
    141     printf("Retrieved from Right: %d\n", dq[*right]);
    142 
    143     if (*left == *right) // Only one element was present
    144     {
    145         *left = -1;
    146         *right = -1;
    147     }
    148     else if (*right == 0)
    149         *right = MAX - 1;
    150     else
    151         (*right)--;
    152 }
    153 
    154 void display(int *dq, int left, int right)
    155 {
    156     int i;
    157     if (left == -1)
    158     {
    159         printf("Dequeue is Empty\n");
    160         return;
    161     }
    162 
    163     printf("Elements: ");
    164     i = left;
    165     while (1)
    166     {
    167         printf("%d ", dq[i]);
    168         if (i == right)
    169             break;
    170         if (i == MAX - 1)
    171             i = 0;
    172         else
    173             i++;
    174     }
    175     printf("\n");
    176 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror