-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstackAndQueue.cpp
More file actions
99 lines (73 loc) · 1.6 KB
/
stackAndQueue.cpp
File metadata and controls
99 lines (73 loc) · 1.6 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
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
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
void printStack(stack<int> myStack)
{
if (myStack.empty()) {
cout << "Error: stack kosong";
}
else {
while(!myStack.empty()) {
cout << "[>]" << myStack.top() << endl;
myStack.pop();
}
cout << endl;
}
}
void printQueue(queue<int> myQueue)
{
if (myQueue.empty()) {
cout << "Error: queue kosong";
}
else {
while(!myQueue.empty()) {
cout << "[>]" << myQueue.front() << endl;
myQueue.pop();
}
cout << endl;
}
}
int main1()
{
stack<int> myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
printStack(myStack);
cout << "Top element: " << myStack.top() << endl;
myStack.pop();
cout << endl;
printStack(myStack);
cout << "Top element after pop: " << myStack.top() << endl;
cout << "Stack size: " << myStack.size() << endl;
while(!myStack.empty()) {
myStack.pop();
}
printStack(myStack);
return 0;
}
int main2()
{
queue<int> myQueue;
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
printQueue(myQueue);
cout << "Front element: " << myQueue.front() << endl;
myQueue.pop();
cout << endl;
printQueue(myQueue);
cout << "Front element after pop: " << myQueue.front() << endl;
cout << "Queue size: " << myQueue.size() << endl;
while(!myQueue.empty()) {
myQueue.pop();
}
printQueue(myQueue);
return 0;
}
int main()
{
main1();
main2();
}