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

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...
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...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class. Use the following code to test your work. public static void testCode()         {             SavingsAccount savings = new SavingsAccount(100.00, 3.5);             SavingsAccount s = (SavingsAccount)savings.ReadAccount();...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
In C++ please. 3. splice() is a specialized method that inserts one list into another in...
In C++ please. 3. splice() is a specialized method that inserts one list into another in constant time but destroys the first list. Explain why this may be implemented for the list but not for any other sequential container. Give an example usage of splice()
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end...
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end public Integer dequeue() // remove from front public boolean isEmpty() // return true if empty Write a method rearrange(Queue q) that takes a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the list. For example, if a Queue contained...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT