-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStack.hpp
87 lines (86 loc) · 1.31 KB
/
Stack.hpp
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
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
using namespace std;
template <class t>
class Stack
{
int tp;
t *arr;
int s;
public:
Stack()
{
tp=-1;
s = 5;
arr = new t[s];
}
Stack(int size)
{
tp=-1;
s = size;
arr = new t[s];
}
~Stack()
{
delete arr;
}
void push(t num)
{
//check if stack is full or not
if(tp == s-1)
{
cout << "STACK OVERFLOW" << endl;
}
else
{
tp++;
arr[tp] = num;
}
}
void pop()
{
//check if stack is empty or not
if(tp == -1)
{
cout << "STACK UNDERFLOW" << endl;
}
else
{
tp--;
}
}
t top()
{
//cout << arr[tp];
return arr[tp];
}
int size()
{
return tp+1;
}
void print()
{
for(int i=tp;i>=0;i--)
{
cout << arr[i] << endl;
}
}
bool empty()
{
if(tp==-1)
{
return true;
}
return false;
}
template <class T>
void copy(Stack<T> copy)
{
copy.tp = tp;
copy.s = s;
for(int i=tp;i>=0;i--)
{
copy.arr[i] = arr[i];
}
}
};