0
/**
     * 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+=this.word;
            return partialResult + ".";
        }
        else{
            partialResult=next.getReversedSentence(partialResult) + this.word;
            return partialResult;
            }
    }

Everything is working fine, except for the period (and the spaces, but Im not worrying about that yet). I cant get the period to be placed properly.

Here's the test it's failing:

public void testGetReversedSentence() {
        LinkedList tail = new LinkedList("not",null);
        LinkedList middle = new LinkedList("too",tail);
        LinkedList head = new LinkedList("tricky",middle);
        assertEquals("not.",tail.getReversedSentence(""));
        assertEquals("not too tricky.",head.getReversedSentence(""));

It comes up with not.too tricky instead of not too tricky.

Edit: Contrusctor

public LinkedList(String word, LinkedList next) {
        this.word = word;
        this.next = next;
    }

Any hints?

1
  • what does the constructor for your linked list class look like? Commented Nov 1, 2010 at 17:27

4 Answers 4

1

Well... there's no need to use 2 methods, (important) --> because this method has String partialResult as a parameter. (You can if you want to and if helper methods are allowed in the solution, but it's unnecessary.) In other words, try to find some way to incorporate the current word with partialResult. Another hint: there is a solution that is 3 lines long (and properly formatted).

Sign up to request clarification or add additional context in comments.

3 Comments

I STILL cannot figure this out..Ive been working on it for hours please give me clearer hints!
@fprime: Let's see... instead of putting + this.word at the very end of the partialReturn = ... statement, try putting it inside the parameter when you call getReversedSentence() and see what you get. You can change things from there to get spacing to work.
@fprime, have you figured it out yet?
1
 public String getReversedSentence(String partialResult) {
        if (next==null) {
            partialResult+=this.word;
            return partialResult + ".";
        }
        else{
            partialResult=next.getReversedSentence(partialResult) + this.word; // <---
            return partialResult;
            }
    }

The problem is the line marked <---. The recursive call will eventuallyt return with the . appended (since it will recurse until it gets to the end of hte list, then add a ., return that and then you append this.word.

Since this is homework, I won't give a solution.

3 Comments

Im still not figuring it out..what is my code currently doing thats erroneous?
See rsp's answer for a further hint
Paul is right, partialResult will always be an empty string during the recursion down. You need to change partialResult before calling getReversedSentence.
1

What is going wrong is that the "." is appended to the word that is deepest in recursion, which is the first word in the reversed sequence.

You want to find a way to add the "." to the end of the reversed sequence, maybe by splitting your method in 2 methods where the one that calls the other knows something about the result.

Comments

-1
//--------------------------linklist class------------------------------------------------

import java.util.Stack;

public class Linklist {
    Stack <Character> a = new Stack <Character>();
    Node front; 
    Node back;
    Node Temp;

    public void insert (char d){
        Node newNode = new Node (d);
    if (front == null){
        front = newNode;
        back = newNode;
    }


    else {
        Temp = front;
        while (Temp != null){

            back= Temp;
        Temp = Temp.next ;
        }

        back.next = newNode;
    }


    }





    public void display(){
        Node T=front;
        System.out.print(T.data);
        T=T.next;
        while(T!=front)
        {
            System.out.print(T.data);
            T=T.next;
            if (T == null ){
                break;
            }
        }
    }


    public void reverse(){
    Linklist bb = new Linklist ();


        Node temp = front;
        while(temp != null ){
            if (temp.data != '_'){
 a.push(temp.data);

            }


            else{
        //char x =  
while (!a.isEmpty()){
    char pop = a.pop();
bb.insert(pop);


            }
bb.insert('_');
            }
            temp = temp.next;
        }// while end

        bb.display();

    }


}
//-------------------------------------------------------------------------------------


//------------------ test class
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        queueLinklist a = new queueLinklist ();
a.insert('m');
a.insert('y');
a.insert('_');
a.insert('n');
a.insert('a');
a.insert('m');
a.insert('e');
a.insert('_');
a.insert('i');
a.insert('s');
a.insert('_');
a.insert('s');
a.insert('a');
a.insert('m');
a.insert('_');

a.display();
System.out.println(" ");
    a.reverse();
    a.reverse();
    }

}
// ---------- node class

public class Node {
char data;
Node next;

Node (){
    data= (Character) null ;
    next = null;
}

Node (char d){
    data = d;
    next = null;
}
}

1 Comment

hmm ... and this answers the question in that ..?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.