Initial Commit
This commit is contained in:
8
Source Code/Assignment 9/.idea/.gitignore
generated
vendored
Normal file
8
Source Code/Assignment 9/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/../../../../../../../:\ProgrammingProjects\JavaProjects\JavaYear1\CS1083\Assignment 9\.idea/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
7
Source Code/Assignment 9/.idea/codeStyles/Project.xml
generated
Normal file
7
Source Code/Assignment 9/.idea/codeStyles/Project.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<ScalaCodeStyleSettings>
|
||||
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||
</ScalaCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
5
Source Code/Assignment 9/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
Source Code/Assignment 9/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
6
Source Code/Assignment 9/.idea/discord.xml
generated
Normal file
6
Source Code/Assignment 9/.idea/discord.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
</component>
|
||||
</project>
|
6
Source Code/Assignment 9/.idea/misc.xml
generated
Normal file
6
Source Code/Assignment 9/.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
Source Code/Assignment 9/.idea/modules.xml
generated
Normal file
8
Source Code/Assignment 9/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Assignment 9.iml" filepath="$PROJECT_DIR$/Assignment 9.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
Source Code/Assignment 9/Assignment 9.iml
Normal file
11
Source Code/Assignment 9/Assignment 9.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
4
Source Code/Assignment 9/input/test1a.in
Normal file
4
Source Code/Assignment 9/input/test1a.in
Normal file
@ -0,0 +1,4 @@
|
||||
0 6
|
||||
12 36
|
||||
25 2
|
||||
316 18
|
4
Source Code/Assignment 9/input/test1b.in
Normal file
4
Source Code/Assignment 9/input/test1b.in
Normal file
@ -0,0 +1,4 @@
|
||||
5 13
|
||||
12 4
|
||||
25 -2
|
||||
55 -3
|
6
Source Code/Assignment 9/input/test2a.in
Normal file
6
Source Code/Assignment 9/input/test2a.in
Normal file
@ -0,0 +1,6 @@
|
||||
0 6
|
||||
0 8
|
||||
9090 7
|
||||
12 36
|
||||
25 2
|
||||
316 18
|
6
Source Code/Assignment 9/input/test2b.in
Normal file
6
Source Code/Assignment 9/input/test2b.in
Normal file
@ -0,0 +1,6 @@
|
||||
5 13
|
||||
12 4
|
||||
25 -2
|
||||
55 -3
|
||||
0 0
|
||||
0 7
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
96
Source Code/Assignment 9/src/Sparse.java
Normal file
96
Source Code/Assignment 9/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;
|
||||
}
|
||||
}
|
||||
}
|
11
Source Code/Assignment 9/src/TestSparse.java
Normal file
11
Source Code/Assignment 9/src/TestSparse.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user