Computer Science, asked by stasnimahmed696, 3 months ago

We have a stack. The stack is maintaining the ascending order always. Now, you would need to insert a new value into this stack using a pushStack(in value) function. Write the pushStack() function so that the function always maintains the ascending order in the stack. You can use one additional stack.

In C++

Answers

Answered by mjeystech
1

Answer:

C++ program to perform push stack data structure

Explanation:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MAX 1000
  4. class Stack {
  5. int top;
  6. public:
  7. int a[MAX]; // Maximum size of Stack
  8. Stack() { top = -1; }
  9. bool push(int x);
  10. int pop();
  11. int peek();
  12. bool isEmpty();
  13. };
  14. bool Stack::push(int x)
  15. {
  16. if (top >= (MAX - 1)) {
  17. cout << "Stack Overflow";
  18. return false;
  19. }
  20. else {
  21. a[++top] = x;
  22. cout << x << " pushed into stack\n";
  23. return true;
  24. }
  25. }
  26. int main()
  27. {
  28. class Stack s;
  29. s.push(10);
  30. s.push(20);
  31. s.push(30);
  32. return 0;
  33. }
Similar questions