stack using array
#include <iostream>
using namespace std;
class Stack {
int arr[10];
int top;
public:
Stack() { top = -1; }
void push(int val) {
if (top == 9)
cout << "Stack Overflow!\n";
else {
arr[++top] = val;
cout << val << " pushed to stack.\n";
}
}
void pop() {
if (top == -1)
cout << "Stack Underflow!\n";
else
cout << arr[top--] << " popped from stack.\n";
}
void display() {
cout << "Stack: ";
for (int i = 0; i <= top; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.display();
s.pop();
s.display();
cout << "\nTime Complexity: O(1)\nSpace Complexity: O(n)\n";
return 0;
}
Editor is loading...
Leave a Comment