将单链表连接成一个列表
Concatenating Singly Linked Lists Into One List
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. */
我制作了两个链表,我需要将前两个链表连接起来填充第三个链表。我被卡住了,因为我需要打印出第三个列表,我和我的朋友们不知道如何在两个列表上使用连接函数来制作第三个列表并打印它。
你可以通过遍历 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;
}
}
编辑:我看到您使用的 SinglyLinkedList.java 可能来自 here
此方法将通过遍历工作,因此您应该只将此视为知道如何通过遍历连接。
由于您使用 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;
}
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. */
我制作了两个链表,我需要将前两个链表连接起来填充第三个链表。我被卡住了,因为我需要打印出第三个列表,我和我的朋友们不知道如何在两个列表上使用连接函数来制作第三个列表并打印它。
你可以通过遍历 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;
}
}
编辑:我看到您使用的 SinglyLinkedList.java 可能来自 here
此方法将通过遍历工作,因此您应该只将此视为知道如何通过遍历连接。
由于您使用 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;
}