forked from Shwetabhdixit/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortinglinkedlist.c
89 lines (66 loc) · 1.3 KB
/
sortinglinkedlist.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
//SORTING A LINKED LIST..
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *start = NULL;
int main()
{
int i,num;
struct node *temp, *new1 , *temp1,*j;
char k;
printf("\nFirst create a list..\n");
printf("\nEnter the number of elements you want to enter: ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
new1 = (struct node *)malloc(sizeof(struct node));
printf("\nEnter element %d: ",i+1);
scanf("%d",&new1->data);
new1->next=NULL;
if(start == NULL)
{
start = new1;
temp1 = start;
}
else
{
temp1->next = new1;
temp1= new1;
}
}
temp =start;
while(temp!=NULL)
{
printf("[%d]-->",temp->data);
temp = temp->next;
}
printf("NULL\n");
printf("\n\n");
//Now we have created list , we need to reverse it.
for(i=1;i<num;i++)
{
for(j=start;j->next!=NULL;j=j->next)
{
if(j->data > j->next->data)
{
temp = j->data;
j->data = j->next->data;
j->next->data = temp;
}
}
}
//Now, list is sorted. we will print it.
printf("\nAfter sorting..\n\n");
temp =start;
while(temp!=NULL)
{
printf("[%d]-->",temp->data);
temp = temp->next;
}
printf("NULL\n");
printf("\n\n");
return 0;
}