Question
Concatenating two Linked Lists Given two linked lists, complete the public void concatenate( ) method that will concatenate the elements of the given list to
Concatenating two Linked Lists Given two linked lists, complete the public void concatenate( ) method that will concatenate the elements of the given list to the list calling the method. Make sure your program addresses edge cases such as an empty list. For this program, we will modify our LinkedList class such that it supports any data type.
____ CLASS LISTCONCATENATE
import java.util.Scanner;
public class ListConcatenate { public static void main(String[] args) { // Create two linked lists SinglyLinkedList
System.out.println("List 1 items: "); list1.print();
while(true){ int data = Integer.parseInt(input.next()); if(data == -1) break; list2.append(data); } System.out.println(" List 2 items: "); list2.print(); list1.concatenate(list2); System.out.println(" Concatenated list is: "); list1.print(); }
}
______CLASS SinglyLinkedList
import java.util.NoSuchElementException;
class SinglyLinkedList
// constructor creates an empty List with a name public SinglyLinkedList(String listName) { name = listName; head = null; } // inserts data to the end of the list only using the head pointer public void append(E data){ // modify your implementation from the lab } // inserts data to the beginning of the list public void prepend(E data){ // modify your implementation from the lab } // print the linked list elements public void print() { ListNode
// insert a new data after the given one public void insertAfter(E givenData, E newData){ // Use your implementation from the lab }
public void concatenate(SinglyLinkedList
private boolean isEmpty() { // [Optional] You can use this method } }
____CLASS LISTNODE
// class to represent one node in a list public class ListNode
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started