-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
125 lines (95 loc) · 1.82 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
using namespace std;
class Stack
{
private:
int MAXIMUM_NUMBER_OF_ITEMS{10};
int currentIndex = -1;
int mainStack[10]{};
public:
Stack() = default;
void push(int elementToBePushed);
int pop();
bool isEmpty();
bool isFull();
int peek();
int count();
void change(int element, int index);
void display();
};
bool Stack::isFull()
{
return(currentIndex == MAXIMUM_NUMBER_OF_ITEMS - 1);
}
bool Stack::isEmpty()
{
return(currentIndex < 0);
}
int Stack::peek()
{
return mainStack[currentIndex];
}
int Stack::count()
{
return(currentIndex + 1);
}
void Stack::display()
{
if(isEmpty())
{
cout << "Stack is empty\n";
return;
}
cout << "Bottom of Stack -> ";
for(int i = 0; i <= currentIndex; i++)
{
cout << mainStack[i] << " ";
}
cout << "End of Stack\n";
}
void Stack::push(int elementToBePushed)
{
if(Stack::isFull())
{
cout << "Stack is Full\n";
return;
}
++currentIndex;
mainStack[currentIndex] = elementToBePushed;
}
int Stack::pop()
{
if(isEmpty())
{
cout << "Stack is Empty\n";
}
int temp = mainStack[currentIndex];
--currentIndex;
return temp;
}
void Stack::change(int element, int index)
{
if((index >= 0) && (index < MAXIMUM_NUMBER_OF_ITEMS))
{
mainStack[index] = element;
return;
}
cout << "Please Input Valid Index\n";
return;
}
int main()
{
Stack S;
S.display();
S.push(1);
S.push(2);
S.push(3);
S.display();
S.pop();
S.display();
S.pop();
S.display();
S.pop();
S.display();
return 0;
}