-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_a_LL.c
82 lines (77 loc) · 1.72 KB
/
sort_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void display(struct node *head)
{
struct node *ptr = head;
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
struct node *create_LL(struct node *head)
{
struct node *temp = NULL;
struct node *new;
int c = 1;
while (c)
{
new = (struct node *)malloc(sizeof(struct node));
new->next = NULL;
printf("Enter data: ");
scanf("%d", &new->data);
if (head == NULL)
{
head = temp = new;
}
else
{
temp->next = new;
temp = new;
}
printf("Enter 0 for break & 1 for continue: ");
scanf("%d", &c);
}
return head;
}
struct node *Sort_a_LL(struct node *head)
{
struct node* curr = head,*nxt = head;
int temp;
if(head == NULL) {
return NULL;
}
else{
while (curr)
{
nxt = curr->next;
while (nxt)
{
if (curr->data > nxt->data)
{
temp = curr->data;
curr->data = nxt->data;
nxt->data = temp;
}
nxt=nxt->next;
}
curr = curr->next;
}
}
return head;
}
int main()
{
struct node *head = NULL;
head = create_LL(head);
display(head);
head = Sort_a_LL(head);
display(head);
return 0;
}