-
Notifications
You must be signed in to change notification settings - Fork 0
/
revsllusingarray.c
94 lines (88 loc) · 1.46 KB
/
revsllusingarray.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
91
92
93
94
//Program to reverse a linked list in C using an auxillary array | Vishruth codes
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
} *root=NULL;
//function to append into the list
void create(int d)
{
struct node *temp,*p,*q;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
if(root==NULL)
{
root=temp;
}
else
{
p=root;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
}
//function to calculate the length of the list
int length()
{
int count=0;
struct node *p;
p=root;
while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}
//function to display the list
void display()
{
struct node *p;
p=root;
while(p!=NULL)
{
printf("%d -> ",p->data);
p=p->next;
}
}
//function to reverse the list
void revsll()
{
int ar[length()],i=0;
struct node *p;
p=root;
while(p)
{
ar[i++]=p->data;
p=p->next;
}
p=root;
i--;
while(p)
{
p->data=ar[i--];
p=p->next;
}
}
int main()
{
int len=6;
int ar[]={7,2,2,0,4,11};
printf("\nThe array: 7,2,2,0,4,11");
for(int i=0;i<len;i++)
{
create(ar[i]);
}
printf("\nThe linked list: ");
display();
printf("\nThe reversed S.L.L. is: ");
revsll();
display();
return 0;
}