Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with a part in a recursion LinkedList question

    Hello,

    Currently I am doing a LinkedList program using recursion. I have passed the tests for the other portions. However, I do not know how to start doing this problem inside the LinkedList question.

    public LinkedList insert(String string) {

    }


    The passed tests includes:

    getLetterCount()
    getLongestWord()
    getSentence()
    getReversedSentence()
    createLinkedList()
    contains()
    find()
    findLast()
    append()

    Can someone help me? Thanks.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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'

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    public class LinkedList {
    	// Get and Set methods are NOT necessary!
     
    	private LinkedList next; 	
    	private final String 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).
    	 */
    	public LinkedList(String word, LinkedList next) {
    		this.word = word;
    		this.next = next;
    	}
     
    	/**
    	 * Converts the entire linked list into a string representation.
    	 */
    	public String toString() {
    		if (next == null)
    			return word;// BASE CASE; no more recursion required
     
    		// Recursive case:
    		String restOfString = next.toString(); // Forward Recursion
    		return word + ";" + restOfString;
    	}
     
    	/**
    	 * Returns the number of entries in the linked list.
    	 * @return number of entries.
    	 */
    	public int getCount() {
    		if (next == null)
    			return 1; // BASE CASE; no more recursion required!
     
    		// Recursive case:
    		return 1 + 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
    	 */
    	public void append(String word) {
    		if(next==null)
    			next = new LinkedList(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
    	 */
    	public int getLetterCount() {
    		if(next==null)
    			return word.length();
    		else
    			return 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.
    	 */
    	public String getLongestWord() {
    		// recursive searches for the longest word
    		if(next==null)
    			return word;
    		else if(next.getLongestWord().length() > word.length())
    			return next.getLongestWord();
    		else
    			return 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.*/
    	public String getSentence() {
    		if(next==null)
    			return word + ".";
    		else
    			return 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. 
    	 */
    	public String 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. */
    	public static LinkedList createLinkedList(String[] words) {
    		LinkedList stuff = new LinkedList(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)
    	 */
    	public boolean contains(String word) {
    		if(this.word.equals(word))
    			return true;
    		else if(next==null)
    			return false;
    		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.
    	 */
    	public LinkedList find(String word) {
     
    		if(word.equals(this.word))
    			return this;
    		else if(next==null)
    			return null;
    		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.
    	 */
    	public LinkedList findLast(String word) {
    		if(next==null){
    			if(this.word.equals(word))
    				return this;
    			else
    				return null;}
     
    		LinkedList stuff = next.findLast(word);
    		if(stuff != null)
    			return stuff;
    		if(this.word.equals(word))
    			stuff=this;
    		return stuff;
     
    	}
     
    	public LinkedList insert(String string) {
     
    	}
    }
    Last edited by Freaky Chris; November 2nd, 2011 at 05:11 AM. Reason: Added highlighting.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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.

    Regards,
    Chris

Similar Threads

  1. A Part Question
    By Aksand in forum Java Theory & Questions
    Replies: 5
    Last Post: April 13th, 2011, 11:12 AM
  2. question about recursion..
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2011, 09:51 AM
  3. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 06:10 AM
  4. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  5. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM