-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseLinkList.c
111 lines (100 loc) · 1.91 KB
/
ReverseLinkList.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<stdio.h>
#include<stdlib.h>
struct node {
int info;
struct node *next;
};
struct node* getnode()
{
return (struct node*)malloc(sizeof(struct node));
}
struct node *list = NULL;
void insert(int x)
{
if(list == NULL)
{
struct node *nn;
nn = getnode();
nn->info = x;
nn->next = NULL;
list = nn;
}
else
{
struct node *nn, *temp;
nn = getnode();
nn->info = x;
nn->next = NULL;
temp = list;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = nn;
}
}
void show()
{
if(list == NULL)
{
printf("List is Empty!\n");
return;
}
struct node *temp;
temp = list;
while(temp!=NULL)
{
printf("%d ", temp->info);
temp = temp->next;
}
printf("\n");
}
void reverse()
{
struct node* prev = NULL;
struct node* current = list;
struct node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
list = prev;
}
int main()
{
int ch, n;
while(1){
printf("----Menu----\n");
printf("1. Insert Element\n");
printf("2. Show Element\n");
printf("3. Reverse List\n");
printf("4. Exit\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter a value to insert : ");
scanf("%d", &n);
insert(n);
break;
case 2:
show();
break;
case 3:
reverse();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice! Try Again!\n");
}
}
return 0;
}