Home arrow static arrow Java Programming [Archive] - Reversing LinkedList elements
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Reversing LinkedList elements
This topic has 142 replies on 10 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next »

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:09 PM (reply 60 of 142)



 
Hi,

What does it sound like? A single linked list only have one link from a node. A double linked list has two links from each node.
So, if we know that a single linked list has links to the next item.. What could then the other link point to in a double linked list? Hmmm.. yes, the previous item.

/Kaj
 

Posts:37,103
Registered: 3/30/99
Re: Reversing LinkedList elements  
Aug 8, 2004 12:12 PM (reply 61 of 142)



 
http://forum.java.sun.com/thread.jsp?forum=31&thread=230533

Dammit!

You are NOT the only person here who wants help! Your questions are NOT more important than anybody else's. Kajbj has NOT forgotten about you. He will address your questions if/when he is ready. Following somebody around saying, "Come back to MY question" is extremely rude.

You are NOT the only person here asking for help and neither kajbj nor anybody else is your personal answer machine. If you're that lost, you should pay for a private tutor. Seriously. You need way more help than these forums can supply effectively.
 

Posts:8,813
Registered: 10/4/00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:16 PM (reply 62 of 142)



 
I think it's terminal...
http://www.r-cis.netfirms.com/info.htm
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:22 PM (reply 63 of 142)



 
I think it's terminal...
http://www.r-cis.netfirms.com/info.htm

Nice, I like the comment "can strike all people of all nationalities". Are there diseases that only affect some nationalities?

/Kaj
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 12:37 PM (reply 64 of 142)



 
Just a reminder... 'cause i am still waiting

Hi can anyone tell me why do I get "OutOfMemory"
exception if I comment out

 first.next = null 
below?

Is it that it will go on calling first.next even if it
reaches null?

 Link previous = first; Link current  = first.next;   first.next = null;  while(current != null) { System.out.println("previous = " + previous + " current = "+ current);  Link next = current.next;  current.next = previous; //last will be FIRST previous     = current;//First will be LAST  current      = next; //for terminating LOOP }  first = previous; }
 

Posts:8,813
Registered: 10/4/00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:41 PM (reply 65 of 142)



 
Nice, I like the comment "can strike all people of all
nationalities". Are there diseases that only affect
some nationalities?

/Kaj

I'm not sure if it has been recognized as a disease yet, but millions of Americans seem to be afflicted with some kind of disorder where they spend millions and millions of dollars deciding who is the lesser of two morons.
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:42 PM (reply 66 of 142)



 
Just a reminder... 'cause i am still waiting

Hi can anyone tell me why do I get "OutOfMemory"
exception if I comment out

 first.next = null 
below?

Is it that it will go on calling first.next even if it
reaches null?

Link previous = first;Link current  = first.next;  first.next = null; while(current != null) {System.out.println("previous = " + previous + "current = "+ current); Link next = current.next; current.next = previous; //last will be FIRSTprevious     = current;//First will be LAST current      = next; //for terminating LOOP} first = previous;}

You are causing an endless loop. Hint, check what happens the first iteration, when previous = first.

/Kaj

 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:44 PM (reply 67 of 142)



 
I'm not sure if it has been recognized as a disease
yet, but millions of Americans seem to be afflicted
with some kind of disorder where they spend millions
and millions of dollars deciding who is the lesser of
two morons.

LOL. So, there are americans that have some self distance :) Or aren't you american?
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:47 PM (reply 68 of 142)



 
But the endless loop is in the printing, caused by a cycle in the list.

/Kaj
 

Posts:8,813
Registered: 10/4/00
Re: Reversing LinkedList elements  
Aug 8, 2004 12:55 PM (reply 69 of 142)



 
LOL. So, there are americans that have some self
distance :) Or aren't you american?

I'm American. Sadly though, I seem to be afflicted with chronic cynicism. It's a rare disorder over here.

A lot of Americans suffer from recurring bouts of enlightened self interest or persistent self-righteousness. They seem to be immune to any compulsion to examine an issue objectively.
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 12:57 PM (reply 70 of 142)



 
But the endless loop is in the printing, caused by a
cycle in the list.

could you please elaborate on that?

first = previous; // this is make the first node of the list point to the previous that is the Last element

first.next = null; this prevents value of 'first' from changing?

 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 1:01 PM (reply 71 of 142)



 
Hi,

Alter:

public String toString() {        StringBuffer buffer = new StringBuffer();        Link l = first;        while (l != null) {            buffer.append(l + " ");            l = l.next;        }        return buffer.toString();    }


So it says:
public String toString() {        StringBuffer buffer = new StringBuffer();        Link l = first;        while (l != null) {            System.out.println(l);            buffer.append(l + " ");            l = l.next;        }        return buffer.toString();    }


And run the program... that will display the cycle.
Remember that System.out and the debugger is your best friend.

/Kaj
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 1:07 PM (reply 72 of 142)



 
oh so first is like an engine of the train right?

having x no. of elements attached to it?
 

Posts:37,103
Registered: 3/30/99
Re: Reversing LinkedList elements  
Aug 8, 2004 1:10 PM (reply 73 of 142)



 
kajbj, your patience truly amazes me.
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 1:17 PM (reply 74 of 142)



 
kajbj, your patience truly amazes me.

jverd I used to be a java teacher for a while. And I can say that after a couple of months of questions like: What does missing ; mean. I got enough of that.
Now I can decide what I want to reply to :)

/Kaj
 
This topic has 142 replies on 10 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next »