/
rlist.c
90 lines (65 loc) · 1.24 KB
/
rlist.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
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <malloc.h>
int count = 1;
struct Node
{
/* data */
int data;
struct Node *next;
};
typedef struct Node Rahul;
Rahul *head = NULL;
Rahul *last = NULL;
void insert(int n){
Rahul *x = (Rahul *)malloc(sizeof(Rahul));
x->data = n;
if(head == NULL){
head = last = x;
x->next = NULL;
}
else
{
last->next = x;
x->next = NULL;
last = x;
}
}
void display(){
Rahul *n = head;
while(n != NULL){
printf("%d -> ", n->data);
n = n->next;
}
}
void reverse(){
Rahul *ptr,*cpt,*pptr;
pptr = cpt = ptr = head;
while(ptr->next != NULL){
if(count == 1){
cpt = cpt->next;
pptr->next=NULL;
ptr = cpt;
}
else{
cpt=cpt->next;
ptr->next=pptr;
pptr=ptr;
ptr=cpt;
}
count = count + 1;
}
ptr->next=pptr;
head = ptr;
display();
}
int main()
{
insert(1);
insert(2);
insert(3);
insert(4);
display();
printf("\n\n");
reverse();
return 0;
}