Initial Commit
This commit is contained in:
BIN
Submissions/As9/CS1083-A9-W2021.pdf
Normal file
BIN
Submissions/As9/CS1083-A9-W2021.pdf
Normal file
Binary file not shown.
BIN
Submissions/As9/IsaacShoebottom_As9_Report.docx
Normal file
BIN
Submissions/As9/IsaacShoebottom_As9_Report.docx
Normal file
Binary file not shown.
BIN
Submissions/As9/IsaacShoebottom_As9_Report.pdf
Normal file
BIN
Submissions/As9/IsaacShoebottom_As9_Report.pdf
Normal file
Binary file not shown.
BIN
Submissions/As9/IsaacShoebottom_As9_Report.zip
Normal file
BIN
Submissions/As9/IsaacShoebottom_As9_Report.zip
Normal file
Binary file not shown.
96
Submissions/As9/IsaacShoebottom_As9_Report/src/Sparse.java
Normal file
96
Submissions/As9/IsaacShoebottom_As9_Report/src/Sparse.java
Normal file
@ -0,0 +1,96 @@
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Sparse {
|
||||
private Node head;
|
||||
|
||||
Sparse(String file) {
|
||||
try {
|
||||
readList(file);
|
||||
} catch (FileNotFoundException exception) {
|
||||
System.err.println("File not found");
|
||||
}
|
||||
}
|
||||
|
||||
private void readList(String filename) throws FileNotFoundException {
|
||||
File file = new File(filename);
|
||||
Scanner sc = new Scanner(file);
|
||||
while (sc.hasNextLine()){
|
||||
long index = sc.nextLong();
|
||||
long value = sc.nextLong();
|
||||
insertValue(index, value);
|
||||
if (sc.hasNextLine())
|
||||
sc.nextLine();
|
||||
}
|
||||
}
|
||||
|
||||
public void mergeLists(Sparse other) {
|
||||
Node headNode = other.head;
|
||||
while (headNode != null) {
|
||||
insertValue(headNode.index, headNode.value);
|
||||
headNode = headNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
private void insertValue(long index, long value) {
|
||||
if (head == null) {
|
||||
head = new Node(index, value);
|
||||
}
|
||||
else {
|
||||
Node nextNode = head;
|
||||
Node lastNode = head;
|
||||
if (value == 0)
|
||||
return;
|
||||
while (nextNode != null && nextNode.index < index) {
|
||||
lastNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
}
|
||||
if (nextNode != null && nextNode.index == index) {
|
||||
nextNode.value += value;
|
||||
if (nextNode.value == 0) {
|
||||
lastNode.next = nextNode.next;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Node insertedNode = new Node(index, value);
|
||||
insertedNode.next = lastNode.next;
|
||||
lastNode.next = insertedNode;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void printRecR(Node node) {
|
||||
if (node != null) {
|
||||
System.out.println(node.index + " : " + node.value);
|
||||
printRecR(node.next);
|
||||
}
|
||||
}
|
||||
|
||||
public void printRec() {
|
||||
printRecR(head);
|
||||
}
|
||||
|
||||
private void printRecBackwardsR(Node node) {
|
||||
if (node != null) {
|
||||
printRecBackwardsR(node.next);
|
||||
System.out.println(node.index + " : " + node.value);
|
||||
}
|
||||
}
|
||||
|
||||
public void printRecBackwards() {
|
||||
printRecBackwardsR(head);
|
||||
}
|
||||
|
||||
private static class Node {
|
||||
long index;
|
||||
long value;
|
||||
Node next;
|
||||
Node(long index, long value) {
|
||||
this.index = index;
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
public class TestSparse {
|
||||
public static void main(String[] args) {
|
||||
Sparse sparse1 = new Sparse(args[0]);
|
||||
Sparse sparse2 = new Sparse(args[1]);
|
||||
|
||||
sparse1.mergeLists(sparse2);
|
||||
sparse1.printRec();
|
||||
System.out.println("now backwards:");
|
||||
sparse1.printRecBackwards();
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
0 6
|
||||
12 36
|
||||
25 2
|
||||
316 18
|
@ -0,0 +1,4 @@
|
||||
5 13
|
||||
12 4
|
||||
25 -2
|
||||
55 -3
|
@ -0,0 +1,6 @@
|
||||
0 6
|
||||
0 8
|
||||
9090 7
|
||||
12 36
|
||||
25 2
|
||||
316 18
|
@ -0,0 +1,6 @@
|
||||
5 13
|
||||
12 4
|
||||
25 -2
|
||||
55 -3
|
||||
0 0
|
||||
0 7
|
BIN
Submissions/As9/test1.png
Normal file
BIN
Submissions/As9/test1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.3 KiB |
BIN
Submissions/As9/test2.png
Normal file
BIN
Submissions/As9/test2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.1 KiB |
Reference in New Issue
Block a user