forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.c
More file actions
111 lines (85 loc) · 1.47 KB
/
list.c
File metadata and controls
111 lines (85 loc) · 1.47 KB
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
// Copyright (c) 2014-2019 K Team. All Rights Reserved.
#include <stdlib.h>
#include <stdio.h>
struct listNode {
int value;
struct listNode *next;
};
struct listNode* list_reverse(struct listNode *x)
{
struct listNode *p;
p = NULL;
while(x != NULL) {
struct listNode *y;
y = x->next;
x->next = p;
p = x;
x = y;
}
return p;
}
struct listNode* list_append(struct listNode *x, struct listNode *y)
{
struct listNode *p;
if (x == NULL)
return y;
p = x;
while (p->next != NULL)
p = p->next;
p->next = y;
return x;
}
struct listNode* list_create(int n)
{
struct listNode *x;
x = NULL;
while (n) {
struct listNode *y;
y = x;
x = (struct listNode*) malloc(sizeof(struct listNode));
x->value = n;
x->next = y;
n = n - 1;
}
return x;
}
void list_print(struct listNode* x)
{
while(x != NULL) {
printf("%d ", x->value);
x = x->next;
}
printf("\n");
}
void list_free(struct listNode* x)
{
while(x != NULL) {
struct listNode *y;
y = x->next;
free(x);
x = y;
}
}
int main()
{
struct listNode *x;
struct listNode *y;
x = list_create(5);
printf("x: ");
list_print(x);
x = list_reverse(x);
printf("reverse(x): ");
list_print(x);
list_free(x);
x = list_create(3);
printf("x: ");
list_print(x);
y = list_create(3);
printf("y: ");
list_print(y);
x = list_append(x, y);
printf("append(x, y): ");
list_print(x);
list_free(x);
return 0;
}