-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.cpp
More file actions
56 lines (45 loc) · 1.08 KB
/
stack.cpp
File metadata and controls
56 lines (45 loc) · 1.08 KB
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
54
55
56
#include<bits/stdc++.h>
using namespace std;
class stk{
private:
int size = 20, topi = -1;
int *s = (int*)malloc(sizeof(int)*size);
void sizeDoubler(){
int *temp = (int*)malloc(sizeof(int)*size*2);
for(int i = 0; i < size; i++) temp[i] = s[i];
int *temp2 = s;
s = temp;
free(temp2);
size*=2;
}
public:
int pop(){
if(topi < 0){
cout << "Stack is Empty" << endl;
}
else{
return s[topi--];
}
}
void push(int d){
while(size <= topi) sizeDoubler();
s[++topi] = d;
}
bool isEmpty(){
if(topi <= -1) return 1;
return 0;
}
int top(){
return s[topi];
}
};
int main(){
stk *s1 = new stk();
s1->push(5);
s1->push(50);
s1->push(500);
s1->push(5000);
cout << s1->top() << endl;
for(int i = 0; i < 4; i++) cout << s1->pop() << " " ;
cout << endl << s1->isEmpty();
}