Having issues with this java problem. Problem: Describe an algorithm for concate
ID: 3670446 • Letter: H
Question
Having issues with this java problem. Problem: Describe an algorithm for concatenating two singly linked lists L and M, into a single list L' that contains all the nodes of L followed by all the nodes of M. But, rather than just describe such an algorithm, create a Java program and method(s) that will do so. Consider using at least three lists. Two of them L and M (although create better names – and lowercase like ‘first’ and ‘second’) you will create and populate with GameEntry objects. The third list L’ (or ‘concat’) will contain the concatenated nodes. Create a method named concatenate () that looks like this: public static SinglyLinkedList concatenate (SinglyLinkedListfirst, SinglyLinkedListsecond); Print the first two lists before calling concatenate. Then print the concatenated list. (And, SinglyLinkedList does contain a ‘toString’ method that can be used.) Use class SinglyLinkedList of the net.datastructures package.
Explanation / Answer
concatenate function
public static SinglyLinkedList concatenate (SinglyLinkedList first, SinglyLinkedList second)
{
// reference to any SinglyLinkedList object
SinglyLinkedList p;
// creating new object of SinglyLinkedList
SinglyLinkedList concat = new SinglyLinkedList();
// reference p to first linkedlist
p = first;
while (p != NULL) //loop until first linked list is empty
{
// add each node of p to concat
concat.add(p.node);
// point p to next node
p = p.next;
}
// reference p to second linkedlist
p = second;
while (p != NULL) //loop until second linked list is empty
{
// add each node of p to concat
concat.add(p.node);
// point p to next node
p = p.next;
}
// return concat object
return (concat);
}
------------------------------------------------------------------------------------------------------------------------
Can provide the output of the function if the definition of SinglyLinkedList is provided
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.