Question
Given the following Java code, which find the path in the matrix. I used linked list to store the matrix. If I do not use
Given the following Java code, which find the path in the matrix. I used linked list to store the matrix. If I do not use Java library linked list, how should I implement linked list from scratch. That means how to write own linked list structure to replace the current linked list?( The new code will not have import java.util.linkedlist)
Make sure the new linked list code from scratch and the following code compile in Eclipse. You cannot use any other Java Library, such as array list and interator.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.Scanner;
public class PathFinder {
public static void main(String args[]){
if(args.length != 2){
System.out.println("Usage: java pathFiner [input file] [output file]");
System.exit(1);
}
PathFinder pf = new PathFinder(args[0], args[1]);
}
private boolean noPath;
private int mSize; // matrix size n
private LinkedList
private String inputFile;
private String outputFile;
public PathFinder(String inputFile, String outputFile){
this.inputFile = inputFile;
this.outputFile = outputFile;
readInAllData();
}
private void readInAllData() {
try {
Scanner sc = new Scanner(new File(inputFile));
PrintStream ps = new PrintStream(outputFile);
System.setOut(ps);
while(sc.hasNextInt()){
int n = sc.nextInt();
mSize = n;
this.matrix = new LinkedList<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
matrix.add(sc.nextInt());
}
}
this.printMatrix();
this.findAllPath();
}
sc.close();
} catch (FileNotFoundException e) {
System.out.println("Can't open input file - " + inputFile);
System.exit(1);
}
}
private void printMatrix() {
int n = mSize;
System.out.println("Matrix: ");
System.out.println(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(matrix.get(i*n+j) + " ");
}
System.out.println();
}
System.out.println();
}
public void findAllPath(){
int n = mSize;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
this.noPath = true;
LinkedList
System.out.printf("Path From %d to %d: ", i+1, j+1);
findPath(i, j, path);
if(this.noPath){
System.out.println("No Path Found");
}
System.out.println();
}
}
}
private void findPath(int s, int e, LinkedList
path.add(s);
// TODO Auto-generated method stub
for(int i = 0; i < mSize; i++){
if(matrix.get(s*mSize+i) == 1){
if(i == e){
this.noPath = false;
LinkedList
result.add(e);
printPath(result);
}
else if(!path.contains(i)){
LinkedList
//newPath.add(i);
findPath(i, e, newPath);
}
}
}
}
private void printPath(LinkedList
for(int i = 0; i < path.size() - 1; i++){
System.out.printf("%d -> ", path.get(i)+1);
}
System.out.println(path.get(path.size()-1) + 1);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started