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
1
Answer:
C++ program to perform push stack data structure
Explanation:
- #include <bits/stdc++.h>
- using namespace std;
- #define MAX 1000
- class Stack {
- int top;
- public:
- int a[MAX]; // Maximum size of Stack
- Stack() { top = -1; }
- bool push(int x);
- int pop();
- int peek();
- bool isEmpty();
- };
- bool Stack::push(int x)
- {
- if (top >= (MAX - 1)) {
- cout << "Stack Overflow";
- return false;
- }
- else {
- a[++top] = x;
- cout << x << " pushed into stack\n";
- return true;
- }
- }
- int main()
- {
- class Stack s;
- s.push(10);
- s.push(20);
- s.push(30);
- return 0;
- }
Similar questions
Computer Science,
1 month ago
Math,
1 month ago
Math,
1 month ago
Social Sciences,
3 months ago
Math,
9 months ago
Math,
9 months ago
Physics,
9 months ago