-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
68 lines (61 loc) · 1.26 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Online C++ compiler to run C++ program online
#include <iostream>
#include <iostream>
template <typename T>
class Stac{
private:
int mysize;
T* mydata;
public:
Stac() {
mysize=0;
}
~Stac() {
delete[] mydata;
}
T pop(){
if(mysize==0){
std::cout << "Stac is empty" << std::endl;
}
else{
T val = mydata[mysize-1];
mydata[mysize-1] = 0;
mysize--;
return val;
}
return -1;
}
void push(T data) {
if(mysize==0){
mydata = new T[mysize++];
mydata[mysize-1] = data;
}else{
mysize++;
mydata[mysize-1] = data;
}
}
T top() {
if(mysize==0){
return -1;
}else{
return mydata[mysize-1];
}
}
bool empty() {
return (mysize==0);
}
size_t size() {
return mysize;
}
};
int main() {
// Write C++ code here
Stac<int> st;
st.push(1);
st.push(2);
st.push(3);
std::cout << st.top() <<std::endl;
std::cout << st.pop() << std::endl;
std::cout << st.top() << std::endl;
return 0;
}