-
Swap items in two way linked list - Help!
Can anyone show me how I can swap two items in a two-way linked list, by
changing just the references not the data?
I've got an idea but I don't know if this is correct (first points at the
first of the two items to be swapped).
swap(ListIterator first) {
ListNode temp = first;
(first.previous).next = first.next;
first = first.next;
first.previous = temp.previous;
first.next = temp;
(first.next).previous = first;
}
first, next, previous are all ListNode objects.
-
Re: Swap items in two way linked list - Help!
That looks like it should work. Drawing pictures is the easiest way to
visualize it but I can't put my pictures here.
PC2
"LordByte" <thelordbyte@hotmail.com> wrote in message
news:3b015b64@news.devx.com...
> Can anyone show me how I can swap two items in a two-way linked list, by
> changing just the references not the data?
>
> I've got an idea but I don't know if this is correct (first points at the
> first of the two items to be swapped).
>
> swap(ListIterator first) {
> ListNode temp = first;
>
> (first.previous).next = first.next;
>
> first = first.next;
> first.previous = temp.previous;
>
> first.next = temp;
>
> (first.next).previous = first;
> }
>
> first, next, previous are all ListNode objects.
>
>
-
Re: Swap items in two way linked list - Help!
I've may have my pictue wrong, but it looks like you are missing two reference
changes. It seems you need to ensure that the object following the switch-a-roo
as well as the object placed first are pointing to the correct objects.
add
first.ne
"Paul Clapham" <pclapham@core-mark.com> wrote:
>That looks like it should work. Drawing pictures is the easiest way to
>visualize it but I can't put my pictures here.
>
>PC2
>
>"LordByte" <thelordbyte@hotmail.com> wrote in message
>news:3b015b64@news.devx.com...
>> Can anyone show me how I can swap two items in a two-way linked list,
by
>> changing just the references not the data?
>>
>> I've got an idea but I don't know if this is correct (first points at
the
>> first of the two items to be swapped).
>>
>> swap(ListIterator first) {
>> ListNode temp = first;
>>
>> (first.previous).next = first.next;
>>
>> first = first.next;
>> first.previous = temp.previous;
>>
>> (first.next).previous = temp;/* ensure that the object
following the switch is pointing
to the correct previous, which will
be temp */
temp.next = first.next; /* According to my picture temp's next
reference is still pointing to the
'first' object (so both next and
previous references are pointing to the
same! */
first.next = temp;
>>
>> (first.next).previous = first; //see you modify the prevous, but not
//the next.
>> }
>>
>> first, next, previous are all ListNode objects.
>>
>>
>
>
-
Re: Swap items in two way linked list - Help!
Your code will crash when the first or last element is swapped.
I did this once some time back for a PSP class. Special cases include:
Either element is the first in the list. (Hint: first.previous is
null; therefore (first.previous).next will raise a null object reference
exception.)
Either element is the last in the list.
"first" and the current node are adjacent.
"first" is the same node as the current node.
I agree that drawing out the possibilities will make it clear that
it's not as simple as it first appears. For the class, we had to sort
the double-linked list. I stuck it out and worked through it. When
I was later tutoring, I suggested the students create a new list, copying
the elements in order.
Have a nice day!
--dang
"LordByte" <thelordbyte@hotmail.com> wrote:
>Can anyone show me how I can swap two items in a two-way linked list, by
>changing just the references not the data?
>
>I've got an idea but I don't know if this is correct (first points at the
>first of the two items to be swapped).
>
>swap(ListIterator first) {
> ListNode temp = first;
>
> (first.previous).next = first.next;
>
> first = first.next;
> first.previous = temp.previous;
>
> first.next = temp;
>
> (first.next).previous = first;
>}
>
>first, next, previous are all ListNode objects.
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|