sidrs

FDS (LV) Ass 7 - Double Ended Queue using Array

Sep 23rd, 2024 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. // TODO
  2. // LN: 116
  3.  
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. class DEqueue {
  9. private:
  10.     int size;
  11.     int *arr;
  12.     int front;
  13.     int rear;
  14. public:
  15.     DEqueue() {
  16.         arr = new int[1];
  17.         front = -1;
  18.         rear = -1;
  19.     }
  20.  
  21.     DEqueue(int size) {
  22.         this->size = size;
  23.         arr = new int[size];
  24.         front = -1;
  25.         rear = -1;
  26.     }
  27.  
  28.     void insertFront();
  29.     void insertRear();
  30.     void deleteFront();
  31.     void deleteRear();
  32.     void display();
  33.  
  34.     ~DEqueue() {
  35.         delete[] arr;
  36.     }
  37. };
  38.  
  39. void DEqueue::insertFront() {
  40.     int element;
  41.     cout << "\nEnter the element to be added: "; cin >> element;
  42.    
  43.     if ((front == 0 && rear == size - 1) || (front == rear + 1)) {
  44.         cout << "\nOVERFLOW! Cannot Insert." << endl;
  45.     }
  46.     else if (front == -1 && rear == -1) {
  47.         front = 0; rear = 0;
  48.         arr[front] = element;
  49.     }
  50.     else if (front == 0) {
  51.         front = size - 1;
  52.         arr[front] = element;
  53.     }
  54.     else {
  55.         front--;
  56.         arr[front] = element;
  57.     }
  58. }
  59.  
  60. void DEqueue::insertRear() {
  61.     int element;
  62.     cout << "\nEnter the element to be added: "; cin >> element;
  63.    
  64.     if ((front == 0 && rear == size - 1) || (front == rear + 1)) {
  65.         cout << "\nOVERFLOW! Cannot Insert." << endl;
  66.     }
  67.     else if (front == -1 && rear == -1) {
  68.         front = 0; rear = 0;
  69.         arr[rear] = element;
  70.     }
  71.     else if (rear == size - 1) {
  72.         rear = 0;
  73.         arr[rear] = element;
  74.     }
  75.     else {
  76.         rear++;
  77.         arr[rear] = element;
  78.     }
  79. }
  80.  
  81. void DEqueue::deleteFront() {
  82.     int element = arr[front];
  83.     cout << "\nElement: " << element << " has been removed from the Queue" << endl;
  84.     if (front == -1 and rear == -1) cout << "\nERROR! Queue is Empty." << endl;
  85.     else if (front == rear) {
  86.         front = -1; rear = -1;
  87.     }
  88.     else if (front == size - 1) {
  89.         front = 0;
  90.     }
  91.     else {
  92.         front++;
  93.     }
  94. }
  95.  
  96. void DEqueue::deleteRear() {
  97.     int element = arr[rear];
  98.     cout << "\nElement: " << element << " has been removed from the Queue" << endl;
  99.     if (front == -1 and rear == -1) cout << "\nERROR! Queue is Empty." << endl;
  100.     else if (front == rear) {
  101.         front = -1; rear = -1;
  102.     }
  103.     else if (rear == 0) {
  104.         rear = size - 1;   
  105.     }
  106.     else {
  107.         rear--;
  108.     }
  109.  
  110. }
  111.  
  112. void DEqueue::display() {
  113.     cout << endl;
  114.     cout << "Displaying Queue Contents:" << endl;
  115.    
  116.     // This might produce a Bug when front = 0; value at 0th index will get printed twice
  117.     if (front > rear) {
  118.         for (int i = front; i < size; i++) {
  119.             cout << arr[i] << " ";
  120.         }
  121.         for (int i = 0; i <= rear; i++) {
  122.             cout << arr[i] << " ";
  123.         }
  124.     }
  125.     else {
  126.         for (int i = front; i <= rear; i++) {
  127.             cout << arr[i] << " ";
  128.         }
  129.     }
  130.  
  131.     cout << endl;
  132. }
  133.  
  134.  
  135. int main() {
  136.  
  137.     int size;
  138.     cout << "Enter the size of the Queue: "; cin >> size;
  139.     DEqueue dq( size );
  140.  
  141.  
  142.     int choice;
  143.     while (true) {
  144.         cout << "\n---------- MENU ----------" << endl;
  145.         cout << "1. Insert at the Front" << endl;
  146.         cout << "2. Insert at the Rear" << endl;
  147.         cout << "3. Delete at the Front" << endl;
  148.         cout << "4. Delete at the Back" << endl;
  149.         cout << "5. Display Queue Contents" << endl;
  150.         cout << "6. Exit" << endl;
  151.         cout << endl;
  152.         cout << "ENTER YOUR CHOICE: "; cin >> choice;
  153.  
  154.         if (choice == 1) { 
  155.             dq.insertFront();
  156.         }
  157.  
  158.         else if (choice == 2) {
  159.             dq.insertRear();
  160.         }
  161.  
  162.         else if (choice == 3) {
  163.             dq.deleteFront();
  164.         }
  165.  
  166.         else if (choice == 4) {
  167.             dq.deleteRear();
  168.            
  169.         }
  170.  
  171.         else if (choice == 5) {
  172.             dq.display();
  173.         }
  174.  
  175.         else if (choice == 6) {
  176.             cout << endl;
  177.             cout << "Terminating Program..." << endl;
  178.             cout << endl;
  179.             break;
  180.         }
  181.  
  182.         else {
  183.             cout << endl;
  184.             cerr << "INVALID CHOICE!" << endl;
  185.             cout << endl;
  186.         }
  187.    
  188.     }
  189.  
  190.     return 0;
  191. }
  192.  
  193.  
  194. /* OUTPUT
  195.  
  196. Enter the size of the Queue: 3
  197.  
  198. ---------- MENU ----------
  199. 1. Insert at the Front
  200. 2. Insert at the Rear
  201. 3. Delete at the Front
  202. 4. Delete at the Back
  203. 5. Display Queue Contents
  204. 6. Exit
  205.  
  206. ENTER YOUR CHOICE: 1
  207.  
  208. Enter the element to be added: 10
  209.  
  210. ---------- MENU ----------
  211. 1. Insert at the Front
  212. 2. Insert at the Rear
  213. 3. Delete at the Front
  214. 4. Delete at the Back
  215. 5. Display Queue Contents
  216. 6. Exit
  217.  
  218. ENTER YOUR CHOICE: 2
  219.  
  220. Enter the element to be added: 20
  221.  
  222. ---------- MENU ----------
  223. 1. Insert at the Front
  224. 2. Insert at the Rear
  225. 3. Delete at the Front
  226. 4. Delete at the Back
  227. 5. Display Queue Contents
  228. 6. Exit
  229.  
  230. ENTER YOUR CHOICE: 1
  231.  
  232. Enter the element to be added: 30
  233.  
  234. ---------- MENU ----------
  235. 1. Insert at the Front
  236. 2. Insert at the Rear
  237. 3. Delete at the Front
  238. 4. Delete at the Back
  239. 5. Display Queue Contents
  240. 6. Exit
  241.  
  242. ENTER YOUR CHOICE: 5
  243.  
  244. Displaying Queue Contents:
  245. 30 10 20
  246.  
  247. ---------- MENU ----------
  248. 1. Insert at the Front
  249. 2. Insert at the Rear
  250. 3. Delete at the Front
  251. 4. Delete at the Back
  252. 5. Display Queue Contents
  253. 6. Exit
  254.  
  255. ENTER YOUR CHOICE: 4
  256.  
  257. Element: 20 has been removed from the Queue
  258.  
  259. ---------- MENU ----------
  260. 1. Insert at the Front
  261. 2. Insert at the Rear
  262. 3. Delete at the Front
  263. 4. Delete at the Back
  264. 5. Display Queue Contents
  265. 6. Exit
  266.  
  267. ENTER YOUR CHOICE: 5
  268.  
  269. Displaying Queue Contents:
  270. 30 10
  271.  
  272. ---------- MENU ----------
  273. 1. Insert at the Front
  274. 2. Insert at the Rear
  275. 3. Delete at the Front
  276. 4. Delete at the Back
  277. 5. Display Queue Contents
  278. 6. Exit
  279.  
  280. ENTER YOUR CHOICE: 3
  281.  
  282. Element: 30 has been removed from the Queue
  283.  
  284. ---------- MENU ----------
  285. 1. Insert at the Front
  286. 2. Insert at the Rear
  287. 3. Delete at the Front
  288. 4. Delete at the Back
  289. 5. Display Queue Contents
  290. 6. Exit
  291.  
  292. ENTER YOUR CHOICE: 5
  293.  
  294. Displaying Queue Contents:
  295. 10
  296.  
  297. ---------- MENU ----------
  298. 1. Insert at the Front
  299. 2. Insert at the Rear
  300. 3. Delete at the Front
  301. 4. Delete at the Back
  302. 5. Display Queue Contents
  303. 6. Exit
  304.  
  305. ENTER YOUR CHOICE: 6
  306.  
  307. Terminating Program...
  308.  
  309.  
  310. */
  311.  
Advertisement
Add Comment
Please, Sign In to add comment