Custom linked list having unwanted elements - java

I have made a custom linked list and node classes for school and I have decided to practice how to use it by making a text-twist like game
In my linked list class, I have a traverse method which prints out the word and how long the word is into the console.
The code I have so far is this:
MyLinkedList.java
package game;
public class MyLinkedList {
public int counter;
public MyNode head;
public MyNode tail;
public MyLinkedList() {
counter = 0;
}
public void InsertToHead(MyNode m) {
if (counter == 0) {
head = tail = m;
} else {
m.setNext(head);
head.setPrev(m);
head = m;
}
counter++;
}
public int size() {
return counter;
}
public boolean isEmpty() {
if (counter == 0) {
return true;
} else {
return false;
}
}
public MyNode retrieveWord(String s) {
MyNode n = head;
while (n.next != null) {
if (s.equals(n.getWord())) {
return n;
} else {
n = n.next;
}
}
if (s.equals(tail.getWord())) {
return tail;
}
return null;
}
public MyNode retrieveLength(int l) {
MyNode n = head;
while (n.next != null) {
if (l == n.getLength()) {
return n;
} else {
n = n.next;
}
}
if (l == tail.getLength()) {
return tail;
}
return null;
}
public void traverse() {
MyNode n = head;
if (head != null) {
while (n.next != null) {
System.out.println(n.getWord() + "\t" + n.getLength());
n = n.next;
}
System.out.println(tail.getWord() + "\t" + n.getLength());
}
}
}
MyNode.java
package game;
public class MyNode {
public String word;
public int length;
public MyNode next, previous;
public MyNode() {
word = null;
length = 0;
next = null;
previous = null;
}
public MyNode(String w, int l) {
word = w;
length = l;
next = null;
previous = null;
}
public void setNext(MyNode n) {
next = n;
}
public void setPrev(MyNode n) {
previous = n;
}
public void toHead(MyNode n){
while(n.previous != null){
n.setPrev(n);
}
}
public void setWord(String w){
word = w;
}
public String getWord(){
return word;
}
public void setLength(int l){
length = l;
}
public int getLength(){
return length;
}
public boolean hasNext(){
return next != null;
}
}
WordSort.java
package game;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class WordSort {
Scanner wordScan;
public WordSort() {
MyLinkedList sixLetters = new MyLinkedList();
MyLinkedList fiveLetters = new MyLinkedList();
MyLinkedList fourLetters = new MyLinkedList();
MyLinkedList threeLetters = new MyLinkedList();
MyLinkedList rejects = new MyLinkedList();
try {
wordScan = new Scanner(new File("corncob_lowercase.txt"));
wordScan.useDelimiter("\r\n");
MyLinkedList ll = new MyLinkedList();
while (wordScan.hasNext()) {
String temp = wordScan.next();
MyNode node = new MyNode();
node.setWord(temp);
node.setLength(temp.length());
ll.InsertToHead(node);
if (temp.length() == 6) {
sixLetters.InsertToHead(node);
} else if (temp.length() == 5) {
fiveLetters.InsertToHead(node);
} else if (temp.length() == 4) {
fourLetters.InsertToHead(node);
} else if (temp.length() == 3) {
threeLetters.InsertToHead(node);
} else {
rejects.InsertToHead(node);
}
}
wordScan.close();
threeLetters.traverse();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Missing File: corncob_lowercase.txt");
}
}
}
and finally, Driver.java
package game;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Driver extends JPanel {
private static final long serialVersionUID = 1L;
public Driver() {
new WordSort();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Hidden Word");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Driver());
frame.setVisible(true);
}
}
Whenever I run this code, all the linked lists (threeLetters, fourLetters, fiveLetters, and sixLetters) are completely fine until the very end, when threeLetters.traverse() is called, I get this output (only the end portion)
act 3
ace 3
aby 3
abe 3
abducts 7
abductors 9
abductor 8
abductions 10
abduction 9
abducting 9
abducted 8
abdominal 9
abdomens 8
abdomen 7
abdication 10
abdicating 10
abdicates 9
abdicated 9
abdicate 8
abbreviations 13
abbreviation 12
abbreviating 12
abbreviates 11
abbreviated 11
abbreviate 10
abattoirs 9
abattoir 8
abatement 9
abashed 7
abasement 9
abandons 8
abandonment 11
abandoned 9
abandon 7
abalone 7
aardwolf 8
abe 8
I can't seem to find out why it's occurring, but it looks like it's printing out everything after abe and it prints abe twice, once with it registering as 3 letters and once as 8 letters long!
I got the text file from this website:
http://www.mieliestronk.com/corncob_lowercase.txt

Your problem here is mainly one of design. The main flaw: attempting to use one instance MyNode in multiple linked lists.
When you InsertToHead(node) you're modifying the contents of node itself, specifically the head and next references.
Fix:
Simple declare a new MyNode for each LinkedList you want to use it in. In your project specifically you're using two for any given node. ll and one of the <inset_number>Letters lists. So declare a new MyNode for each list:
...
while (wordScan.hasNext()) {
String temp = wordScan.next();
MyNode node = new MyNode();
MyNode node2 = new MyNode();
node.setWord(temp);
node2.setWord(temp);
node.setLength(temp.length());
node2.setLength(temp.length());
ll.InsertToHead(node2);
...
That should fix your problem.
If you want to know WHY it was happening. Trace the code. It has to do with attempting to add nodes that already have some more nodes attached into a list.
Additional Notes:
Try your best to avoid public fields unless you're SURE you want them. I.E. in MyLinkedList someone using your class shouldn't be able to access (or even see!) the counter, head or tail, so those should be made private. If you really want to access them, create get and set methods
Your nested if block in WordSort is a perfect place for a switch like this:
switch(temp.length()) {
case 6:
sixLetters.InsertToHead(node);
break;
case 5:
fiveLetters.InsertToHead(node);
break;
case 4:
fourLetters.InsertToHead(node);
break;
case 3:
threeLetters.InsertToHead(node);
break;
default:
rejects.InsertToHead(node);
break;
}
MyNode works fine as a separate class. However I will many times choose to implement a simple class like a node as a nested class. It can make for some very clean code. Try it out!
Be careful when designing your classes. There are a lot of extra methods in your design. It can be easy to chug ahead creating methods you may or may not ever use. I like to only create methods when I see that I need them in a class that uses the class in question.
Happy Coding!

Related

How to speed up Depth First Search method?

I'm trying to do a Depth First Search of my graph, and something is slowing it down quite a lot and I'm not sure what.
Here is my Bag code:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Bag<Item> implements Iterable<Item> {
private Node<Item> first; // beginning of bag
private Node<Item> end;
private int n; // number of elements in bag
public int label;
public int edges;
public static class Node<Item> {
private Item item;
private Node<Item> next;
public int label;
public int edges;
}
public Bag() {
first = null; // empty bag initialized
end = null;
n = 0;
}
public void add(Item item) {
if (n==0) {
Node<Item> head = new Node<Item>(); // if bag is empty
first = head;
end = head;
head.item = item; // new node both first and end of bag
edges++;
n++;
}
else {
Node<Item> oldlast = end; // old last assigned to end of node
Node<Item> last = new Node<Item>();
last.item = item;
oldlast.next = last; // new node added after old last
end = last;
n++; // size increased
edges++;
}
}
public Iterator<Item> iterator() {
return new LinkedIterator(first); // returns an iterator that iterates over the items in this bag in arbitrary order
}
public class LinkedIterator implements Iterator<Item> {
private Node<Item> current;
public LinkedIterator(Node<Item> first) {
current = first; // iterator starts at head of bag
}
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException(); // if there is next item, current is moved to next
Item item = current.item;
current = current.next;
return item; // item is returned
}
}
}
Here is my driver:
import java.util.ArrayList;
import java.util.Random;
public class Driver {
public static ArrayList<Integer> randomNum(int howMany) {
ArrayList<Integer> numbers = new ArrayList<Integer>(howMany);
Random randomGenerator = new Random();
while (numbers.size() < howMany) {
int rand_int = randomGenerator.nextInt(10000);
if (!numbers.contains(rand_int)) {
numbers.add(rand_int);
}
}
return numbers;
}
public static void main(String[] args) {
ArrayList<Integer> num = randomNum(100);
Graph G = new Graph(num);
System.out.println("The length of longest path for this sequence with graph is: " + G.dfsStart(num));
}
}
I send an ArrayList of random integers to my dfsStart method from the driver, which looks at all the different paths for each starting node in my graph. my DepthFirstSearch method calls the getAdjList for each starting node to find its neighbors using my Bag adj, and then works its way down each path before backtracking.
Here is my Graph code, containing my longest path method:
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class Graph {
public final int V; // initializing variables and data structures
public Bag<Integer>[] adj;
public int longestPath;
public Graph(ArrayList<Integer> numbers) {
try {
longestPath = 0;
this.V = numbers.size();
adj = (Bag<Integer>[]) new Bag[V]; // bag initialized
for (int v = 0; v < V; v++) {
adj[v] = new Bag<Integer>();
}
for (int i = 0; i < V; i++) {
adj[i].label = numbers.get(i);
int j = (i + 1);
while (j < numbers.size()) {
if (numbers.get(i) < numbers.get(j)) {
addEdge(i, numbers.get(j));
}
j++;
}
}
}
catch (NoSuchElementException e) {
throw new IllegalArgumentException("invalid input format in Graph constructor", e);
}
}
public void addEdge(int index, int num) {
adj[index].add(num);
}
public int getIndex(int num) {
for (int i = 0; i < adj.length; i++) {
if (adj[i].label == num) {
return i;
}
}
return -1;
}
public Bag<Integer> getAdjList(int source) {
Bag<Integer> adjList = null;
for (Bag<Integer> list : adj) {
if (list.label == source) {
adjList = list;
break;
}
}
return adjList;
}
public int dfsStart(ArrayList<Integer> numbers) {
for (int i=0;i<numbers.size();i++) {
// Print all paths from current node
depthFirstSearch(numbers.get(i),new ArrayList<>(300));
}
return longestPath;
}
public void depthFirstSearch(int src, ArrayList<Integer> current) {
current.add(src);
Bag<Integer> srcAdj = getAdjList(src);
if (srcAdj.size() == 0) {
// Leaf node
// Print this path
longestPath = Math.max(longestPath, current.size());
}
for (int links : srcAdj) {
depthFirstSearch(links, current);
}
current.remove(current.size()-1);
}
}
I believe the suggestion below helped get rid of the error, but it is still unbelievably slow when trying to find the longest path in a graph of more than 150 vertices.
Even for a small dense graph there can be many unique paths from a src node. I tested for this input [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] there are 16777216 unique paths from all nodes. So you can expect OOM for bigger inputs. one way is to update the longestPath as soon as a path is found instead of adding it to the list.
Change this to later.
addtoCount(current.size());
to
longestPath = Math.max(longestPath, current.size());
Make sure longestPath is global and initialized to 0 before every test case.
Well, I do not know JAVA but that is an incredible lot of code for doing a simple thing such as depth first search.
In C++ it is done like this:
void cPathFinder::depthFirst(
int v)
{
// initialize visited flag for each node in graph
myPath.clear();
myPath.resize(nodeCount(), 0);
// start recursive search from starting node
depthRecurse(v, visitor);
}
void cPathFinder::depthRecurse(
int v )
{
// remember this node has been visited
myPath[v] = 1;
// look for new adjacent nodes
for (int w : adjacent(v))
if (!myPath[w])
{
// search from new node
depthRecurse(w);
}
}

