Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 list1 = new SinglyLinkedList("List 1"); SinglyLinkedList list2 = new SinglyLinkedList("List 2"); Scanner input = new Scanner(System.in); while(true){ int data = Integer.parseInt(input.next()); if(data == -1) break; list1.append(data); }

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 { private ListNode head; private String name; // constructor creates empty List with "List" as the name public SinglyLinkedList() { this("List"); }

// 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 currentNode = head; if(isEmpty()) { System.out.printf("Empty %s%n", name); return; } System.out.printf("The %s is:", name); System.out.printf("["); while (currentNode.next != null) { System.out.printf("%d, ", currentNode.data); currentNode = currentNode.next; } System.out.printf("%d]%n", currentNode.data); } // Removes the front Node public E removeFront() { // [Optional] you may want to implement this method } // Removes the LinkNode with the given data public void remove(E current) { // modify your implementation from the lab } // counts the length of the list public int length(){ // modify your implementation from the lab } // get an item from the list specified by the index public E get(int index){ // modify your implementation from the lab }

// 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 list2) { // Complete this method }

private boolean isEmpty() { // [Optional] You can use this method } }

____CLASS LISTNODE

// class to represent one node in a list public class ListNode{ E data; // data for the node ListNode next; // reference to the next node in the list // constructor creates a ListNode that refers to object ListNode(E object){ this(object, null); } // constructor creates ListNode that refers to the specified object // and to the next ListNode ListNode(E object, ListNode node){ data = object; next = node; } // return reference to data in node E getData() { return data; } // return reference to next node in list ListNode getNext(){ return next; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How did you feel about taking piano lessons as a child? (general)

Answered: 1 week ago