-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_a.cpp
157 lines (141 loc) · 3.67 KB
/
problem_a.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "problem_a.hpp"
using namespace strukdat::stack;
template <typename T>
int getMax(Stack<T> Top) {
int result = 0;
pointer<T> dump = nullptr;
if (isEmpty(Top)){
result = 0;
}
else{
int max = 0;
while(!(isEmpty(Top))){
if (max < peek(Top)){
max = peek(Top);
}
pop(Top, dump);
delete dump;
}
result = max;
}
return result;
}
//Membuat fungsi-fungsi untuk mempermudah pengerjaan fungsi getEqualStack
template <typename T>
int sumElement(Stack<T> Top){
pointer<T> temp = Top;
int sum = 0;
while (temp != nullptr){
sum += temp->data;
temp = temp->next;
}
return sum;
}
template <typename T>
int getEqualStack(Stack<T> Top1, Stack<T> Top2, Stack<T> Top3)
{
int result = 0;
if (isEmpty(Top1) || isEmpty(Top2) || isEmpty(Top3)){
result = 0;
}
else{
pointer<T> delTop = nullptr;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int max = 0;
bool equal = 0;
while (equal != 1){
sum1 = sumElement(Top1);
sum2 = sumElement(Top2);
sum3 = sumElement(Top3);
if (sum1 > sum2){
if (sum1 > sum3){
max = 1;
}
else{
max = 3;
}
}
else{
if (sum2 > sum3){
max = 2;
}
else{
max = 3;
}
}
switch(max){
case 1:
pop(Top1, delTop);
delete delTop;
break;
case 2:
pop(Top2, delTop);
delete delTop;
break;
case 3:
pop(Top3, delTop);
delete delTop;
break;
}
if (sum1 == sum2 && sum2 == sum3){
equal = 1;
result = sum1;
}
}
}
return result;
}
bool isBracketBalanced(std::string expression)
{
pointer<char> newElement = nullptr;
Stack<char> stringElement = nullptr;
pointer<char> dump = nullptr;
createStack(stringElement);
int length = expression.length();
for (int i=length-1; i>=0; i--){
createElement(newElement, expression[i]);
stringElement = push(stringElement, newElement);
std::cout << i;
}
bool check = true;
if (isEmpty(stringElement)){
check = false;
}
else{
char bracketCheck, bracketCheck2;
pointer<char> last;
if (length % 2 == 1){
check = false;
}
else{
for (int i=0; i<(length / 2); i++){
bracketCheck = peek(stringElement);
switch(bracketCheck){
case '(':
bracketCheck2 = ')';
break;
case '[':
bracketCheck2 = ']';
break;
case '{':
bracketCheck2 = '}';
break;
}
last = lastNode(stringElement);
if (last->data != bracketCheck2){
check = false;
delete stringElement;
break;
}
else{
pop(stringElement, dump);
delete dump;
delete last;
}
}
}
}
return check;
}