bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_2 / assignments / assignment_19.cpp

assignment_19.cpp (2749B)


      1 /* WAP to reverse the order of the elements in the stack using additional Queue. */
      2 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 class Queue {
      7 private:
      8     int *arr;
      9     int front;
     10     int rear;
     11     int capacity;
     12 
     13 public:
     14     Queue(int size) {
     15         capacity = size;
     16         arr = new int[capacity];
     17         front = -1;
     18         rear = -1;
     19     }
     20 
     21     bool isEmpty() { return (front == -1 || front > rear); }
     22     bool isFull() { return (rear == capacity - 1); }
     23 
     24     void enqueue(int val) {
     25         if (isFull()) return;
     26         if (front == -1) front = 0;
     27         arr[++rear] = val;
     28     }
     29 
     30     int dequeue() {
     31         if (isEmpty()) return -1;
     32         int val = arr[front++];
     33         if (front > rear) { front = -1; rear = -1; }
     34         return val;
     35     }
     36 
     37     ~Queue() { delete[] arr; }
     38 };
     39 
     40 class Stack {
     41 private:
     42     int *arr;
     43     int top;
     44     int capacity;
     45 
     46 public:
     47     Stack(int size) {
     48         capacity = size;
     49         arr = new int[capacity];
     50         top = -1;
     51     }
     52 
     53     bool isFull() { return (top == capacity - 1); }
     54     bool isEmpty() { return (top == -1); }
     55 
     56     void push(int val) {
     57         if (isFull()) { cout << "Stack Overflow!\n"; return; }
     58         arr[++top] = val;
     59     }
     60 
     61     int pop() {
     62         if (isEmpty()) { cout << "Stack Underflow!\n"; return -1; }
     63         return arr[top--];
     64     }
     65 
     66     void display() {
     67         if (isEmpty()) { cout << "Stack is empty!\n"; return; }
     68         for (int i = top; i >= 0; i--) cout << arr[i] << " ";
     69         cout << endl;
     70     }
     71 
     72     void reverseUsingQueue() {
     73         if (isEmpty()) { cout << "Stack is empty! Nothing to reverse.\n"; return; }
     74 
     75         Queue q(capacity);
     76         while (!isEmpty()) q.enqueue(pop());
     77         while (!q.isEmpty()) push(q.dequeue());
     78     }
     79 
     80     ~Stack() { delete[] arr; }
     81 };
     82 
     83 int main() {
     84     int size, choice, val;
     85     cout << "Enter the capacity of the stack: ";
     86     cin >> size;
     87     if (size <= 0) { cout << "Invalid stack size!\n"; return 0; }
     88 
     89     Stack s(size);
     90 
     91     while (true) {
     92         cout << "1.push 2.pop 3.reverse 0.display -1.Exit\n";
     93         cout << "Enter your choice: ";
     94         cin >> choice;
     95 
     96         switch (choice) {
     97             case 1:
     98                 cout << "Enter value: ";
     99                 cin >> val;
    100                 s.push(val);
    101                 break;
    102             case 2: {
    103                 int v = s.pop();
    104                 if (v != -1) cout << "Popped element: " << v << endl;
    105                 break;
    106             }
    107             case 3:
    108                 s.reverseUsingQueue();
    109                 break;
    110             case 0:
    111                 s.display();
    112                 break;
    113             case -1:
    114                 cout << "Exiting...\n";
    115                 return 0;
    116             default:
    117                 cout << "Wrong choice.\n";
    118         }
    119     }
    120 }