-
Notifications
You must be signed in to change notification settings - Fork 6
/
stack.cpp
92 lines (84 loc) · 1.6 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;
class Stack
{
private:
int size;
int top;
int *arr = NULL;
public:
Stack(int n)
{
size = n;
arr = new int[n];
top = -1;
}
bool isEmpty()
{
if (top == -1)
return true;
return false;
}
bool isFull()
{
if (top == size - 1)
return true;
return false;
}
void PUSH(int n)
{
if (isFull())
{
cout << "Stack is fully filled." << endl;
}
else
{
*(arr + (++top)) = n;
}
}
int POP()
{
if (isEmpty())
{
cout << "The Stack is already empty." << endl;
}
else
{
return *(arr + (top--));
}
}
void traversal()
{
for (int i = 0; i < top + 1; i++)
{
cout << *(arr + i) << " ";
}
if (isEmpty())
{
cout << "The Stack is already empty->" << endl;
}
else
cout << endl;
}
};
int main()
{
Stack *stPtr = new Stack(100);
stPtr->traversal();
stPtr->PUSH(10);
stPtr->PUSH(75);
stPtr->PUSH(23);
stPtr->PUSH(95);
stPtr->traversal();
cout << stPtr->POP() << endl;
cout << stPtr->POP() << endl;
cout << stPtr->POP() << endl;
stPtr->traversal();
stPtr->PUSH(78);
stPtr->PUSH(63);
stPtr->PUSH(225);
stPtr->PUSH(89);
stPtr->traversal();
free(stPtr);
return 0;
}