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 matrix;

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 path = new 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) {

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 = new LinkedList(path);

result.add(e);

printPath(result);

}

else if(!path.contains(i)){

LinkedList newPath = new LinkedList(path);

//newPath.add(i);

findPath(i, e, newPath);

}

}

}

}

private void printPath(LinkedList path) {

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!