-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse LL.cpp
79 lines (74 loc) · 1.5 KB
/
Reverse LL.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
int data;
struct node * link;
};
void append ( struct node **, int ) ;
void display ( struct node * ) ;
void reverse (struct node **);
int main(){
struct node *p ;
p=NULL;
int n;
char ch[10];
do {
printf("Enter the value\n");
scanf("%d",&n);
append(&p,n);
printf("Do you want to add another node? Type Yes/No\n");
scanf("%s",ch);
} while(!strcmp(ch,"Yes"));
printf("The elements in the linked list are");
display(p);
printf("\n");
printf("The elements in the reversed linked list are");
reverse(&p);
display(p);
printf("\n");
return 0;
}
void append ( struct node **q, int num ) {
struct node *temp=*q;
struct node *nn=NULL;
nn=(struct node*)malloc(sizeof(struct node));
nn->data=num;
nn->link=NULL;
if(temp==NULL)
{
*q=nn;
}
else
{
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=nn;
}
}
void display ( struct node *q ) {
struct node *temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->link;
}
}
void reverse ( struct node **x ){
struct node *temp1,*temp2,*temp3;
temp1=temp2=temp3=*x;
temp1=temp1->link->link;
temp2=temp2->link;
temp3->link=NULL;
temp2->link=temp3;
while(temp1!=NULL)
{
temp3=temp2;
temp2=temp1;
temp1=temp1->link;
temp2->link=temp3;
}
*x=temp2;
}