-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path41. Reversing a LL.c
67 lines (57 loc) · 957 Bytes
/
41. Reversing a LL.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
#include<stdio.h>
struct node
{
int data;
struct node* next;
};
struct node* head, *newnode,*temp,*currentnode,*nextnode,*prevnode;
int choice;
void main()
{
head=0;
do
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the data : ");
scanf("%d",&newnode->data);
if (head==0)
{
head = temp =newnode;
}
else
{
temp->next = newnode;
temp=newnode;
}
printf("\ndo you want to add a new node : 1.YES 2.NO ");
scanf("%d",&choice);
}while(choice==1);
temp->next=0;
display();
reverse();
display();
}
void display()
{
printf("\n\nThe data in the nodes are : \t");
temp=head;
while(temp!=0)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL");
}
void reverse()
{
prevnode =0;
currentnode = nextnode = head;
while(currentnode!=0)
{
nextnode =currentnode->next;
currentnode->next = prevnode;
prevnode=currentnode;
currentnode=nextnode;
}
head=prevnode;
}