Question

In: Computer Science

please modify the method public void addList(SinglyLinkedList<E> s) that add list to another with addlast() and...

please modify the method public void addList(SinglyLinkedList<E> s) that add list to another with addlast() and remove() methods without changing the parameter of addList method.

//////////////////////////////////////////////////

public class SinglyLinkedList<E> {

     private class Node<E> {

           private E element;

           private Node<E> next;

           public Node(E e, Node<E> n) {

                element = e;

                next = n;

           }

           public E getElement() {

                return element;

           }

           public void setElement(E element) {

                this.element = element;

           }

           public Node<E> getNext() {

                return next;

           }

           public void setNext(Node<E> next) {

                this.next = next;

           }

     }

     private Node<E> head = null;

     private Node<E> tail = null;

     private int size = 0;

     public SinglyLinkedList() {

     }

     public void size() {

           System.out.println(size);

     }

     public boolean isEmpty() {

           return size == 0;

     }

     public E first() {

           if (isEmpty())

                return null;

           return head.getElement();

     }

     public E last() {

           if (isEmpty())

                return null;

           return tail.getElement();

     }

     public void add(E e) {

           head = new Node<>(e, head);

           if (size == 0)

                tail = head;

           size++;

     }

     public void addLast(E e) {

           Node<E> newest= new Node<>(e, null);

           if (isEmpty()) head=newest;

           else tail.setNext(newest);

           tail=newest;

           size++;

     }

     public void addFirst(E e) {

           head= new Node<>(e, head);

           if (size==0) tail=head;

           size++;

     }

     public void display() {

           Node<E> current = head;

           if (head == null) {

                System.out.println("List is empty");

                return;

           }

           System.out.println("Nodes of singly linked list: ");

           while (current != null) {

                System.out.println(current.getElement() );

                current = current.next;

           }

           System.out.println();

     }

     public void addList(SinglyLinkedList<E> s) {

           Node<E> current= s.head;

           while (current!=null) {

                addLast(current.element);

                current.getNext();

           }

           tail=s.tail;

           size+=s.size;

     }

}

Solutions

Expert Solution

Updation done in the code :

in the method public void addList(SinglyLinkedList<E> s){}

with the current code it was going into infinite loop

Reason for infinite loop : the variable current was not getting updated , ie for every iteration , current must be updated with current.getNext(). With the current code current.getNext() was called but its value was never getting updated in current variable. So a minor change in the code fixed it.

-----------------------------------------------------------------------------

Please find the main method to test the code and output below:

Please find the actual code for SinglyLinkedList class below:

------------------------------------------------------------------------------------------------------------

class SinglyLinkedList<E> {
private class Node<E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> n) {
element = e;
next = n;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}

private Node<E> head = null;

private Node<E> tail = null;

private int size = 0;

public SinglyLinkedList() {
}

public void size() {
System.out.println(size);
}

public boolean isEmpty() {
return size == 0;
}

public E first() {
if (isEmpty())
return null;
return head.getElement();
}

public E last() {
if (isEmpty())
return null;
return tail.getElement();
}

public void add(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size++;
}

public void addLast(E e) {
Node<E> newest = new Node<>(e, null);
if (isEmpty())
head = newest;
else
tail.setNext(newest);
tail = newest;
size++;
}

public void addFirst(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size++;
}

public void display() {
Node<E> current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while (current != null) {
System.out.println(current.getElement());
current = current.next;
}
System.out.println();
}

public void addList(SinglyLinkedList<E> s) {
Node<E> current = s.head;
while (current != null) {
addLast(current.element);
current = current.getNext(); //this line was updated
}
tail = s.tail;
size += s.size;
}


}



Related Solutions

In Java, write the method public static void insertUnique(List l, T e), user of the ADT...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Write a method public static <E> List<List<E>> filterMinLength(List<List<E>> listOfLists, int minLength) that given a list of...
Write a method public static <E> List<List<E>> filterMinLength(List<List<E>> listOfLists, int minLength) that given a list of lists and a minimum length returns a new list of lists, containing only the lists that are at least as long as the minimum length in the same order they appeared in the input. The original list must not be modified. For example, if listOfLists = [[1, 2, 3], [], [4, 5], [6, 7, 8, 9]] and minLength = 3, then the method should...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol"
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects the language is in java
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void...
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output? Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output?
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the List method: void printDogList() //print all dogs in list /* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */ Method: void findDogUsingWhile(String dogName) /* You’ll need an index. Iterate over your list of dog...
~IMPORTANT: you are NOT allowed to modify public static void main.~ Complete and fix program, by...
~IMPORTANT: you are NOT allowed to modify public static void main.~ Complete and fix program, by defining a countOccurrences function, and by modifying the existing mostFrequentCharacter function, so as to satisfy the following specs: Function countOccurrences takes two arguments, a string and a character. It returns the number of times the character occurs in the string. Function mostFrequentCharacter takes a string as an argument. It returns the character that occurs the most times in the string. If multiple characters tie...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT