Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions stack_array_implementation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
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;
}