-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.cpp
53 lines (45 loc) · 839 Bytes
/
stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
class Stack {
int *stack;
int top;
int size;
public:
Stack(int size) {
stack = (int *)malloc(sizeof(int) * size);
top = 0;
this->size = size;
}
bool push(int val) {
if (top==size)
return false;
else {
stack[top] = val;
top++;
}
}
int pop() {
if(isempty())
return -1;
else {
top--;
return stack[top];
}
}
bool isempty() {
return (top==0);
}
};
int main() {
Stack s = Stack(4);
s.push(1);
cout<<s.isempty()<<endl;
s.push(2);
s.push(5);
s.push(44);
for(int i=0; i<5; i++)
cout<<s.pop()<<" "<<s.isempty()<<endl;
return 0;
}