blob: 8d227f911d98be02b3f9fad1c582e3955d5152df (
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
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
|
#include <stdio.h>
#include "stack.h"
struct stack_st stack = { .len = 0, //prageused el
.size = 0, // max el
.arr = NULL};
// Functions
void stackPush(int val){
if (stack.len == 0 || stack.arr == NULL){
// Peame küsima mälu
stack.size = 1;
stack.arr = malloc(stack.size * sizeof(int));
if (stack.arr == NULL) {
printf("Mälu otsas\n");
return;
}
stack.arr[stack.len++] = val;
}
else if (stack.len == stack.size){
int tmp_size = malloc(2 stack.size);
int* tmp_arr = realloc(stack.val, tmp_size * sizeof(int));
if (stack.arr == NULL) {
printf("Mälu otsas\n");
return;
}
stack.size = tmp_size;
stack.arr = tmp_arr;
stack.arr[stack.len] = val;
stack.len++;
return;
}
}
int stackPop(void){
// Mälu vähendamine
/*
return stack.arr[--stack.len];
*/
// Peaks olema kogu värk.
int x = stack.val[--stack.len]
int tmp_size = stack.size/2;
if (stack.len < tmp-size){
int* tmp_arr = realloc(stack.arr, tmp_size *sizeof(int));
if (!tmp_arr){
printf("No good, malu ots");
return 0;
}
stack.size = tmp_size;
stack.arr = tmp_arr;
if (stack.len == 0)
stack_free();
// End of new stuff //
if (stackIsEmpty()){
return 0;
}
int stackPopped = stack.arr[--stack.len];
return stackPopped;
}
int stackIsEmpty(void) {
return !(stack.len > 0);
}
int stackPeek(){
// To avoid funking up the stack.len
if (stackIsEmpty()) return 0;
else if (stack.len == 1) return stack.arr[1];
else{
int x = stackPop();
stackPush(x);
return x;
}
}
void stackPrint(){
while(1){
if(stackIsEmpty())
break;
int x = stackPop();
printf("%d\n", x);
}
return;
}
/*
stack_free(){
free(stack.val); // kutsuda kõige lõpus vist
stack.arr = NULL;
}
*/
|