-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab6_2.c
71 lines (71 loc) · 1.33 KB
/
lab6_2.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
66
67
68
69
70
71
//reverse a singly linkedlist using recursion
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} * first, *ptr;
void create(int);
void display();
void reverse(struct node *);
int main()
{
first = NULL;
ptr = NULL;
int a, n;
printf("Enter the number of nodes \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter Data \n");
scanf("%d", &a);
create(a);
}
printf("Linked list: \n");
display();
reverse(first);
printf("\nLinked list after reversing it: \n");
display();
return 0;
}
void create(int a)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = a;
temp->next = NULL;
if (first == NULL)
{
first = temp;
}
else
{
ptr = first;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void display()
{
ptr = first;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
void reverse(struct node *ptr)
{
if (ptr->next == NULL)
{
first = ptr;
return;
}
reverse(ptr->next);
struct node *q;
q = ptr->next;
q->next = ptr;
ptr->next = NULL;
}