forked from bhaveshlohana/HacktoberFest2020-Contributions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting Linked list
101 lines (95 loc) · 2.03 KB
/
Sorting Linked list
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
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void sort();
struct Node{
int data;
struct Node *next;
}
*Start =NULL;
int main()
{
printf("This is my first link list program\n");
while(1)
{
int choice;
printf("\nEnter your choice\n1.For creating\n2.For Displaying\n3.Sorting element in liste\n4.For exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
break;
case 2:display();
break;
case 3:sort();
break;
case 4:exit(0);
}
}
return 0;
}
void create()
{
int a;
do{
struct Node *current,*newnode;
newnode=(struct Node*)malloc(sizeof(struct Node));
printf("\nEnter the data to be entered in the node: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(Start==NULL)
{
current=newnode;
Start=newnode;
}
else{
current->next=newnode;
current=newnode;
}
printf("Do you want to continue creating nodes\n1. to continue\n0. to stop: ");
scanf("%d",&a);
}
while(a!=0);
}
void display()
{
struct Node *temp;
temp=Start;
printf("The linked list is: ");
if(Start==NULL)
{
printf("the list is empty\n");
}
else{
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
}
void sort()
{
struct Node *temp,*temp1;
temp=Start;
int swap;
if(Start==NULL)
printf("List is empty\n");
else{
while(temp->next!=NULL)
{
for(temp1=temp->next;temp1!=NULL;temp1=temp1->next)
{
if(temp->data>temp1->data)
{
swap=temp1->data;
temp1->data=temp->data;
temp->data=swap;
}
}
temp=temp->next;
}
}
}