Did I write the copy constructor for my Linked List program correctly?

I am working on a project for my Data Structures class that asks me to write a class to implement a linked list of ints.
Use an inner class for the Node.
Include the methods below.
Write a tester to enable you to test all of the methods with whatever data you want in any order.
I have to create three different constructors. One of the constructors is a copy constructor. I have my code down below showing what I did but I'm not sure I wrote this constructor correctly. I also have a method called addToFront one of the many methods I need to implement in this project. Can someone let me know what I would need to write for the copy constructor? I have no idea what I need to write for a copy constructor. I've tried looking it up but the examples shown don't match with what I'm trying to write.
public class LinkedListOfInts {
Node head;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts() {
}
public LinkedListOfInts(LinkedListOfInts other) {
}
public void addToFront(int x) {
head = new Node(x, head);
}
public String toString() {
String result = " ";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
result += ptr.value + " ";
return result;
}
public static void main(String[] args) {
LinkedListOfInts list = new LinkedListOfInts();
for (int i = 0; i < 15; i++)
list.addToFront(i);
System.out.println(list);
}
}
You can iterate over the nodes of the other list and sequentially create new tail nodes based on their values.
public LinkedListOfInts(LinkedListOfInts other) {
Node tail = null;
for(Node n = other.head; n != null; n = n.nextNode){
if(tail == null) this.head = tail = new Node(n.value, null);
else {
tail.nextNode = new Node(n.value, null);
tail = tail.nextNode;
}
}
}
// ...
public static void main(String[] args) {
LinkedListOfInts list = new LinkedListOfInts();
for (int i = 0; i < 15; i++)
list.addToFront(i);
LinkedListOfInts copy = new LinkedListOfInts(list);
System.out.println(list);
System.out.println(copy);
}

