简体   繁体   中英

Java Generic Doubly Linked List Swap

http://users.cis.fiu.edu/~weiss/dsaajava3/code/MyLinkedList.java

Above link shows the code that I want to modify in order to have swap method from the doubly linked list. I apologize in advance using URL in place due being 300 line of code pasted here is not very readable.


For the output, I'd like to just have a single list output, for example :

[ 28 27 **21** 25 24 23 22 **26** 20 0 1 2 3 4 5 6 7 8 ]  //(2, 7) swap


Below is my attempt (part of the code to include/modify to original) :

swap method:

public AnyType swap( int idx, int idx2)
{
    return swap( getNode( idx ), getNode ( idx2 ) );
}

private AnyType swap( Node<AnyType> p, Node<AnyType> p2)
{
    Node<AnyType> temp = p;   //realize it doesn't work due to linking
    p = p2;
    p2 = temp;

    return p.data;
}

main:

class TestLinkedList
{
  public static void main( String [ ] args )
  {
    MyLinkedList<Integer> lst = new MyLinkedList<>( );

    for( int i = 0; i < 10; i++ )
            lst.add( i );
    for( int i = 20; i < 30; i++ )
            lst.add( 0, i );

    lst.remove( 0 );
    lst.remove( lst.size( ) - 1 );

    lst.swap(2, 7);
    System.out.println("swap: " + lst);
  }
}

output

swap: [ 28 27 26 25 24 23 22 21 20 0 1 2 3 4 5 6 7 8 ]
21


I realize that my approach to solution is not the use of linked list. I've been analyzing the code that I am to modify; however am having hard time with the pointers and the doubly linked list to understand the coding part (not the concept). Please explain how I should approach to solution. Thanks.


EDIT

/* Swaps idx and idx2 nodes
 * @param idx the index of the object
 * @param idx2 the index of the object
 */
public void swap( int idx, int idx2)
{
    swap( getNode( idx  ), getNode ( idx2 ) );
            if( idx < 0 || idx >= size( ) || idx2 < 0 || idx2 >= size( )){
        throw new IndexOutOfBoundsException( "swap first index: " + idx + ", second index: " + idx2 );
    }
}

/**
 * Swaps node and data at p and p2
 * @param p the index of the object
 * @param p2 the index of the object
 */
public void swap( Node<AnyType> p, Node<AnyType> p2)
{

    Node<AnyType> temp = p;
    p = p2;
    p2 = temp;

   temp = p.next.prev;
    p.next.prev = p2.next.prev;
    p2.next.prev = temp;        


    AnyType dataTemp = p.data;
    p.data = p2. data;
    p2.data = dataTemp;
}

output

[ 29 28 27 26 25 24 23 22 21 20 0 1 2 3 4 5 6 7 8 9 ]
swap: [ 29 28 22 26 25 24 23 27 21 20 0 1 2 3 4 5 6 7 8 9 ]

It is now working. Did I do it right? However I get compile error with. I assume it has to do with public void swap( Node<AnyType> p, Node<AnyType> p2) method which has warning: exporting non-public type through public API . What can I do here to fix it?

Let's say your input is:

[ 1 2 3 4 5 6 7 8 9 ]

and you want to swap 3 and 7:

[ 1 2 >7< 4 5 6 >3< 8 9 ]

This means you need to update all these values:

2.next = 7
7.prev = 2
7.next = 4
4.prev = 7
6.next = 3
3.prev = 6
3.next = 8
8.prev = 3

That is a total of 8 assignments, not counting assignment to temporary variables.


What if you wanted to swap 5 and 6 instead:

[ 1 2 3 4 >6< >5< 7 8 9 ]

4.next = 6
6.prev = 4
6.next = 5
5.prev = 6
5.next = 7
7.prev = 5

Hmmm... That was only 6 assignments.


What if you wanted to swap first and last element:

[ >9< 2 3 4 5 6 7 8 >1< ]

You didn't share this part, but I'll assume you have both a head and a tail reference, so:

head   = 9
9.prev = null
9.next = 2
2.prev = 9
8.next = 1
1.prev = 8
1.next = null
tail   = 1

Whoa, that was different.


Now you just need to code all that, taking care to handle all combinations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM