In: Computer Science
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. I need it to work with the generic version of SLLNode. It is posted at the bottom.
public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements in the linked list while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } static SLLNode generateLL1() { // Create/return a linked list that has {3, 4, 1, 2} // Note that this is not quite a useful function. Just for practice purpose } static SLLNode generateLL2(int a, int b) { // Create/return a linked list that has {a, b, a, b} // eg) generateLL2(10,20) returns a list {10,20,10,20} } static SLLNode generateLL_with_array(int[] nums) { // Creat/return a linked list using the given int array // Return null if the array is empty (size is zero). // eg) generateLL3(new int[]{2,3,4}) returns a list {2,3,4} } static void attach(SLLNode ls1, SLLNode ls2) { // Given two linked lists, attach the second list at the end of the first list // eg) Suppose ls1={1,2,3}, ls2={50,60} as lists, attach(ls1, ls2) makes ls1 = {1,2,3,50,60} // Assume ls1 is not empty. // Hint: You need to go to the last node of ls1 and make a connection from it to the ls2 } public static void main(String[] args) { printLinkedList(generateLL1()); // 3 4 1 2 printLinkedList(generateLL2(20,30)); // 20 30 20 30 printLinkedList(generateLL_with_array(new int[] {2})); // 2 printLinkedList(generateLL_with_array(new int[] {2,3,4,5})); // 2 3 4 5 SLLNode ls1 = generateLL1(); attach(ls1,generateLL2(20,30)); printLinkedList(ls1); // 3 4 1 2 20 30 20 30 } }
---------------------------------------------------------------------------------------------------
public class SLLNode<E> { E info; SLLNode<E> next; public SLLNode(E val) { info = val; next = null; } }
Solution:
ListDemoHw.java
public class ListDemoHw {
public static void printLinkedList(SLLNode node) {
// display all elements in the linked list
while (node != null) {
System.out.print(node.info + " ");
node = node.next; // move to the next node
}
System.out.println();
}
static SLLNode generateLL1() {
// Create/return a linked list that has {3, 4, 1, 2}
// Note that this is not quite a useful function. Just for practice purpose
SLLNode head = new SLLNode(3); // 3 at head
// Nodes for 4, 1, 2
SLLNode node2 = new SLLNode(4);
SLLNode node3 = new SLLNode(1);
SLLNode node4 = new SLLNode(2);
// Connect nodes
head.next = node2;
node2.next = node3;
node3.next = node4;
return head;
}
static SLLNode generateLL2(int a, int b) {
// Create/return a linked list that has {a, b, a, b}
// eg) generateLL2(10,20) returns a list {10,20,10,20}
int[] nums = { a, b, a, b };
return generateLL_with_array(nums);
}
static SLLNode generateLL_with_array(int[] nums) {
// Create/return a linked list using the given int array
// Return null if the array is empty (size is zero).
// eg) generateLL3(new int[]{2,3,4}) returns a list {2,3,4}
// Head
SLLNode head = new SLLNode(nums[0]);
SLLNode temp = head; // temp at head
for (int i = 1; i < nums.length; i++) {
SLLNode newNode = new SLLNode(nums[i]); // new node
temp.next = newNode; // connecting newnode to temp
temp = newNode; // changing temp
}
return head;
}
static void attach(SLLNode ls1, SLLNode ls2) {
// Given two linked lists, attach the second list at the end of the first list
// eg) Suppose ls1={1,2,3}, ls2={50,60} as lists, attach(ls1, ls2) makes ls1=
// {1,2,3,50,60}
// Assume ls1 is not empty.
// Hint: You need to go to the last node of ls1 and make a connection from it to
// the ls2
// Get the tail of first linked list
SLLNode temp = ls1;
while (temp.next != null) {
temp = temp.next;
}
// Connect tail of ls1 with head of ls2
temp.next = ls2;
}
public static void main(String[] args) {
printLinkedList(generateLL1()); // 3 4 1 2
printLinkedList(generateLL2(20, 30)); // 20 30 20 30
printLinkedList(generateLL_with_array(new int[] { 2 })); // 2
printLinkedList(generateLL_with_array(new int[] { 2, 3, 4, 5 })); // 2 3 4 5
SLLNode ls1 = generateLL1();
attach(ls1, generateLL2(20, 30));
printLinkedList(ls1); // 3 4 1 2 20 30 20 30
}
}
SLLNode.java
public class SLLNode<E> {
E info;
SLLNode<E> next;
public SLLNode(E val) {
info = val;
next = null;
}
}
Output:
Screenshots: