-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISortList.java
More file actions
53 lines (53 loc) · 1.04 KB
/
Copy pathISortList.java
File metadata and controls
53 lines (53 loc) · 1.04 KB
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
import java.util.*;
public class ISortList {
private class Node{
Node next; int d;
Node(int d){
next=null;
this.d=d;
}
}
Node head;
void accept(int n) {
System.out.println("Rnter data");
head = new Node(new Scanner(System.in).nextInt());
Node temp;
for(int i=1;i<n;i++) {
temp=new Node(new Scanner(System.in).nextInt());
insert(temp);
}
}
void insert(Node t) {
if(t.d<head.d) {
t.next=head;
head=t;
return;
}
Node temp=head;
while(temp.next!=null) {
if((temp.next).d>t.d) {
Node x=temp.next;
temp.next=t;
t.next=x;
return;
}
temp=temp.next;
}
temp.next=t;
}
void disp() {
Node temp=head;
while(temp!=null) {
System.out.print(temp.d+",");
temp=temp.next;
}
System.out.println();
}
public static void main(String args[]) {
System.out.println("Enter number of nodes");
ISortList ob = new ISortList();
ob.accept(new Scanner(System.in).nextInt());
System.out.println("The sorted list: ");
ob.disp();
}
}