forked from Adel-Nasim/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack Based Array.txt
69 lines (67 loc) · 982 Bytes
/
Stack Based Array.txt
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
#include<iostream>
using namespace std;
const int MAX_SIZE = 3;
template<class t>
class stack {
int top;
t item[MAX_SIZE];
public:
stack() :top(-1) {}
bool isEmpty()
{
return top < 0;
}
void push(t Element)
{
if (top >= MAX_SIZE-1)
{
cout << "Stack full on push";
}
else
{
top++;
item[top] = Element;
}
}
void pop()
{
if (isEmpty())
cout << "stack empty on pop";
else
top--;
}
void pop(t&Element)
{
if (isEmpty())
cout << "stack empty on pop";
else {
Element = item[top];
top--;
}
}
void getTop(t&stackTop)
{
if (isEmpty())
cout << "stack empty on getTop";
else {
stackTop = item[top];
cout << stackTop << endl;
}
}
void print() {
cout << "[ ";
for (int i = top; i >= 0; i--)
{
cout << item[i] << " ";
}
cout << "]";
cout << endl;
}
};
int main() {
stack<int>s;
s.push(5);
s.push(15);
s.push(20);
s.print();
}