-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderedlist.java
160 lines (156 loc) · 5.1 KB
/
orderedlist.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package javaapplication1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Scanner;
import static javaapplication1.unorderedlist.print;
import static javaapplication1.unorderedlist.printtofile;
public class orderedlist {
Node head;
Node tail;
public static void add(orderedlist list,int data){
Node newnode=new Node(data);
if(list.head==null){ list.head=newnode;list.tail=newnode;}
else{
Node temp=list.tail;
temp.next=newnode;
newnode.pre=temp;
list.tail=newnode;
}
}
public static boolean ispresent(orderedlist list,int data){
Node temp=list.head;
while(temp!=null) {
if((temp.data).equals(data)) return true;
temp=temp.next;
}
return false;
}
public static void remove(orderedlist list,int data){
Node temp=list.head;
if((list.head.data).equals(data)){
list.head=temp.next;
}
else{
Node temppre=list.head;
while(!temp.data.equals(data) ) {
temppre=temp;
if(temp.next==null) break;
temp=temp.next;
}
temppre.next=temp.next;
temp.pre=temppre;
}
}
public static void sort(orderedlist list){
mergesort(list.head,list.tail,list);
}
public static void print(orderedlist list){
Node temp=list.head;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
public static orderedlist twolistsort(orderedlist l1,orderedlist l2){
Node temp1=l1.head;
Node temp2=l2.head;
orderedlist l3=new orderedlist();
while(temp1!=l1.tail.next && temp2!=l2.tail.next){
if((int)temp1.data<(int)temp2.data){
l3.add(l3,(int)temp1.data);
temp1=temp1.next;
}
else{
l3.add(l3,(int)temp2.data);
temp2=temp2.next;
}
}
while(temp1!=l1.tail.next){
l3.add(l3,(int)temp1.data);
temp1=temp1.next;
}
while(temp2!=l2.tail.next){
l3.add(l3,(int)temp2.data);
temp2=temp2.next;
}
return l3;
}
public static void mergesort(Node f,Node l,orderedlist list){
if(f!=l){
Node temp1=f;
Node temp2=l;
while(temp1.next!=temp2 && temp1!=temp2){
temp1=temp1.next; temp2=temp2.pre;
}
mergesort(f,temp1,list);
mergesort(temp1.next,l,list);
merge(f,temp1,l);
}
}
public static void merge(Node f,Node mid,Node l){
orderedlist l1=new orderedlist();
orderedlist l2=new orderedlist();
Node temp1=f;
while(temp1!=mid.next){
l1.add(l1,(int) temp1.data);
temp1=temp1.next;
}
Node temp2=mid.next;
while(temp2!=l.next){
l2.add(l2,(int) temp2.data);
temp2=temp2.next;
}
orderedlist l3=twolistsort(l1,l2);
temp1=f;
temp2=l3.head;
while(temp2!=null){
temp1.data=temp2.data;
temp1=temp1.next;
temp2=temp2.next;
}
}
public static void printtofile(orderedlist list) throws IOException{
File f;
f = new File("C:\\Users\\YATHIN\\Documents\\NetBeansProjectsbridge\\JavaApplication1\\src\\javaapplication1\\textfile.txt");
FileWriter fw=new FileWriter(f);
Node temp=list.head;
while(temp!=null){
fw.write((String)(temp.data).toString()+" ");
temp=temp.next;
}
fw.flush();
System.out.println();
}
public static void main(String args[]) throws IOException{
String filesdata = new String(Files.readAllBytes(Paths.get("C:\\Users\\YATHIN\\Documents\\NetBeansProjectsbridge\\JavaApplication1\\src\\javaapplication1\\textfile.txt")));
orderedlist list=new orderedlist();
filesdata=filesdata.replaceAll("\n", " ");
String[] arr=filesdata.split(" ");
for(int i=0;i<arr.length;i++){
list.add(list,Integer.parseInt(arr[i]));
}
print(list);
sort(list);
print(list);
System.out.println("enter word to search:");
Scanner sc=new Scanner(System.in);
int ans=sc.nextInt();
if(list.ispresent(list,ans)){
list.remove(list,ans);
System.out.println(ans+" is removed from the list");
}
else{
orderedlist l1=new orderedlist();
l1.add(l1,ans);
list=twolistsort(list,l1);
System.out.println(ans+" is added to the list");
}
print(list);
printtofile(list);
}
}