【问题标题】:Concatenating Singly Linked Lists Into One List将单链表连接成一个列表
【发布时间】:2016-02-21 19:29:17
【问题描述】:

 包singlyLinkedList;

import net.datastructures.*;

// CREATE LIST 1
// CREATE LIST 2
// PRINT LISTS 1 & 2
// CONCATENATE LISTS 1 & 2 into LIST 3
// PRINT LIST 3
public class GameEntrySinglyLinkedList {

    public static SinglyLinkedList<String> concatenate (SinglyLinkedList<String>game1, SinglyLinkedList<String>game2) {
        // DECLARE METHOD LISTS
        SinglyLinkedList<String> result;
        SinglyLinkedList<String> temp;

        // TRY
        try {
            result = game1.clone();
            temp = game2.clone();
            /* DEBUGGING PURPOSES
            System.out.printf ("\n\n(DEBUG) Test Result: %s\n", result);
            System.out.printf ("\n\n(DEBUG) Test Temp: %s\n", temp);
            */
        }
        // CATCH CLONE EXCEPTION
        catch(CloneNotSupportedException n) {
            return null; 
        }
        // WHILE
        while(!temp.isEmpty()) {
            result.addFirst(temp.removeFirst());
            /* DEBUGGING PURPOSES
            System.out.printf ("\n\n(DEBUG) Test Result: %s\n", result);
            System.out.printf ("\n\n(DEBUG) Test Temp: %s\n", temp);
            */
        }

        return result;
    }

    /* *********** *
     * MAIN METHOD *
     * *********** */
    public static void main(String[] args) {
        // LOCAL VARIABLES
        SinglyLinkedList<String> game1 = new SinglyLinkedList<String> ();
        SinglyLinkedList<String> game2 = new SinglyLinkedList<String> ();
        SinglyLinkedList<String> gameTotal = new SinglyLinkedList<String> ();

        // POPULATE LISTS
        game1.addFirst("CLG");
        game1.addFirst("C9");

        game2.addFirst("TSM");
        game2.addFirst("Immortals");

        // PRINT LISTS
        System.out.printf ("\n\nGame 1: %s\n", game1);
        System.out.printf ("\n\nGame 2: %s\n", game2);

        // CALL CONCATENATE METHOD, STORE IN gameTotal
        gameTotal = concatenate(game1, game2);

        // PRINT TOTAL GAMES LIST FROM gameTotal
        System.out.printf("\n\nAll Games: %s\n", gameTotal);

    }

}

/* Can't figure out why .addLast won't work but .addFirst will. 
 * It screws up the order a little in the final list as well as
 * an extra ", " after CLG which shows an empty spot in the list
 * at the end. Don't know how to get rid of that either. */

我已经创建了两个链接列表,我需要将前两个列表连接起来填充第三个列表。我被卡住了,因为我需要打印出第三个列表,而我和我的朋友不知道如何在两个列表上使用连接函数来制作第三个并打印它。

【问题讨论】:

    标签: java singly-linked-list


    【解决方案1】:

    由于您使用addFirst 添加所有项目,并且根据您只想连接它们(而不是排序)的问题,我假设您正在寻找的结果是:

    List 1: C9 -> CLG        
    List 2: Immortals -> TSM
    Expected result: C9 -> CLG -> Immortals -> TSM
    

    所以假设,实现可以是:

    public static SinglyLinkedList<String> concatenate (SinglyLinkedList<String>game1, SinglyLinkedList<String>game2) {
        SinglyLinkedList<String> result;
        SinglyLinkedList<String> temp;
        try {
            result = game1.clone(); // game1 copied into result
            temp = game2.clone(); // get copy of second list, which we will destroy in the process of concatenation
        }
        catch(CloneNotSupportedException e) { // can only happen if it was not implemented
            return null; 
        }
        while(!temp.isEmpty()) {
            result.addLast(temp.removeFirst());
        }
    
        return result;
    }
    

    【讨论】:

    • 我需要为 .clone() 抛出一个异常,因为它是一个未处理的异常。编辑:我什至可以在连接函数中重新创建相同的列表,但我知道那会很愚蠢。
    • 只有在没有实现克隆方法的情况下才会发生该异常。因此,您当然需要知道它是否由您使用的库实现(似乎是),但如果是,则永远不会发生此异常。因此,我添加了对该异常的非常原始的处理
    • 好的,我明白这一切,除了临时和结果没有得到解决。
    • 你有没有定义它们上面试试?它给你的错误到底是什么?
    • 好的,上面的代码没有错误,让我试着让它现在运行,看看会发生什么。
    【解决方案2】:

    你可以通过遍历 game2 来做一些事情,比如: 基本上是从game2的头部开始并附加到game1。

    您可以将方法添加到列表中,例如

    public SinglyLinkedListNode getHead()
    {
       return head;
    }
    

    并使用它

    public void concatenate(SinglyLinkedList game1, SinglyLinkedList game2) 
    {
    
                SinglyLinkedListNode current = game2.getHead();
                while (current != null) 
               {
    
                      game1.addLast( current );
                      current = current.next;
                }
    
    }
    

    编辑:我看到你正在使用来自 here 的 SinglyLinkedList.java

    此方法将通过遍历起作用,因此您应该仅将其视为知道如何通过遍历进行连接。

    【讨论】:

    • 我收到一个错误,SinglyLinkedListNode 无法解析为类型,并且 SinglyLinkedList.head 字段也不可见。编辑:我是否缺少一些声明或导入?
    • 你可以为你的头部添加一个getter并使用它。
    • 我收回我说的话,你能展示你的 addFirst() 方法的代码吗?您应该能够在列表中的节点中有一个“下一个”字段。我不明白“字符串”是如何做到的。
    • 我不明白第一个代码如何 public String getHead() { return head; } 会有所帮助,因为程序似乎不知道 head 是什么。
    • 这是我所有的代码......没有其他代码。编辑:它输出 - 游戏 1:(C9,CLG)游戏 2:(Immortals,TSM)
    猜你喜欢
    • 2019-07-04
    • 2016-12-20
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多