-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_List.java
158 lines (134 loc) · 3.86 KB
/
Linked_List.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Linked_List {
static File myFile;
class Node {
private Node next;
private String word;
private String lines ="";
public Node(String word,int lines) {
this.word = word;
this.lines = lines+" ";
this.next = null;
}
}
private Node head;
private Node tail;
private int length=0;
public Linked_List() {
head = null;
tail = null;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public void addNode(String word, int line) {
Node newNode = new Node(word,line);
if(head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
length++;
}
public void removDupsAndSave() throws FileNotFoundException {
//Erase dublicates and save to file function
myFile = new File("/Users/TamerAltaji/Desktop/Java-Workplace/DataStructureProject/src/output") ;
Scanner s = new Scanner(myFile);
PrintWriter pw = new PrintWriter(myFile);
pw.write("CONCORDANCE\n");
char Letter = this.head.word.charAt(0);
Node slow = this.head, fast = this.head.next, fastPrevious = this.head;
int checker = 1;
if(head !=null) {
while(slow != null) {
if(slow.word.charAt(0)!=Letter) {
Letter =slow.word.charAt(0) ;checker=1;
}
if(checker == 1) {
pw.write("\n"+(char)(Letter-32)+"\n");
checker=0;
}
if(fast.next!=null) {
while(slow.word.equals(fast.word)) {
slow.lines +=fast.lines;
if(fast.next==null) {
break;
}
fast = fast.next;
fastPrevious = fastPrevious.next;
}
}
pw.write(slow.word + " " + slow.lines+"\n");
if(fast.next != null) {
fast = fast.next;
fastPrevious = fastPrevious.next;
slow = fastPrevious;
}
else {
if(slow.next!=null) {
fastPrevious = fastPrevious.next;
slow = fastPrevious;
}
else break;
}
}
}
pw.write("\nThere are " + length + " words in the file");
pw.close();s.close();
System.out.println("Saved in output file!!");
}
Node sortedMerge(Node a, Node b)
{
Node result = null;
/* Base cases */
if (a == null)
return b;
if (b == null)
return a;
/* Pick either a or b, and recur */
if (a.word.compareTo(b.word)<0) {
result = a;
result.next = sortedMerge(a.next, b);
}
else {
result = b;
result.next = sortedMerge(a, b.next);
}
return result;
}
Node mergeSort(Node h)
{
//Base case : if head is null
if (h == null || h.next == null) {
return h;
}
Node middle = getMiddle(h);
Node nextofmiddle = middle.next;
middle.next = null;
Node left = mergeSort(h);
Node right = mergeSort(nextofmiddle);
Node sortedlist = sortedMerge(left, right);
return sortedlist;
}
//Utility function to get the middle of the linked list
public static Node getMiddle(Node head)
{
if (head == null)
return head;
Node slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}