-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
65 lines (51 loc) · 1.33 KB
/
test.c
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
#include <stdio.h>
#include <stdlib.h>
typedef struct mut_array{
int val;
struct mut_array * next;
} mut_array;
mut_array * sentinel = malloc(sizeof(sentinel));
mut_array * new() {
mut_array * first = malloc(sizeof(mut_array));
first->val = 0;
first->next = sentinel; //should link to a sentinel
return first;
}
void add(mut_array * first, int val) {
mut_array * next;
while((next = first->next) != sentinel) {
printf("searching...\r\n");
first = next;
}
next = malloc(sizeof(mut_array));
next->val = val;
next->next = sentinel;
}
void printMutArray(mut_array * first) {
mut_array * next;
printf("%d ", first->val);
while((next = first->next) != sentinel) {
printf("%d ", next->val);
}
printf("\r\n");
}
void delete() {
// should loop (while..) and free(..) every cell
// is there a better way of freeing the resources?
}
int main() {
mut_array * myMutArray;
myMutArray = new();
//edit(<pointer to myMutArray>, <int position>, <int newValue>);
myMutArray->val = 1;
add(myMutArray, 2);
add(myMutArray, 3);
printMutArray(myMutArray);
//try to print second val (2):
mut_array * second = myMutArray->next;
printf("asd\r\n");
printf("%d %d\r\n", sentinel, second);
printf("second == sentinel? %d", second==sentinel);
printf("second: %d\r\n", second->val);
return 0;
}