-
Notifications
You must be signed in to change notification settings - Fork 83
/
Stack.cpp
75 lines (57 loc) · 743 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#define SIZE 10000
using namespace std;
template <class type>
class stack
{
private:
int top;
type a[SIZE];
public:
type pop()
{
if(isEmpty()) {
cout << "Stack empty\n";
return -1;
}
return a[top];
top--;
}
type push(type x)
{
if(isFull()) {
cout << "Stack is full\n";
return -1;
}
else {
top++;
a[top] = x;
return a[top];
}
}
bool isFull()
{
return top == SIZE - 1;
}
bool isEmpty()
{
return top == -1;
}
type getTopElement()
{
if(isEmpty()) {
return -1;
}
else {
return a[top];
}
}
};
int main()
{
stack <int> s;
s.push(5);
s.push(55);
s.push(555);
cout << s.getTopElement()<< endl;
}