-
Notifications
You must be signed in to change notification settings - Fork 0
/
REVERSING A SLL.c
83 lines (83 loc) · 1.86 KB
/
REVERSING A SLL.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
#include<stdio.h>
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("#### value is %d #### ",ptr->info );
printf("location is %d #### pointing next to %d ####\n",ptr,ptr->next);
ptr=ptr->next ;
}
}
}
void reverse_list()
{
struct node *cnode,*pnode,*nnode;
cnode=start;
pnode=start;
nnode=start->next;
cnode->next=NULL;
while(nnode!=NULL)
{
pnode=cnode;
cnode=nnode;
nnode=nnode->next;
cnode->next=pnode;
}
start=cnode;
}
int main()
{
int n,i;
printf("ENTER THE NUMBER OF NODES YOU WANT IN THE LIST :: \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();
}
display();
reverse_list();
printf("AFTER REVERSING THE LIST:: ");
display();
}