-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathList.java
More file actions
69 lines (64 loc) · 1.93 KB
/
Copy pathList.java
File metadata and controls
69 lines (64 loc) · 1.93 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
import java.io.File;
public class List {
private ListNode head;
private ListNode tail;
public List(){
this.head = null;
this.tail = null;
}
//Used to insert a new node into the list
//always inserting at the end of the list
public void insertNode(String fileName){
ListNode newNode = new ListNode(fileName);
if(this.tail == null){
this.head = newNode;
}
else{
this.tail.setNext(newNode);
}
this.tail = newNode;
}
//Searches for the node with the name of the file
//if the node is not found, then creates a new node
//and inserts in the end of the list
public void addNodeFrequency(String fileName){
ListNode curr = head;
while(curr != null){
//Node found
if(curr.getFileName().equals(fileName)){
curr.increaseFrequency();
return;
}
if(curr.getNext() == null){
insertNode(fileName);
return;
}
curr = curr.getNext();
}
insertNode(fileName);
}
//Gets the length of the List
public int length(){
ListNode curr = head;
int counter = 0;
while(curr != null){
counter++;
curr = curr.getNext();
}
return counter;
}
//Prints all the occurrences of the
//word in each file and the total frequency
public void print() {
ListNode curr = head;
int totalFrequency = 0;
// Traverse the list until the end
while (curr != null) {
String fileName = new File(curr.getFileName()).getName();
System.out.printf("File: %s | Frequency: %d\n", fileName, curr.getFrequency());
totalFrequency += curr.getFrequency();
curr = curr.getNext();
}
System.out.println("Total occurrences: " + totalFrequency);
}
}