The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: Need help with a part in a recursion LinkedList question

One word: context. We don't know your code, what you've done, exactly where you are stuck, and what is confusing about this method in particular. Please see the link in my signature entitled 'getting help'

Re: Need help with a part in a recursion LinkedList question

Here's the code I have so far. I am stuck on the problem right at the end.

publicclassLinkedList{// Get and Set methods are NOT necessary!privateLinkedList next;privatefinalString word;/** Constructs this link.
* @param word ; a single word (never null).
* @param next ; the next item in the chain (null, if there are no more items).
*/publicLinkedList(String word, LinkedList next){this.word= word;this.next= next;}/**
* Converts the entire linked list into a string representation.
*/publicString toString(){if(next ==null)return word;// BASE CASE; no more recursion required// Recursive case:String restOfString = next.toString();// Forward Recursionreturn word +";"+ restOfString;}/**
* Returns the number of entries in the linked list.
* @return number of entries.
*/publicint getCount(){if(next ==null)return1;// BASE CASE; no more recursion required!// Recursive case:return1+ next.getCount();// Forward recursion}/** Creates a new LinkedList entry at the end of this linked list.
* Recursively finds the last entry then adds a new link to the end.
* @param word
*/publicvoid append(String word){if(next==null)
next =newLinkedList(word,null);else
next.append(word);}/**
* Recursively counts the total number of letters used.
*
* @return total number of letters in the words of the linked list
*/publicint getLetterCount(){if(next==null)return word.length();elsereturn word.length()+ next.getLetterCount();// returns the total number of letters. word1.length() +// word2.length()+...// "A" -> "CAT" -> null returns 1 + 3 = 4.}/**
* Recursively searches for and the returns the longest word.
* @return the longest word i.e. word.length() is maximal.
*/publicString getLongestWord(){// recursive searches for the longest wordif(next==null)return word;elseif(next.getLongestWord().length()> word.length())return next.getLongestWord();elsereturn word;}/** Converts linked list into a sentence (a single string representation).
* Each word pair is separated by a space.
* A period (".") is appended after the last word.
* The last link represents the last word in the sentence.*/publicString getSentence(){if(next==null)return word +".";elsereturn word +" "+ next.getSentence();}/**
* Converts linked list into a sentence (a single string representation).
* Each word pair is separated by a space. A period (".") is appended after
* the last word. The last link represents the first word in the sentence
* (and vice versa). The partialResult is the partial string constructed
* from earlier links. This partialResult is initially an empty string.
*/publicString getReversedSentence(String partialResult){if(next==null)
partialResult = word + partialResult +".";else
partialResult = next.getReversedSentence(" "+ word + partialResult);return partialResult;}/** Creates a linked list of words from an array of strings.
* Each string in the array is a word. */publicstaticLinkedList createLinkedList(String[] words){LinkedList stuff =newLinkedList(words[0],null);for(int i =1; i<words.length; i++)
stuff.append(words[i]);return stuff;// Hint: This is a wrapper method. You'll need to create your// own recursive method.// Yes this is possible _without_ loops!}/**
* Searches for the following word in the linked list. Hint: use .equals not ==
* to compare strings.
*
* @param word
* @return true if the linked list contains the word (case sensivitive)
*/publicboolean contains(String word){if(this.word.equals(word))returntrue;elseif(next==null)returnfalse;return next.contains(word);}/** Recursively searches for the given word in the linked list.
* If this link matches the given word then return this link.
* Otherwise search the next link.
* If no matching links are found return null.
* @param word the word to search for.
* @return The link that contains the search word.
*/publicLinkedList find(String word){if(word.equals(this.word))returnthis;elseif(next==null)returnnull;return next.find(word);}/**
* Returns the last most link that has the given word, or returns null if
* the word cannot be found.
* Hint: Would forward recursion be useful?
* @param word the word to search for.
* @return the last LinkedList object that represents the given word, or null if it is not found.
*/publicLinkedList findLast(String word){if(next==null){if(this.word.equals(word))returnthis;elsereturnnull;}LinkedList stuff = next.findLast(word);if(stuff !=null)return stuff;if(this.word.equals(word))
stuff=this;return stuff;}publicLinkedList insert(String string){}}

Re: Need help with a part in a recursion LinkedList question

Hi ZenithX,

Please use [highlight=java][/highlight] when posting code snippets to preservse formatting.

Also in regards to your problem, you have been very vague, we need to know what you already know about this problem and which bit of the logic you are getting stuck on. Saying you don't know how to implement insertion into a linked list isn't a valid problem for us to solve, as in effect we would just be completing your homework for us. Please have a read about linked lists, how they work and then most your understandings of the logic and your code attempt at the solution. Then we can prodive you with help regarding the problem at hand.