Why isnt this merge sort on linked list working properly?

(disclaimer: for school, so cant import other Java utilities)
So I have to merge sort on a linked list, and I have almost all of it down. Here it is:
class musicNode {
String track; // The name of the track
int played= 0; // The number of times played
int shuffleTag= 0; // For shuffling
musicNode next;
public musicNode() { // Here's how we construct an empty list.
next = null;
}
public musicNode(String t) {
track = t; next = null;
}
public musicNode(String t, musicNode ptr) {
track = t; next = ptr;
}
public boolean LTTrack(musicNode x) { // Compares tracks according to alphabetical order on strings
if (this.track.compareTo(x.track)<=0) return true;
else return false;
}
};
// This class represents a playlist;
// We assume that each track appears at most once in the playlist
public class MusicPlayer {
protected musicNode head = null; // Pointer to the top of the list.
int length=0; // the number of nodes in the list.
boolean debug= false;
public MusicPlayer() {
}
public void setToNull() {
head = null;
}
public boolean isEmpty() {
return head == null;
}
public musicNode head() {
return head;
}
void insertTrack(String name) { // Inserts a new track at the top of the list.
musicNode temp= new musicNode(name, head);
head= temp;
length++;
}
void sortTrack() { // TODO
musicNode main = this.head;
mergeSort(main);
}
public musicNode mergeSort(musicNode head) {
if ((head == null) || (head.next == null)){
return head;
}
musicNode left = head;
musicNode right = head.next;
while((right != null) && (right.next != null)){
head = head.next;
right = (right.next).next;
}
right = head.next;
head.next = null;
return merge(mergeSort(left), mergeSort(right));
}
There also this JUnit test:
public void testSortMixed() {
MusicPlayer trackList= new MusicPlayer();
trackList.insertTrack("d");
trackList.insertTrack("b");
trackList.insertTrack("e");
trackList.insertTrack("a");
trackList.insertTrack("c");
MusicPlayer trackListTwo= new MusicPlayer();
trackListTwo.insertTrack("e");
trackListTwo.insertTrack("d");
trackListTwo.insertTrack("c");
trackListTwo.insertTrack("b");
trackListTwo.insertTrack("a");
trackList.sortTrack();
musicNode tmp= trackList.head;
musicNode tmp2= trackListTwo.head;
for(int i=0; i< 5; i++){
assertEquals(tmp2.track, tmp.track);
tmp2= tmp2.next;
tmp=tmp.next;
}
}
The problem is that it sorts according to the last track you insert, and only from then on. So say you insert alphabets from a-f, but the last one you insert was "c", itll only show you "cdef". But if the last one was "a" then it works as intended.
So how it works is, when you insert a track it gets inserted onto the beginning of the list, not end, becoming the head. I feel like that may be whats messing it up, as I adapted and looked from my notes and online which insert at the bottom.
I dont know how to account for this though. Also I know it sorts based on what was inserted last (in the JUnit test above it sorts to "cde" because I created a main function and played around with it)
ANY help appreciated.
The key point is the second line in the method sortTrack:
void sortTrack() {
musicNode main = this.head;
this.head = mergeSort(main); // you forgot to set the head of linked list to the merged
}
I'd tested it in my laptop and everything goes okay now xD

How to implement LinkedList Bag ADT to create a spellchecker

Fully develop the classes for the Linked Implementation of the ADT Bag (i.e., LinkedBag, Node).
Test your classes well (call all methods) before you proceed.
Spell checker
Write a spell checker that:
a. Reads in words from an external file.
b. Tests each word against a dictionary (an instance of your LinkedBag class) of correctly spelled words (if the word is found in the dictionary then it’s spelled correctly).
I have already developed the classes for the linked implementation of the ADT Bag and also made the spellchecker.java
I need help to understand how to use and implement linkedbag class and my words.txt file for this problem.
Dictionary.java
package Bags;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Dictionary {
private int M = 1319; // prime number
final private Bucket[] array;
public Dictionary() {
this.M = M;
array = new Bucket[M];
for (int i = 0; i < M; i++) {
array[i] = new Bucket();
}
}
private int hash(String key) {
return (key.hashCode() & 0x7fffffff) % M;
}
// call hash() to decide which bucket to put it in, do it.
public void add(String key) {
array[hash(key)].put(key);
}
// call hash() to find what bucket it's in, get it from that bucket.
public boolean contains(String input) {
input = input.toLowerCase();
return array[hash(input)].get(input);
}
public void build(String filePath) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
add(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
//
public String[] getRandomEntries(int num) {
String[] toRet = new String[num];
for (int i = 0; i < num; i++) {
// pick a random bucket, go out a random number
Node n = array[(int) Math.random() * M].first;
int rand = (int) Math.random() * (int) Math.sqrt(num);
for (int j = 0; j < rand && n.next != null; j++)
n = n.next;
toRet[i] = n.word;
}
return toRet;
}
class Bucket {
private Node first;
public boolean get(String in) { // return key true if key exists
Node next = first;
while (next != null) {
if (next.word.equals(in)) {
return true;
}
next = next.next;
}
return false;
}
public void put(String key) {
for (Node curr = first; curr != null; curr = curr.next) {
if (key.equals(curr.word)) {
return; // search hit: return
}
}
first = new Node(key, first); // search miss: add new node
}
class Node {
String word;
Node next;
public Node(String key, Node next) {
this.word = key;
this.next = next;
}
}
}
My problem is that I am calling Hash() to decide which bucket to put it in and do the whole process. Instead of this I want to use an Instance of my linkedbag class to look through my word.txt file and return output if the input matches or not..

Interesting system.exit() behaviour and break behaviour - what is happening?

I am trying to implement BFS as follows:
package search;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import common.MyNode;
import common.Parser;
public class BFS extends Search {
Queue<MyNode> frontier;
Queue<MyNode> visited;
ArrayList<MyNode> route;
Parser p;
boolean found = false;
private int toWriteInt = 0;
private double distance;
public BFS(MyNode startNode, MyNode goalNode, Parser p) {
super(startNode, goalNode, p);
frontier = new LinkedList<MyNode>();
visited = new LinkedList<MyNode>();
this.p = p;
route = new ArrayList<MyNode>();
}
public void Search() {
visited.add(this.getStartNode());
if (isGoal(this.getStartNode())) {
System.out.println("goal found at start");
goalFound();
}
ArrayList<MyNode> successors = this.getStartNode().getSuccessors();
for (int i = 0; i < successors.size(); i++) {
successors.get(i).setParent(this.getStartNode());
if (!(visited.contains(successors.get(i)))
&& !(frontier.contains(successors.get(i)))) {
if (isGoal(successors.get(i))) {
visited.add(successors.get(i));
System.out.println("goal found at start successor");
goalFound();
break;
} else {
frontier.add(successors.get(i));
}
}
}
while (!frontier.isEmpty()) {
MyNode current = frontier.poll();
ArrayList<MyNode> currentSuccessors = current.getSuccessors();
visited.add(current);
for (int i = 0; i < currentSuccessors.size(); i++) {
if (!(visited.contains(currentSuccessors.get(i)))
&& !(frontier.contains(currentSuccessors.get(i)))) {
currentSuccessors.get(i).setParent(current);
if (isGoal(currentSuccessors.get(i))) {
visited.add(currentSuccessors.get(i));
System.out.println("goal found in loop");
goalFound();
break;
} else {
frontier.add(currentSuccessors.get(i));
}
}
}
}
}
private boolean isGoal(MyNode toCheck) {
boolean goal = false;
if (toCheck.equals(this.getGoalNode())) {
goal = true;
}
return goal;
}
private void goalFound() {
System.out.println("goal found with " + visited.size());
printRoute();
System.exit(0);
}
This all works find. The print route method is as follows:
public void printRoute() {
MyNode i = this.getGoalNode();
while (i.getParent() != null) {
System.out.println(i.getId());
distance = distance
+ Search.distFrom(i.getLat(), i.getLon(), i.getParent()
.getLat(), i.getParent().getLon());
i = i.getParent();
}
System.out.println(this.startNode.getId());
System.out.println("completed routing");
System.out.println("path length of " + distance + "km");
}
}
Once again, this works fine and prints the correct route. However, If i remove the system.exit(0) the code does not work and the while loop of printRoute() will simply print forever, printing out the same two nodes twice (i and i.getParent()). I find this really strange, as the method must have completed to hit my system.exit.
Can anyone tell me why this is? It's very problematic to have to call system.exit as i would like to embed BFS in my code and it means I cannot.
thanks,
MJB
edit: It was suggested below that returning would fix my issue. Below is A* search that exhibits the same behaviour and only works if I System.Exit even though I return:
package search;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
import common.MyNode;
import common.Parser;
public class AStar extends Search {
private ArrayList<MyNode> openList; // This is basically our frontier
private ArrayList<MyNode> closedList;// This is the visited set.
public AStar(MyNode startNode, MyNode goalNode, Parser p) {
super(startNode, goalNode, p);
openList = new ArrayList<MyNode>();
closedList = new ArrayList<MyNode>();
}
public Stack<MyNode> Search() {
Stack<MyNode> toReturn = new Stack<MyNode>();
openList.add(this.getStartNode());
while (!openList.isEmpty()) {
Collections.sort(openList);
MyNode q = openList.remove(0);
openList.remove(q);
// generate q's 8 successors and set their parents to q
// if successor is the goal, stop the search
if (isGoal(q)) {
System.out.println("search completed- goal found");
MyNode i = this.getGoalNode();
while (i.getParent() != null) {
toReturn.add(i);
i = q.getParent();
}
return toReturn;
}
closedList.add(q);
ArrayList<MyNode> successors = q.getSuccessors();
for (MyNode node : successors) {
if (closedList.contains(node)) {
continue;
}
node.setParent(q);
/*
* successor.g = q.g + distance between successor and q
* successor.h = distance from goal to successor
* successor.f=successor.g + successor.h
*/
double g = q.getG()
+ Search.distFrom(q.getLat(), q.getLon(),
node.getLat(), node.getLon());
double h = Search.distFrom(this.getGoalNode().getLat(), this
.getGoalNode().getLon(), q.getLat(), q.getLon());
node.setG(g);
node.setH(h);
node.setF(g + h);
// if a node with the same position as successor is in the OPEN
// list
// has a lower f than successor, skip this successor
int openIndex = openList.indexOf(node);
int closedIndex = closedList.indexOf(node);
if (openIndex > -1) {
if (openList.get(openIndex).compareTo(node) == -1)
continue;
}
// if a node with the same position as successor is in the
// CLOSED list
// which has a lower f than successor, skip this successor
if (closedIndex > -1) {
if (closedList.get(closedIndex).compareTo(node) == -1)
continue;
}
if (openIndex > -1)
openList.remove(openIndex);
Collections.sort(openList);
if (closedIndex > -1)
closedList.remove(closedIndex);
openList.add(node);
Collections.sort(openList);
}
closedList.add(0, q);
}
return toReturn;
}
edit: as requested, main method:
public static void main(String[] args) {
AStar search = new AStar(parse.getAllNodes().get(new Long("21295291")),
parse.getAllNodes().get(new Long("319561075")), parse);
search.Search();
}
These nodes have the following attributes:
node [id=21295291, lat=52.4737031, lon=-1.8747258]
node [id=119126329, lat=52.4701337, lon=-1.8716235]
Code
goalFound();
break;
breaks inner loop (for) but you have also outer while loop. You can use 'return;' here instead of 'break' (or labeled break).

Categories

Resources