In: Computer Science
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;
}
}
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;
}
}