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