package 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. */
我制作了两个链接列表,我需要填充第三个列表,前两个列表连接起来。我被困了,因为我需要打印出第三个列表,我的朋友和我无法弄清楚如何在两个列表上使用连接函数来制作第三个并打印它。
答案 0 :(得分:0)
您可以通过遍历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
此方法将通过遍历工作,因此您应该将其视为知道如何通过遍历连接。
答案 1 :(得分:0)
由于您使用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;
}