【发布时间】: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. */
我已经创建了两个链接列表,我需要将前两个列表连接起来填充第三个列表。我被卡住了,因为我需要打印出第三个列表,而我和我的朋友不知道如何在两个列表上使用连接函数来制作第三个并打印它。
【问题讨论】: