FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DSA-PS-Java-Programs/SortLinkedList.java at master · codec-akash/DSA-PS-Java-Programs · GitHub
codec-akash
/
DSA-PS-Java-Programs
Public
Notifications
You must be signed in to change notification settings
Fork
32
Star
2
Code
Issues
1
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DSA-PS-Java-Programs
/
SortLinkedList.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
141 lines (130 loc) · 2.95 KB
Breadcrumbs
DSA-PS-Java-Programs
/
SortLinkedList.java
Copy path
File metadata and controls
141 lines (130 loc) · 2.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
class
LinkedList
{
Node
head
;
public
LinkedList
()
{
this
.
head
=
null
;
}
void
insertAtEnd
(
int
data
)
{
Node
newNode
=
new
Node
(
data
);
if
(
head
==
null
)
{
head
=
newNode
;
}
else
{
Node
current
=
head
;
while
(
current
.
next
!=
null
)
{
current
=
current
.
next
;
}
current
.
next
=
newNode
;
}
}
void
insertAtStart
(
int
data
)
{
Node
newNode
=
new
Node
(
data
);
if
(
head
==
null
)
{
head
=
newNode
;
}
else
{
newNode
.
next
=
head
;
head
=
newNode
;
}
}
void
traverse
()
{
Node
n
=
head
;
while
(
n
!=
null
)
{
System
.
out
.
print
(
n
.
data
+
" -> "
);
n
=
n
.
next
;
}
}
void
sortList
()
{
int
swap
=
0
;
Node
n
=
head
;
Node
s1
=
head
;
Node
s2
=
head
;
while
(
s1
!=
null
)
{
s2
=
head
;
Node
tmp
=
head
;
Node
tmp2
=
null
;
while
(
s2
.
next
!=
null
)
{
if
(
s2
.
data
>
s2
.
next
.
data
)
{
swap
++;
if
(
s2
==
head
)
{
head
=
s2
.
next
;
}
tmp2
=
s2
.
next
.
next
;
s2
.
next
.
next
=
s2
;
tmp
.
next
=
s2
.
next
;
s2
.
next
=
tmp2
;
}
tmp
=
s2
;
s2
=
s2
.
next
;
if
(
s2
==
null
)
{
break
;
}
}
s1
=
s1
.
next
;
}
System
.
out
.
println
(
"Swap "
+
swap
);
}
}
class
Node
{
int
data
;
Node
next
;
public
Node
(
int
data
)
{
this
.
data
=
data
;
this
.
next
=
null
;
}
}
public
class
Main
{
public
static
void
main
(
String
[]
args
) {
LinkedList
l1
=
new
LinkedList
();
l1
.
insertAtEnd
(
5
);
l1
.
insertAtEnd
(
2
);
l1
.
insertAtEnd
(
3
);
l1
.
insertAtEnd
(
1
);
l1
.
insertAtEnd
(
12
);
l1
.
insertAtEnd
(-
1
);
l1
.
insertAtEnd
(
14
);
l1
.
insertAtEnd
(
15
);
l1
.
insertAtEnd
(
16
);
l1
.
insertAtEnd
(-
5
);
l1
.
insertAtEnd
(
6
);
l1
.
insertAtEnd
(
7
);
l1
.
insertAtEnd
(
8
);
l1
.
insertAtEnd
(
9
);
l1
.
insertAtEnd
(
10
);
l1
.
insertAtEnd
(-
11
);
l1
.
insertAtEnd
(
17
);
l1
.
insertAtEnd
(-
18
);
l1
.
insertAtEnd
(
0
);
l1
.
insertAtEnd
(-
222
);
l1
.
traverse
();
System
.
out
.
println
();
l1
.
sortList
();
l1
.
traverse
();
System
.
out
.
println
();
}
}
Back
|
FazBrowse Home
|
New Git URL