-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_link_list.c
80 lines (74 loc) · 1.67 KB
/
reverse_link_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *createlinklist(int n);
void display(struct node *head);
struct node *revserse_link_list(struct node *head);
int main()
{
int n;
struct node *head = NULL;
printf("Enter how many nodes: ");
scanf("%d", &n);
head = createlinklist(n);
display(head);
printf("\n");
head = revserse_link_list(head);
display(head);
return 0;
}
struct node *createlinklist(int n)
{
struct node *head = NULL;
struct node *temp = NULL;
struct node *p = NULL;
for (int i = 0; i < n; i++)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->next = NULL;
printf("Enter data: ");
scanf("%d", &temp->data);
if (head == NULL)
{
head = temp;
}
else
{
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp;
}
}
return head;
}
void display(struct node *head)
{
struct node *p = head;
while (p != NULL)
{
printf("%d->", p->data);
p = p->next;
}
printf("NULL");
}
struct node *revserse_link_list(struct node *head)
{
struct node *previous, *current, *next;
previous = NULL;
current = head;
while (current != NULL)
{
next = current->next;
current->next = previous;
previous = current;
current = next;
}
return previous;
}