简体   繁体   English

Java通用双链表交换

[英]Java Generic Doubly Linked List Swap

http://users.cis.fiu.edu/~weiss/dsaajava3/code/MyLinkedList.java 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. 上面的链接显示了我想要修改的代码,以便具有双向链接列表中的swap方法。 I apologize in advance using URL in place due being 300 line of code pasted here is not very readable. 我预先使用URL表示歉意,因为此处粘贴的300行代码不太可读。


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 . 我假设它与public void swap( Node<AnyType> p, Node<AnyType> p2)方法有关,该方法具有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: 而您想交换3和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. 总共有8个分配,不包括对临时变量的分配。


What if you wanted to swap 5 and 6 instead: 如果您想交换5和6怎么办:

[ 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. 嗯...那只是6个作业。


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. 现在,您只需要编写所有代码,小心处理所有组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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