forked from spevnev/uprintf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalt_linked_list.c
57 lines (46 loc) · 1.15 KB
/
alt_linked_list.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
#include <stdlib.h>
#include "uprintf.h"
#define NODES 5
struct NodeB;
typedef struct NodeA {
int value;
struct NodeB *next;
} NodeA;
typedef struct NodeB {
float value;
struct NodeA *next;
} NodeB;
int main(void) {
NodeA head = {
.value = 0,
.next = NULL,
};
void *_cur = &head;
for (int i = 1; i < NODES; i++) {
if (i % 2 == 0) {
NodeB *cur = (NodeB *) _cur;
NodeA *node = (NodeA *) malloc(sizeof(*node));
if (node == NULL) return 1;
node->value = i * 2;
node->next = NULL;
cur->next = node;
_cur = node;
} else {
NodeA *cur = (NodeA *) _cur;
NodeB *node = (NodeB *) malloc(sizeof(*node));
if (node == NULL) return 1;
node->value = i * -1.23;
node->next = NULL;
cur->next = node;
_cur = node;
}
}
uprintf("Alternating linked list: %S\n", &head);
_cur = head.next;
while (_cur != NULL) {
NodeA *next = ((NodeB *) _cur)->next;
free(_cur);
_cur = next;
}
return _upf_test_status;
}