Implementation of Stack
Implementation of Stack
#include <iostream>
using namespace std;
class Stack {
private:
int top;
int arr[100];
public:
void push(int x)
{
arr[++top] = x;
cout << "Pushed " << x << " to stack\n";
}
int pop()
{
if (top < 0) {
cout << "Stack underflow" << endl;
return 0;
}
return arr[top--];
}
int peek()
{
if (top < 0) {
cout << "Stack is empty" << endl;
return 0;
}
return arr[top];
}
bool isEmpty()
{
return (top < 0);
}
};
int main()
{
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element is: " << s.peek() << endl;
while (!s.isEmpty()) {