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 10:07 AM (reply 30 of 142)



 
Hi,

I inserted sys.print but i only get hashcode i.e.
memory address

That is because the things you are printing doesn't have a 'nice' implementation of toString().
Write one your self... Here is a program which you can add System.out to:

class Test {    public static void main(String[] args) throws Exception {        LinkedList list = new LinkedList();        list.add("1");        list.add("2");        list.add("3");        list.add("4");        System.out.println(list);        list.reverse();        System.out.println(list);    } } class LinkedList {     Link first;    Link last;     public void add(String value) {        Link l = new Link(value);        if (last == null) {            last = l;            first = last;        } else {            last.next = l;            last = l;        }    }     public void reverse() {         if (first == null) return;         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;            previous = current;            current = next;         }         first = previous;      }     public String toString() {        StringBuffer buffer = new StringBuffer();        Link l = first;        while (l != null) {            buffer.append(l + " ");            l = l.next;        }        return buffer.toString();    }     class Link {        Link next;        String value;        Link(String value) {            this.value = value;        }        public String toString() {            return value;        }    }}


anyother way?

Yes, see the code above, it has better implementations of toString();


do you understand the code in OP ?

Yes I do, but I think it's better if you learn how to get the information on by debugging, or adding System.out. Play with the code I posted.

/Kaj
 

Posts:5,965
Registered: 5/17/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:08 AM (reply 31 of 142)



 
can you tell me whats the
".next"
for?
is .next a field?

Yes, nodes in a singly linked list has, at the minimum, a value and a reference to the next node in the list, like
public class Node {   // some value field   Node next; // ref. to next node}

Next is the name often used in the litterature. In a doubly linked list there's another pointer usually called prev holding a reference to the previous node.
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:09 AM (reply 32 of 142)



 
Is it? Can you post codes please?
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 10:10 AM (reply 33 of 142)



 
Is it? Can you post codes please?

Look at the code I posted. It's a complete example of a LinkedList

/Kaj

 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:10 AM (reply 34 of 142)



 
A.

What do you mean by a memory address. I thought java didn't let you access those?
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:12 AM (reply 35 of 142)



 
Is it? Can you post codes please?

Look at the code I posted. It's a complete example of
a LinkedList

/Kaj


I think you missed the irony in that statement, and the bazillion preceding posts.

Best,
/k1
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 10:12 AM (reply 36 of 142)



 
But Java can still print them for you. :)

/Kaj
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 10:13 AM (reply 37 of 142)



 
I think you missed the irony in that statement, and
the bazillion preceding posts.

I probably did
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 10:18 AM (reply 38 of 142)



 
I think you missed the irony in that statement, and
the bazillion preceding posts.

In that case I do also hope that your post in New Forum Suggestions about an Urgent forum was posted with a lot of irony.

/Kaj
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 10:26 AM (reply 39 of 142)



 
kajbj,

Let's play this.

You become mr.A's personal tutor.

After a week, we'll rediscuss.

/k1
 

Posts:18,384
Registered: 21.03.00
Re: Reversing LinkedList elements  
Aug 8, 2004 10:34 AM (reply 40 of 142)



 
You become mr.A's personal tutor.

After a week, we'll rediscuss.

Nope, I'm no ones personal tutor. I try to help everyone :)

/Kaj
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 11:09 AM (reply 41 of 142)



 
After brooding over your nice code i've made following comments are they correct?

[code]
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
}
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 11:16 AM (reply 42 of 142)



 
AHA I got IT DUDE LOL....

its like all like this

as it iterates through the List....

it will go on changing the pointer...

previous will become next link pointer

and next link pointer will become previous
 

Posts:826
Registered: 7/1/03
Re: Reversing LinkedList elements  
Aug 8, 2004 11:16 AM (reply 43 of 142)



 
Well Mr. A, it's been fun.

Good luck with your notional exam.

Bye!

/k1
 

Posts:357
Registered: 8/5/04
Re: Reversing LinkedList elements  
Aug 8, 2004 11:19 AM (reply 44 of 142)



 
Well Mr. A, it's been fun.
Good luck with your notional exam.

You are like an irritant You need to be diminished

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