-
Notifications
You must be signed in to change notification settings - Fork 256
/
16 Reversing using Recursion.c
66 lines (60 loc) · 1.16 KB
/
16 Reversing using Recursion.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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
}*first=NULL;
void Create(int A[], int n)
{
int i;
struct Node* last, * t;
first = (struct Node*)malloc(sizeof(struct Node));
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < n; i++)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node* p)
{
while (p != NULL)
{
printf("->%d", p->data);
p = p->next;
}
}
void RDisplay(struct Node* p)
{
if (p!=NULL)
{
printf("->%d", p->data);
RDisplay(p->next);
}
}
//reversing using Recursion
void Rreverse(struct Node* q, struct Node* p) // taking two pointer p and q ; p will always ahead and q will follow that;
{
if (p != NULL)
{
Rreverse(p, p->next); // passing two pointer
p->next = q;// while recturnig it will make p next as q
}
else
first = q; // while p i null make first as q ;
}
int main()
{
int A[] = { 10,20,30,40,50 };
Create(A, sizeof (A)/sizeof (int ));
Rreverse(NULL,first);
Display(first);
printf("\n\n");
return 0;
}