Im working on Java homework and I've got myself a bit stuck.
I have a Class called Anagrams which has two pointers to a Node class to create a linked list. These Nodes hold String values, and a pointer to the next Node in the list.
The problem is that I need to write a method in the anagrams class called getAnagrams to go through the list and return an array of Strings. Each string must an angram in the list with it's anagrams.
For example if there was a list containing:
abc, bca, ret, etr, hyg
The method would return:
abc bca
ret etr
My Anagrams class looks like this:
The Node class is:
I can only use String and StringBuffer from the standard library. I'm not sure if any kind of sorting would help. I've been wracking my brains but can't think of a decent way to do it. If anyone has any ideas that may help then please let me know.
I have a Class called Anagrams which has two pointers to a Node class to create a linked list. These Nodes hold String values, and a pointer to the next Node in the list.
The problem is that I need to write a method in the anagrams class called getAnagrams to go through the list and return an array of Strings. Each string must an angram in the list with it's anagrams.
For example if there was a list containing:
abc, bca, ret, etr, hyg
The method would return:
abc bca
ret etr
My Anagrams class looks like this:
Code:
// Class used to find anagrams
//
public class Anagrams implements AnagramsInterface {
// This class needs to be implemented
private Node first; // the start of the list
private Node last; // last node in the list
private int count; // number of nodes in the list
public Anagrams() {
first = null;
last = null;
count = 0;
}
public void addWord(String s) {
Node n = new Node(s);
if (first == null) {
first = n;
count++;
if (last == null) {
last = first;
}
}
else {
last.setNext(n);
last = n;
count++;
}
}
public String[] getAnagrams () {
}
}
The Node class is:
Code:
// Class used to store individual nodes in a linked list
//
class Node {
private String value;
private Node next;
public Node(String s)
{
value = s;
next = null;
}
public Node(String s, Node n)
{
value = s;
next = n;
}
// Accessors and mutators for value and next
public String getValue() { return value; }
public void setValue(String s) { value = s; }
public Node getNext() { return next; }
public void setNext(Node n) { next = n; }
}
I can only use String and StringBuffer from the standard library. I'm not sure if any kind of sorting would help. I've been wracking my brains but can't think of a decent way to do it. If anyone has any ideas that may help then please let me know.