Question

Please write a C++ code for 2D matrix by using STACK push and pop function.

Please write a C++ code for 2D matrix by using STACK push and pop function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code:

#include<bits/stdc++.h>
  
using namespace std;
#define MAX_SIZE 1000
class Stack
{
int top;
public:
int a[MAX_SIZE];   
  
Stack() { top = -1; }
bool push(int x);
int pop();
};
  
bool Stack::push(int x)
{
if (top >= (MAX_SIZE-1)) {
cout << "Overflow";
return false;
}
else{
a[++top] = x;
cout<<x <<" added to stack ";
return true;
}
}
  
int Stack::pop() {
if (top < 0)
{
cout << "Underflow";
return 0;
}
else
{
int x = a[top--];
return x;
}
}
  
int main() {
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout<<s.pop() << " removed from stack ";
  
return 0;
}

Output

======

Execute l> Share main.cpp STDIN I.lı Result #includec bits/stdc++.h> $g++-o main .cpp main 10 added to stack 20 added to stac

Add a comment
Know the answer?
Add Answer to:
Please write a C++ code for 2D matrix by using STACK push and pop function.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT