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_18.cpp

assignment_18.cpp (2344B)


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