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...
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()
Modify listarr.java program by adding the following 2 methods: public void insertsorted(x); // Inert x in...
Modify listarr.java program by adding the following 2 methods: public void insertsorted(x); // Inert x in a sorted list. protected int binsearch(x); // Binary search for x Assume you have a data file p1.txt with the following contents: 8     4 15 23 12 36 5 36 42 3 5 14 36 and your main program is in p1.java file. To compile: javac p1.java To execute: java p1 < any data file name say p1.txt Your output should be formatted (i.e....
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...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT