-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_of_linked_list.cpp
93 lines (82 loc) · 1.57 KB
/
sorting_of_linked_list.cpp
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
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* next;
};
struct node* head=NULL;
class linked
{
public:
void insert(int x)
{
struct node* temp=new node();
temp->data=x;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
struct node* temp1=head;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next =temp;
}
}
void sort()
{
struct node* temp=head;
while(temp->next!=NULL)
{
struct node* temp1=temp->next;
while(temp1!=NULL)
{
if(temp->data > temp1->data)
{
swap(temp->data,temp1->data);
}
temp1=temp1->next;
}
temp=temp->next;
}
}
void disp()
{
struct node* temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
};
int main()
{
linked l;
l.insert(2);
l.insert(18);
l.insert(-84);
l.insert(90);
l.insert(42);
l.insert(81);
l.insert(34);
l.insert(-101);
l.insert(6);
l.insert(48);
l.insert(54);
l.insert(5);
l.insert(-6);
l.insert(8);
l.insert(-54);
l.insert(10);
l.disp();
l.sort();
l.disp();
return 0;
}