diff --git a/stack_array_implementation.cpp b/stack_array_implementation.cpp new file mode 100644 index 0000000..e078e58 --- /dev/null +++ b/stack_array_implementation.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +struct mystack +{ + int *arr; + int cap; + int top; + + mystack(int c) + { + cap = c; + arr = new int[cap]; + top = -1; + } + + void push(int x) + { + top++; + arr[top] = x; + } + + int pop() + { + int res = arr[top]; + top--; + return res; + } + + int peek() + { + return arr[top]; + } + + int size() + { + return (top + 1); + } + + // can use bool function like this only no need to put the false condition in the else part + bool isempty() + { + return (top == -1); + } +}; +int main(void) +{ + mystack s(5); + s.push(5); + s.push(10); + s.push(20); + cout << s.pop() << endl; + cout << s.size() << endl; + cout << s.peek() << endl; + cout << s.isempty() << endl; + return 0; +}