blob: ec1de31828c608dfe645c80bb3faddb213e64a7d (
plain)
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
|
#include <stdio.h>
#include "stack.h"
int main() {
int x, y, z;
while (1) {
scanf("%d", &x);
// 1. Kui x==0 siis print stack.
if (x == 0){
printf("\nPrindin stacki\n");
stackPrint();
break;
}
// 2. Kui empty Push.
else if (stackIsEmpty())
stackPush(x);
// 3. Kui x pos. ja peeked pos: push(x).
// 4. kui x pos. aga peeked neg., y=pop() ja push(x+y).
else if (x >= 0){
y = stackPeek();
if (y >= 0)
stackPush(x);
else{
z = stackPop();
if (x+z != 0)
stackPush(x+z);
}
}
// 5. Kui x on neg, siis pop()
else if (x < 0) {
z = stackPop();
if (x+z != 0) {
stackPush(x+z);
}
}
} // While loop End
return 0;
}
|