-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseLL.c
74 lines (65 loc) · 1.67 KB
/
ReverseLL.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
#include <stdio.h>
struct node{
int data;
struct node *next;
};*head, *tail = NULL;
void createNode(int data){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
}
void sortList() {
struct node *current = head, *index = NULL;
int temp;
if(head == NULL) {
return;
}
else {
while(current != NULL) {
index = current->next;
while(index != NULL) {
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL) {
printf("%d---->", current->data);
current = current->next;
}
printf("NULL");
printf("\n");
}
int main()
{
createNode(40);
createNode(30);
createNode(10);
createNode(20);
printf("Original list: \n");
display();
sortList();
printf("Sorted list: \n");
display();
return 0;
}