Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Given the LinkedList<E> and Node<E> classes in the attached files, write the

ID: 3577273 • Letter: 4

Question

4. Given the LinkedList<E> and Node<E> classes in the attached files, write the following method

public LinkedList<E> addAll(LinkedList<E> p)

that when called as LL1.addAll(LL2), will add all of the elements of the linked list LL2 to the tail end of LL1 and return the modified LL1, where both LL1 and LL2 are linked lists containing E. This method needs to be included in LinkedList<E> class and tested to your satisfaction.

import java.io.*;

public class LinkedList<E>

{

Node<E> head;

Node<E> tail;

int size;

  

public LinkedList()

{

head=null;

tail=head;

size=0;

}

  

  

  

public boolean isEmpty() {

  

return (size==0);

}

public int size() {

return size;

}

public void addFirst(E val) {

  

Node<E> first = new Node<E>();

first.setValue(val);

  

if (isEmpty() ){

  

head=first;

tail=first;

}

  

else {

first.setNext(head);

head.setPrev(first);

head=first;

  

}

size++;

}

public ArrayList<E> returnList() {

  

ArrayList<E> p =new ArrayList<E>(size);

  

Node<E> temp;

  

if (isEmpty()) {

  

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

  

  

}

else {

  

temp= head;

while (temp!=null) {

//System.out.println(temp.getValue());

p.add(temp.getValue());

temp=temp.getNext();

}

}

return p;

}

public void printList() {

  

Node<E> temp;

  

if (isEmpty()) {

  

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

  

  

}

else {

  

temp= head;

while (temp!=null) {

System.out.println(temp.getValue());

temp=temp.getNext();

} ;

}

  

}

public void addLast (E val) {

Node<E> last = new Node<E>();

last.setValue(val);

  

if (isEmpty()) {

  

head=last;

tail=last;

}

else {

  

tail.setNext(last);

last.setPrev(tail);

tail=last;

}

size ++;

}

// handle error situations

// index value has to be >=0 and <=size

// size()=0 you do special stuff

public void add(int index, E val) {

  

if ((index>=0)||(index<=size)) {

  

if (index==0) addFirst(val);

else if (index==size) addLast(val);

else {

int j =0;

Node<E> temp= head;

while (j!=index) {

//System.out.println(temp.getValue());

temp=temp.getNext();

j++;

}

Node<E> newNode = new Node<E>();

newNode.setNext(temp);

newNode.setPrev(temp.getPrev());

(temp.getPrev()).setNext(newNode);

// (newNode.getPrev()).setNext(newNode);

temp.setPrev(newNode);

newNode.setValue(val);

size++;

}

  

}

else

System.out.println ("index out of bounds ...");

  

}

public E removeFirst() {

  

E val;

  

if (isEmpty()) {

System.out.println("List empty - nothing to remove");

return null;

}

else {

val=head.getValue();

if (size==1) {

head=null;   

tail=null;

}

else {

head=head.getNext();

head.setPrev(null);

}

size--;

return val;

}

}

//handle empty list

public E removeLast() {

  

E val=null;

  

if ((isEmpty())||(size==1)) val=removeFirst();

  

else {

val =tail.getValue();

tail=tail.getPrev();

tail.setNext(null);

size--;

  

}

return val;

}

// returns true if the linkedlist contains the item val

// returns false otherwise

//for testing equality of objects use the equals() method. Assume toString() method exists for val and for items stored in the linked list.

public boolean contains(Object val) {

Node<E> temp;

temp= head;

while (temp!=null) {

if (temp.getValue().toString().equals(val.toString()))

return true;

else

temp=temp.getNext();   

}

return false;

}

public LinkedList<E> addAll(LinkedList<E> p) {

}

// Returns:

//a new LinkedList which contains the elements in the reverse order of this list from head to tail

// null if the list is empty.

public LinkedList<E> reverseList() {

LinkedList<E> p = new LinkedList<E>();

Node<E> temp=head;

while (temp!=null) {

p.addFirst(temp.getValue());

temp=temp.getNext();

}

return p;   

}

//Parameters:

//index - index of the element to return

//Returns:

//the element at the specified position in this list

//Throws:

//IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

public E get (int index) {

int i=0;

if (index < 0 || index >= size)throw new IndexOutOfBoundsException();

else {

Node<E> temp= head;

  

while (i!=index) {

temp=temp.getNext();

i++;

}

return (temp.getValue());

}

}

//Parameters:

//index - the index of the element to be removed

//Returns:

//the element previously at the specified position

//Throws:

//IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

public E remove(int index) {

if (index < 0 || index >= size)throw new IndexOutOfBoundsException();

if (index==0) return removeFirst();

else if (index==size-1) return removeLast();

else {   

E val=null;

int i=0;

Node<E> temp= head;   

while (i!=index) {

temp=temp.getNext();

i++;

}   

val=temp.getValue();

(temp.getPrev()).setNext(temp.getNext());

(temp.getNext()).setPrev(temp.getPrev());

return (val);

}

}

//Parameters:

//index - index of the element to replace

//val - element to be stored at the specified position

//Returns:

//the element previously at the specified position

//Throws:

//IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

public E set(int index, E val) {

if (index < 0 || index >= size)throw new IndexOutOfBoundsException();

int i=0;

E value=null;

Node<E> temp= head;   

while (i!=index) {

temp=temp.getNext();

i++;

}

value=temp.getValue();

temp.setValue(val);

return (value);

}

}

