-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_link.c
More file actions
68 lines (54 loc) · 1.01 KB
/
reverse_link.c
File metadata and controls
68 lines (54 loc) · 1.01 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
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *next;
int data;
};
struct node * reverse_help(struct node *l, struct node *r)
{
if (r == NULL)
return l;
struct node *nl = r;
r = r->next;
nl->next = l;
return reverse_help(nl, r);
}
struct node * reverse(struct node *n)
{
return reverse_help(NULL, n);
}
struct node *createNode(int num)
{
struct node *p = malloc(sizeof(struct node));
p->next = NULL;
p->data = num;
return p;
}
void printfNode(struct node *l)
{
struct node *p = l;
while (p != NULL) {
printf("%d\n", p->data);
p = p->next;
}
}
int main(int argc, char const *argv[])
{
struct node *p = createNode(2);
struct node *q = createNode(3);
struct node *r = createNode(5);
struct node *s = createNode(7);
struct node *t = createNode(11);
struct node *o = createNode(13);
p->next = q;
q->next = r;
r->next = s;
s->next = t;
t->next = o;
printfNode(p);
printf("-----------------------------------\n");
struct node *rev = reverse(p);
printfNode(rev);
return 0;
}