#include<iostream>
using namespace std;
#define Size 5
class Stack
{
private:
int Array[Size];
int top;
public:
Stack()
{
top = -1;
}
void Push(int x)
{
if (top == Size - 1)
{
cout << "Error, stack overFlow!" << endl;
return;
}
Array[++top] = x;
}
void Pop()
{
if (top == -1)
{
cout << "Error, stack is Empty!" << endl;
return;
}
top--;
}
int Top()
{
return Array[top];
}
bool IsEmpty()
{
if (top == -1)
return 1;
return 0;
}
void print()
{
cout << "Stack: ";
for (int i = 0; i <= top; i++)
{
cout << Array[i] << " ";
}
cout << "
";
}
};
int main()
{
Stack s;
s.Push(1);
s.Push(2);
s.print();
return 0;
}
#include<iostream>
#include<stack>
using namespace std;
bool isValid(string s) {
stack<char> st;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
st.push(s[i]);
if (!st.empty())
{
if (s[i] == ')')
{
if (st.top() == '(')
{
st.pop();
continue;
}
else
break;
}
//
if (s[i] == '}')
{
if (st.top() == '{')
{
st.pop();
continue;
}
else
break;
}
//
if (s[i] == ']')
{
if (st.top() == '[')
{
st.pop();
continue;
}
else
break;
}
}
else
return false;
}
return st.empty() ? true : false;
}
int main()
{
string s;
cin >> s;
cout << isValid(s) << "
";
return 0;
}
10 pushed into stack
20 pushed into stack
30 pushed into stack
30 Popped from stack
Top element is : 20
Elements present in stack : 20 10
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> stack;
stack.push(21);
stack.push(22);
stack.push(24);
stack.push(25);
stack.pop();
stack.pop();
while (!stack.empty()) {
cout << stack.top() <<" ";
stack.pop();
}
}