Explanation / Answer

PROGRAM CODE:

package simple;

import java.io.*;

import java.util.ArrayList;

/*this was written for my referrence. Since Node class was not provided

* class Node<E>

{

   E val;

   Node<E> next;

   Node<E> prev;

  

   public Node<E> getNext() {

       return next;

   }

   public void setNext(Node<E> next) {

       this.next = next;

   }

   public Node<E> getPrev() {

       return prev;

   }

   public void setPrev(Node<E> prev) {

       this.prev = prev;

   }

   void setValue(E val)

   {

       this.val = val;

   }

  

   E getValue()

   {

       return val;

   }

}*/

public class LinkedList<E>

{

Node<E> head;

Node<E> tail;

int size;

  

public LinkedList()

{

   head=null;

   tail=head;

   size=0;

}

  

  

  

public boolean isEmpty() {

  

return (size==0);

   }

   public int size() {

   return size;

}

public void addFirst(E val) {

  

Node<E> first = new Node<E>();

first.setValue(val);

  

if (isEmpty() ){

  

head=first;

tail=first;

}

  

else {

first.setNext(head);

head.setPrev(first);

head=first;

  

}

size++;

}

public ArrayList<E> returnList() {

  

ArrayList<E> p =new ArrayList<E>(size);

  

Node<E> temp;

  

if (isEmpty()) {

  

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

  

  

}

else {

  

temp= head;

   while (temp!=null) {

//System.out.println(temp.getValue());

p.add(temp.getValue());

temp=temp.getNext();

}

   }

return p;

}

public void printList() {

  

Node<E> temp;

  

if (isEmpty()) {

  

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

  

  

}

else {

  

temp= head;

   while (temp!=null) {

System.out.println(temp.getValue());

temp=temp.getNext();

} ;

   }

  

}

public void addLast (E val) {

   Node<E> last = new Node<E>();

last.setValue(val);

  

if (isEmpty()) {

  

head=last;

tail=last;

}

else {

  

tail.setNext(last);

last.setPrev(tail);

tail=last;

}

size ++;

}

// handle error situations

// index value has to be >=0 and <=size

// size()=0 you do special stuff

public void add(int index, E val) {

  

if ((index>=0)||(index<=size)) {

  

if (index==0) addFirst(val);

else if (index==size) addLast(val);

else {

int j =0;

Node<E> temp= head;

while (j!=index) {

//System.out.println(temp.getValue());

temp=temp.getNext();

j++;

}

Node<E> newNode = new Node<E>();

newNode.setNext(temp);

newNode.setPrev(temp.getPrev());

(temp.getPrev()).setNext(newNode);

   // (newNode.getPrev()).setNext(newNode);

temp.setPrev(newNode);

newNode.setValue(val);

size++;

}

  

}

else

System.out.println ("index out of bounds ...");

  

}

public E removeFirst() {

  

E val;

  

if (isEmpty()) {

System.out.println("List empty - nothing to remove");

return null;

}

else {

val=head.getValue();

if (size==1) {

head=null;

tail=null;

}

   else {

head=head.getNext();

head.setPrev(null);

}

   size--;

   return val;

}

}

//handle empty list

public E removeLast() {

  

E val=null;

  

if ((isEmpty())||(size==1)) val=removeFirst();

  

else {

   val =tail.getValue();

tail=tail.getPrev();

tail.setNext(null);

size--;

  

}

return val;

}

// returns true if the linkedlist contains the item val

// returns false otherwise

//for testing equality of objects use the equals() method. Assume toString() method exists for val and for items stored in the linked list.

public boolean contains(Object val) {

Node<E> temp;

temp= head;

   while (temp!=null) {

if (temp.getValue().toString().equals(val.toString()))

return true;

else

temp=temp.getNext();

}

return false;

}

public LinkedList<E> addAll(LinkedList<E> p) {

   LinkedList<E> linkedlist = this;

   Node<E> temp;

   if (p.isEmpty()) {

  

      System.out.println("list is empty !");

  

      }

      else {

  

      temp= p.head;

   while (temp!=null) {

      //System.out.println(temp.getValue());

      E val = temp.getValue();

      linkedlist.addLast(val);

      temp=temp.getNext();

      }

      }

   return linkedlist;

}

// Returns:

//a new LinkedList which contains the elements in the reverse order of this list from head to tail

// null if the list is empty.

public LinkedList<E> reverseList() {

LinkedList<E> p = new LinkedList<E>();

Node<E> temp=head;

while (temp!=null) {

p.addFirst(temp.getValue());

temp=temp.getNext();

}

return p;

}

//Parameters:

//index - index of the element to return

//Returns:

//the element at the specified position in this list

//Throws:

//IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

public E get (int index) {

int i=0;

if (index < 0 || index >= size)throw new IndexOutOfBoundsException();

else {

Node<E> temp= head;

  

   while (i!=index) {

temp=temp.getNext();

i++;

}

return (temp.getValue());

}

}

//Parameters:

//index - the index of the element to be removed

//Returns:

//the element previously at the specified position

//Throws:

//IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

public E remove(int index) {

if (index < 0 || index >= size)throw new IndexOutOfBoundsException();

if (index==0) return removeFirst();

else if (index==size-1) return removeLast();

else {

E val=null;

int i=0;

Node<E> temp= head;

   while (i!=index) {

temp=temp.getNext();

i++;

}

val=temp.getValue();

(temp.getPrev()).setNext(temp.getNext());

(temp.getNext()).setPrev(temp.getPrev());

return (val);

}

}

//Parameters:

//index - index of the element to replace

//val - element to be stored at the specified position

//Returns:

//the element previously at the specified position

//Throws:

//IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

public E set(int index, E val) {

   if (index < 0 || index >= size)throw new IndexOutOfBoundsException();

int i=0;

   E value=null;

Node<E> temp= head;

   while (i!=index) {

temp=temp.getNext();

i++;

}

value=temp.getValue();

temp.setValue(val);

return (value);

}

}

package simple;

public class LinkedListTester

{

   public static void main(String[] args)

   {

       LinkedList<Integer> ll1 = new LinkedList<>();

       LinkedList<Integer> ll2 = new LinkedList<>();

       ll1.addFirst(1);

       ll1.addFirst(2);

       System.out.println("LL1: ");

       ll1.printList();

       ll2.add(0, 3);

       ll2.addAll(ll1);

       System.out.println("LL2: ");

       ll2.printList();

   }

}

OUTPUT:

LL1:

2

1

LL2:

3

2

1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote