-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_a_linked_list.c
60 lines (58 loc) · 1.07 KB
/
reverse_a_linked_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
#include <stdio.h>
#include <stdlib.h>
typedef struct node Node;
struct node
{
int data;
struct node *next;
};
void display(Node *first);
void reverse(Node **first);
void create(Node **first, int array[], int len);
int main(int argc, char* argv[])
{
Node *first;
first =(Node*)malloc(sizeof(Node));
int array[]={1,2,3,4,5,6};
int len = sizeof(array)/sizeof(int);
create(&first,array,len);
reverse(&first);
display(first);
return 0;
}
void display(Node *first)
{
while (first != NULL)
{
printf("%d->",first->data);
first=first->next;
}
}
void reverse(Node **first)
{
Node *p,*q,*r;
p=(*first);
while (p != NULL)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
(*first)=q;
}
void create(Node **first, int array[], int len)
{
Node *p,*end;
(*first)->data=array[0];
(*first)->next=NULL;
end=(*first);
for(int i = 1; i<len; i++)
{
p=(Node*)malloc(sizeof(Node));
p->data=array[i];
p->next=NULL;
end->next=p;
end=p;
}
}