public String recursiveToString()
{
DoubleLinkedListNode<T> current = first;
String list = "";
if(current == null)
{
return "";
}
else
{
list += current.info + ", ";
current = current.next;
return list + recursiveToString();
}
}
It is supposed to print out the list when I run the code but it just crashes every time it runs. This is everything that I've tried to do so far.
First off you'll want to pass the current position in the list back into the function each time, so change your signature to this:
public String recursiveToString(DoubleLinkedListNode<T> current)
Then alter your code a bit so it uses that and continues to pass it down, the list variable is also unnecessary, the recursion will take care of concatenating everything:
public String recursiveToString(DoubleLinkedListNode<T> current)
{
if(current == null)
{
return "";
}
else
{
return current.info + " " + recursiveToString(current.next);
}
}
If you would like to do it without changing the signature of the function, just rename the function I have above and create the definition for your parameterless function like so:
public String recursiveToString()
{
return aboveFunction(first);
}
Related
So, I have created a class that generates a generic doubly linked list. The problem is that the tester file keeps saying that I have done something incorrectly. It looks like it starts having problems with the deleting nodes portion of the test.
This is the deletion portion of the tester.
(The original numbers are 0,1,2,3,4,5,6,7,8,9 and the end result should be 1,3,4,5,6,7,8)
public static boolean deleteTest(GenDoubleLinkedList<Integer> intList)
{
printDecorated("Removing Test\nRemoving first item, third item, and last item");
intList.resetCurrent();
intList.deleteCurrent();
intList.goToNext();
intList.deleteCurrent();
while(intList.moreToIterate())
{
intList.goToNext();
}
intList.deleteCurrent();
intList.print();
return valuesMatch(intList,TEST_VALS_2);
}
These are my deleteCurrent, moreToIterate, resetCurrent and goToNext methods.
public void deleteCurrent() {
if(current != null && prev != null) {
System.out.println("DELETING " + current.data);
current = current.nextLink;
prev.nextLink = current;
next.prevLink = current;
}
else if(current != null && prev == null) {
System.out.println("DELETING " + current.data);
head = head.nextLink;
current = current.nextLink;
}
}
public boolean moreToIterate() {
//System.out.println(current.nextLink != null);
if(current.nextLink != null)
return true;
return false;
}
public void resetCurrent() {
current = head;
prev = null;
next = current.nextLink;
}
public void goToNext() {
//System.out.println(current.data);
if(current != null) {
current = current.nextLink;
prev = current.prevLink;
next = current.nextLink;
}
}
(I have the System.out.println()'s to troubleshoot.)
Here is what the console is outputting.
From my system out it looks like it should be deleting the correct nodes but when the test runs it obviously did not delete the correct ones. I am at a standstill and cannot figure out the issue.
If I need to I can give the entire tester file along with any of my code. Any help is appreciated.
current = current.nextLink should be changed to current = prev in your delete routine and current = current.nextLink should be removed from your "delete head" portion of your delete routine.
Please next time paste your cade it code tags so we can copy paste it to refer to it and also the line numbers...
When my program comes to this method it never seems to update the target value. If I input "dave" it will remain "dave" no matter how many calls to the method are made.
public Person lookup(String name){
if(firstPerson == null){
return null;
}
Person target = null;
for (target = firstPerson; target != null; target = target.nextPerson){
if(name.equals(target.name)){
return target;
}
else {
return null;
}
}
return target; // replace this line
}
If I add a friend via this addFriend method firstFriend will end up printing whatever the last added name was. If the inputted named were rob bill and travis
The output would be travis travis travis.
public void addFriend(Person friend){
firstFriend = new Friend(friend, firstFriend);
return; // replace this line
public String friendString(){
String friendList = "";
if(firstFriend == null){
return null;
}
for(Friend pointer = firstFriend; pointer != null; pointer = pointer.nextFriend){
friendList = friendList + firstFriend.who.name + " ";
}
return friendList.trim(); // replace this line
}
You always return in the first iteration of the loop. If the person is found it's returned (the if branch), and if it isn't, null is returned (the else branch). Instead, you should keep iterating until you find the correct person or exhaust the list. The first condition, BTW, is a subset of the loop (if firstPerson is null target will just become null immediately), and can (should!) also be removed:
public Person lookup(String name){
Person target = null;
for (target = firstPerson; target != null; target = target.nextPerson) {
if (name.equals(target.name)) {
return target;
}
}
return target; // Or null explicitly - matter of taste.
}
if(name.equals(target.name)){
return target;
}
else {
return null;
}
The else part needs to go away. The effect of this code is that it only checks the first value and if it is not the value that you want to look up it is coming out straight away.
Change
return target; // replace this line
to return null;
and remove the else part mentioned above
I need to implement a toString() recursive method for a linked list Queue. I know my toString method worked fine on a linked list implementation I did last week, so something is wrong with how I'm handling the Queue aspect of it.
toString method for my QueueList:
public String toString()
{
if (front.info == null)
{
System.out.println("Error, queue is empty");
return "";
}
if (front.link == null) //base case: if this is last element in stack
{
return (" \"" + front.info + "\" , ");
}
else //normal recursive function
{
return (" \"" + front.info + "\" , " + front.link.toString());
}
}
and my constructors and such for QueueList:
public class QueueNode
{
E info;
QueueNode link;
}
private QueueNode front;//first element to be placed into queue
private QueueNode rear;//last element to be placed into queue
private int NoE;//counter for number of elements in queue
public QueueList()
{
front = null;
rear = null;
NoE = 0;
}
I tried to see what was going on in it using this test:
public boolean test() {
QueueList<String> q = new QueueList<String>();
q.enqueue("The Godfather");
q.enqueue("Casino");
q.enqueue("Goodfellas");
String r = q.toString();
q.PrettyPrint();
with the output
IN -> [ "The Godfather" , QueueList$QueueNode#a3901c6] -> OUT.
I realize this is because I'm telling saying front.link.toString() in the recursive part of the toString method, but even if I change it to front.link.info.toString(), my output is
IN -> [ "The Godfather" , Casino] -> OUT.
It may be possibly something to do with with my enqueue and dequeue methods then, which are as follows:
public void enqueue(E element)
{
QueueNode newNode = new QueueNode();//creates new Node to hold element
newNode.info = element;//set info of new Node to element
newNode.link = null;//make link null since it's at back of list
if (rear == null)//checks if queue is empty
{
front = newNode;
}
else
{
rear.link = newNode;//sets second to last node's link to newNode
}
rear = newNode;//makes newNode the new last link
NoE++;//increase counter
}
public E dequeue() throws InvalidOperationException
{
if (front == null)//sanitize code
{
throw new InvalidOperationException("There is nothing in the queue.");
}
E element = front.info;//creates an element file that takes the info in front of queue
front = front.link;//makes second-to-front element new front
if (front == null)//if this emptied the queue, make sure rear is also empty
{
rear = null;
}
NoE--;//reduce counter
return element;
}
Please help me out if you can. Thanks.
There is absolutely no need to make toString recursive, and in fact it is incorrect to do so. Your data structure is not recursive (i.e. a tree) but linear.
If your list contained, say, 1 million items, you would quickly run out of stack space (StackOverflow, literally).
Use a loop instead.
EDIT: If you are required do to this recursively, then the issue is that the recursive method must be QueueNode#toStringRecursive(), not Queue#toString(). The method Queue#toString() allocates a buffer and provides it to a special toStringRecursive() method on QueueNode which does the recursion. QueueNode#toString() must be responsible only for its own node contents.
Method Queue#toString()
public String toString()
{
StringBuilder buf = new StringBuilder();
if (front == null)
// queue is empty
else
front.toStringRecursive(buf);
return buf.toString();
}
Method QueueNode#toStringRecursive()
public void toStringRecursive(StringBuilder buf)
{
buf.append(this.toString());
if (this.link != null)
this.toStringRecursive(buf);
}
where QueueNode.toString() is responsible only for stringifying one node (itself).
Note that this is one way to do it. It would be possible to write this as a recursive method on Queue as well, but it could not be called toString(). Queue#toString() would set up the initial conditions and then invoke the recursion.
I am new to java and I want to create a very simple "word completion " program. I will be reading in a dictionary file and recursively adding the words into a Node array (size 26). I believe I have managed to do this successfully but I am not sure how to go through and print the matches. For the sake of testing, I am simply inserting 2 words at the moment by calling the function. Once everything is working, I will add the method to read the file in and remove junk from the word.
For example: If the words "test" and "tester" are inside the tree and the user enters "tes", it should display "test" and "tester".
If somebody could please tell me how to go through and print the matches (if any), I would really appreciate it. Full code is below.
Thank you
What you implemented is called "trie". You might want to look at the existing implementations.
What you used to store child nodes is called a hash table and you might want to use a standard implementations and avoid implementing it yourself unless you have very-very specific reasons to do that. Your implementation has some limitations (character range, for example).
I think, your code has a bug in method has:
...
else if (letter[val].flag==true || word.length()==1) {
return true;
}
If that method is intended to return true if there are strings starting with word then it shouldn't check flag. If it must return true if there is an exact match only, it shouldn't check word.length().
And, finally, addressing your question: not the optimal, but the simplest solution would be to make a method, which takes a string and returns a node matching that string and a method that composes all the words from a node. Something like this (not tested):
class Tree {
...
public List<String> matches(CharSequence prefix) {
List<String> result = new ArrayList<>();
if(r != null) {
Node n = r._match(prefix, 0);
if(n != null) {
StringBuilder p = new StringBuilder();
p.append(prefix);
n._addWords(p, result);
}
}
return result;
}
}
class Node {
...
protected Node _match(CharSequence prefix, int index) {
assert index <= prefix.length();
if(index == prefix.length()) {
return this;
}
int val = prefix.charAt(index) - 'a';
assert val >= 0 && val < letter.length;
if (letter[val] != null) {
return letter[val].match(prefix, index+1);
}
return null;
}
protected void _addWords(StringBuilder prefix, List<String> result) {
if(this.flag) {
result.add(prefix.toString());
}
for(int i = 0; i<letter.length; i++) {
if(letter[i] != null) {
prefix.append((char)(i + 'a'));
letter[i]._addWords(prefix, result);
prefix.delete(prefix.length() - 1, prefix.length());
}
}
}
}
Maybe a longshot here, but why don't you try regexes here? As far as i understand you want to match words to a list of words:
List<String> getMatches(List<String> list, String regex) {
Pattern p = Pattern.compile(regex);
ArrayList<String> matches = new ArrayList<String>();
for (String s:list) {
if (p.matcher(s).matches()) {
matches.add(s);
}
}
return matches
}
Ok so say I have a function that looks for a specific word in a custom LinkedList class:
public LinkedList find(String word) {
if (this.word.equals(word))
return this;
if (next==null)
return null;
if (next.find(word)==next)
return next;
return null;
}
This code works fine, however it returns the FIRST found object that matches the criteria. What if I wanted to return the LAST object found that matches the paramater? I'm having a hard time figuring this out. Keep in mind I want to use recursion.
EDIT: What would be wrong with this code:
public LinkedList findLast(String word) {
LinkedList temp=new LinkedList(word, null);
if (next==null && next.word.equals(word))
return next;
if (next==null && !next.word.equals(word))
temp=next.findLast(word);
return temp;
}
Well, think of it this way: you need to recurse right to the end of the list, and then let the return value bubble up.
So the start of your method should either be a recursive call to look further down the list, or noting that we're at the end of the list - which is equivalent to the "further" result being null.
Now when you're returning, there are three options:
You've already found a match later than the current point - so return that reference
You've not found a match (so the return value of the recursive call was null) and:
The current point's word matches - so return the current point
The current point doesn't match - so return null
Hopefully that should be enough to get you to an implementation - if not, please ask more questions. I'd rather not give a full implementation when this is presumably homework.
Store a reference to the latest one found and keep on calling itself until it returns null -- then return the latest-reference.
Note, for clarification: you're going to have to iterate through your entire linked-list (unless you have a doubly-linked-list) to achieve this -- store a reference every time you find a match (but just overwrite the same reference each time) -- then return whatever the reference holds once you reach the end of this list.
public class LinkedList {
private static int uniqueIdCounter = 0;
private final String word;
private int uniqueId;
private LinkedList next = null;
public LinkedList( String word ) {
this.word = word;
this.uniqueId = uniqueIdCounter++;
}
#Override
public String toString() {
return this.word + "(" + this.uniqueId + ")";
}
public void setNext( LinkedList next ) {
this.next = next;
}
public LinkedList find( String word ) {
return this.find( word, null );
}
public LinkedList find( String word, LinkedList result ) {
if( this.word.equals( word ) ) {
result = this;
}
if( this.next != null ) {
result = this.next.find(word, result);
}
return result;
}
public static void main(String[] args) {
LinkedList head = new LinkedList( "A");
System.out.println( "Head is: " + head );
LinkedList B = new LinkedList( "B" );
head.setNext( B );
System.out.println( "B is: " + B );
LinkedList A2 = new LinkedList( "A" );
B.setNext( A2 );
System.out.println( "A2 is: " + A2 );
LinkedList last = head.find( "A" );
System.out.println( "Last is: " + last );
}
}
And here's the output:
Head is: A(0)
B is: B(1)
A2 is: A(2)
Last is: A(2)
Every straight recursive function has two places for some useful actions: before further method call and after:
function(n){
doBefore(n);
function(n+1)
doAfter(n)
}
doBefore() is executed "on the way forward", doAfter() is executed "on the way back". Now your algorithm checks word equality on the way forward. You have to modify your algorithm so that this check is performed on the way back.
public LinkedList find(String word, LinkedList result) {
if (this.word.equals(word))
result = this;
if (next != null )
return next.find(word, result)
return result;
Two-liner:
public LinkedList find(String word, LinkedList result) {
result = this.word.equals(word) ? this : result;
return next == null ? result : next.find(word, result);
#fprime: Ya, explanation: remember the result, replace it with later result, return when at the end.
Method with one argument:
public LinkedList find(String word){
result = this.word.equals(word) ? this : null;
if(next != null)
previous = next.find(word);
return (previous != null) ? previous : result
else
return result;
Just run it backwards from the tail.
public LinkedList find(String word) {
if (this.word.equals(word))
return this;
if (prev==null)
return null;
if (prev.find(word)==prev)
return prev;
return null;
}
To start with, you initial find(String word) does not work correctly.
Your first if statement is perfect. It is you success base case.
Your second if statement is also perfect. It is your failure base case.
Your third is where you go off the rails. You have handled all (in this case both) base cases, now all that is left is the recursive case. You don't need to check anything here. next.find(word) will return the correct answer, success or fail.
For findLast(String word), I can't add much to what Jon Skeet said. About the only advice I can add it to never have the a node check its neighbor. Each node should only ever check itself. You should see plenty of this.word.equals(word) but never next.word.equals(word).
public LinkedList find(String word) {
if(this.word.equals(word)) return this;
return next==null?null:next.find(word);
}
public LinkedList rfind(String word) {
if(next != null) {
LinkedList res = next.rfind(word);
if(res != null) return res;
}
return this.word.equals(word)?this:null;
}