468,253 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

How to reverse lines using LinkedList?

Hi, I'm writing a program that reverses the lines of a String using the LinkedList structure, which includes an iterator. I have my code written out, but every time my program runs, it takes a very long time before it outputs the lines. I'm having difficulty proceeding to the next step. If anyone could point me to the right direction on what to do next, I would greatly appreciate it.

Also note: I'm unable to call the reverse() method from the Collections class.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.util.ListIterator;
  3. import java.io.*;
  4.  
  5. public class ReverseList
  6. {
  7.    public static void main (String[] args)
  8.    {
  9.  
  10.       LinkedList<String> phrase = new LinkedList<String>();
  11.         phrase.add("Four");
  12.         phrase.add("score");
  13.         phrase.add("and");
  14.         phrase.add("seven");
  15.         phrase.add("years");
  16.         phrase.add("ago");        
  17.         System.out.println("Phrase in normal order: " + phrase);
  18.  
  19.       reverse(phrase);
  20.  
  21.         System.out.println("Phrase in reverse order: " + phrase);
  22.     }
  23.  
  24.     public static LinkedList<String> reverse(LinkedList<String> a)
  25.    {
  26.       ListIterator<String> order = a.listIterator(a.size());
  27.       LinkedList<String> reversed = new LinkedList<String>();
  28.       while(order.hasPrevious())
  29.       {              
  30.          reversed.add(order.previous());
  31.       }
  32.       return reversed;
  33.    }
  34.  
Feb 12 '11 #1

✓ answered by Dheeraj Joshi

phrase is a variable inside the main method it is not a class member.
Your reverse function actually reverses the order. But you are not storing it anywhere after the return and you print phrase again (which is the original list)

Try this
Expand|Select|Wrap|Line Numbers
  1. phrase = reverse(phrase);
  2.  
Note: Once the reverse method returns the list your original list is lost since you are storing the reversed list in phrase list variable.

Regards
Dheeraj Joshi

2 2701
Dheeraj Joshi
1,123 Expert 1GB
phrase is a variable inside the main method it is not a class member.
Your reverse function actually reverses the order. But you are not storing it anywhere after the return and you print phrase again (which is the original list)

Try this
Expand|Select|Wrap|Line Numbers
  1. phrase = reverse(phrase);
  2.  
Note: Once the reverse method returns the list your original list is lost since you are storing the reversed list in phrase list variable.

Regards
Dheeraj Joshi
Feb 14 '11 #2
Ah! Thank you very much. :)
Feb 14 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by kaczmar2 | last post: by
2 posts views Thread by adam.timberlake | last post: by
3 posts views Thread by Robert | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.