• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reversing the elements in the list  RSS feed

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the following code to reverse the elements of the list but i only print the last element each time
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your question?
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this code i have five elements list and instead of printing the elements from the end to the begining, i prints only the fifth element five times.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:in this code i have five elements list and instead of printing the elements from the end to the begining, i prints only the fifth element five times.

Well, if this is a proper List (java.util.List), easiest is to simply get its ListIterator, and bang through its elements using hasPrevious()/previous().

Winston
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, it isnot a simple list but it is linked list with dynamic adding and here is the full code
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:no, it isnot a simple list but it is linked list with dynamic adding...

Well then my first question would be: Why not? There seems very little point in creating a list that doesn't implement List.

And since it only seems to incorporate a forward pointer, what you're trying to do runs against the underlying structure.

My advice: bang all the elements into an array (or, probably even better, an ArrayList), and read that in reverse order.

Winston
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is an exercise on this kind of list and i want to solve it in this way but i can't find what is wrong
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:it is an exercise on this kind of list and i want to solve it in this way...

In which case, your approach is flawed. Forward-only linked lists are designed to be read forwards, NOT backwards, so you're trying to force a square peg into a round hole.

It is however, extremely simple to add a new Node at the front of the list. Have a think how that might help you.

Winston
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally i found a way to reverse it using the nodes, here is the code
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:finally i found a way to reverse it using the nodes...

Well done.

However, you really are making life difficult for yourself:
1. If you added an insertAtFront() method (sometimes called push()), you could do this an awful lot more easily. And in O(n) time.
2. If you arrange your ListNodes in a ring (ie, where lastNode.nextNode == firstNode), you may also find a few things easier.
3. (optional) You might want to think about a size field. What you have is a very "pure" structure, but a method like size() will take O(n) time to execute.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!