-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdemo_array_stack_4.cpp
More file actions
57 lines (48 loc) · 1.12 KB
/
demo_array_stack_4.cpp
File metadata and controls
57 lines (48 loc) · 1.12 KB
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
#include <stack>
#include <iostream>
using namespace std;
stack<int> stack_push(stack<int> stack) {
for (int i = 0; i < 5; i++) {
stack.push(i);
}
return stack;
}
stack<int> stack_pop(stack<int> stack) {
cout << "Pop: ";
for (int i = 0; i < 5; i++) {
int y = (int)stack.top();
stack.pop();
cout << (y) << endl;
}
return stack;
}
void stack_peek(stack<int> stack) {
int element = (int)stack.top();
cout << "Element on stack top : " << element << endl;
}
void stack_search(stack<int> stack, int element) {
int pos = -1, co = 0;
while (stack.size() > 0) {
co++;
if (stack.top() == element) {
pos = co;
break;
}
stack.pop();
}
if (pos == -1) {
cout << "Element not found" << endl;
} else {
cout << "Element is found at position: " << pos << endl;
}
}
int main() {
stack<int> stack;
stack = stack_push(stack);
stack = stack_pop(stack);
stack = stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
return 0;
}