-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstackADT.cpp
More file actions
48 lines (38 loc) · 987 Bytes
/
stackADT.cpp
File metadata and controls
48 lines (38 loc) · 987 Bytes
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
#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];
}
void traverser(){
for(int i = topi; i >= 0; i--) cout << s[i] << " "; cout << endl;
}
};