-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDupli.java
More file actions
86 lines (74 loc) · 1.87 KB
/
Dupli.java
File metadata and controls
86 lines (74 loc) · 1.87 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
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
import java.util.Scanner;
public class Dupli {
class Node{
Node next;
int data;
Node(int data){
this.data=data;
this.next=null;
}
}
Node head=null;
void in(int data){
Node newnode=new Node(data);
if(head==null){
head=newnode;
return;
}
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=newnode;
}
void sort(){
Node current,index;
for(current=head;current!=null&¤t.next!=null;current=current.next){
for(index=current.next;index!=null;index=index.next){
if(current.data>index.data){
int temp=current.data;
current.data=index.data;
index.data=temp;
}
}
}
}
void delete(){
Node current = head;
while (current != null && current.next != null) {
if (current.data == current.next.data) {
current.next = current.next.next;
} else {
current = current.next;
}
}
}
void traverse(){
if(head==null){
System.out.println("empty");
return;
}
Node temp=head;
while(temp!=null){
System.out.println(temp.data+"-->");
temp=temp.next;
}
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
Dupli d=new Dupli();
d.in(10);
d.in(5);
d.in(10);
d.in(10);
d.in(5);
System.out.println("initial linked list");
d.traverse();
d.sort();
System.out.println("sorted linked list");
d.traverse();
System.out.println("duplicate removed linked list");
d.delete();
d.traverse();
}
}