-
Notifications
You must be signed in to change notification settings - Fork 4
/
lec10.c
61 lines (53 loc) · 953 Bytes
/
lec10.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
// recursive traversal of linked list
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *
insert(struct Node *head, int data)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
struct Node *temp1 = head;
while (temp1->next != NULL)
{
temp1 = temp1->next;
}
temp1->next = temp;
return head;
}
return head;
}
void print(struct Node *p)
{
if (p == NULL)
return;
printf("%d ", p->data); // print current value
print(p->next); // recursive call
}
void print_reverse(struct Node *p)
{
if (p == NULL)
return;
print_reverse(p->next);
printf("%d ", p->data);
}
int main(void)
{
struct Node *head = NULL;
head = insert(head, 2);
head = insert(head, 4);
head = insert(head, 6);
head = insert(head, 5);
print(head);
}