SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
Given below is the completed implementation of MyLinkedList class. Other classes are not
modified and remain same as in question - MyList, MyAbstractList, TestMyLinkedList.
The output of TestMyLinkedList is shown below. Please don't forget to rate the answer if it
helped. Thank you very much.
Since the MyList is not completely pasted in the question ... giving the complete class file below
MyList.java
public interface MyList> extends java.lang.Iterable
{
// Add a new element at the end of this list
public void add (E e);
// Add a new element at specified index in this list
public void add (int index, E e);
// Return true if this list contains the element
public boolean contains (E e);
// Return the element at the specified index
public E get (int index);
// Return the index of the first matching element
// Return -1 if no match.
public int indexOf (E e);
// Return the index of the last matching element
// Return -1 if no match.
public int lastIndexOf (E e);
E remove(int index);
void clear();
E set(int index, E e);
}
MyLinkedList.java
public class MyLinkedList> extends MyAbstractList
implements Comparable>
{
private Node head, tail; // head and tail pointers
// Create a default list
public MyLinkedList()
{
}
public int size()
{
return size;
}
// Need this for implementing Comparable
public int compareTo (MyLinkedList e)
{
return(size() - e.size());
}
// Create a list from an array of objects
public MyLinkedList (E[] objects)
{
super(objects); // Passes the array up to abstract parent
}
// Return the head element in the list
public E getFirst()
{
if (size == 0)
{
return null;
}
else
{
return head.element;
}
}
// Return the last element in the list
public E getLast()
{
if (size == 0)
{
return null;
}
else
{
return tail.element;
}
}
// Add an element to the beginning of the list
public void addFirst (E e)
{
// Create a new node for element e
Node newNode = new Node(e);
newNode.next = head; // link the new node with the head
head = newNode; // head points to the new node
size++; // Increase list size
if (tail == null) // new node is only node
{
tail = head;
}
}
// Add an element to the end of the list
public void addLast (E e)
{
// Create a new node for element e
Node newNode = new Node(e);
if (tail == null)
{
head = tail = newNode; // new node is only node
}
else
{
tail.next = newNode; // Link the new with the last node
tail = tail.next; // tail now points to the last node
}
size++; // Increase size
}
// Remove the head node and
// return the object from the removed node
public E removeFirst()
{
if (size == 0)
{
return null;
}
else
{
Node temp = head;
head = head.next;
size--;
if (head == null)
{
tail = null;
}
return temp.element;
}
}
// Remove the last node and return the object from it
public E removeLast()
{
if (size == 0)
{
return null;
}
else if (size == 1)
{
Node temp = head;
head = tail = null;
size = 0;
return temp.element;
}
else
{
Node current = head;
for (int i = 0; i < size - 2; i++)
{
current = current.next;
}
Node temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
// Remove the element at the specified position
// Return the element that was removed from the list.
@Override
public E remove (int index)
{
if (index < 0 || index >= size)
{
return null;
}
else if (index == 0)
{
return removeFirst();
}
else if (index == size - 1)
{
return removeLast();
}
else
{
Node previous = head;
for (int i = 1; i < index; i++)
{
previous = previous.next;
}
Node current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
// Return a String representation of the linked list elements
@Override
public String toString()
{
StringBuilder result = new StringBuilder("[");
Node current = head;
for (int i = 0; i < size; i++)
{
result.append (current.element);
current = current.next;
if (current != null)
{
result.append (", "); // Separate elements with a comma
}
else
{
result.append ("]"); // Insert the ] in the string
}
}
return result.toString();
}
// Clear the list
@Override
public void clear()
{
size = 0;
head = tail = null;
}
// Add a new element at specified index in this list
@Override
public void add(int index, E e)
{
if (index == 0)
{
addFirst (e);
}
else if (index == size)
{
addLast (e);
}
else
{
checkIndex (index);
// Create a new node for element e
Node newNode = new Node(e);
Node previous = head;
for (int i = 1; i < index; i++)
{
previous = previous.next;
}
newNode.next = previous.next;
previous.next = newNode;
size++; // Increase size
}
}
// Return true if this list contains the element e
@Override
public boolean contains(E e)
{
return indexOf(e) != -1;
}
// Return the element at the specified index
@Override
public E get (int index)
{
Node current = head;
for(int i = 0; current != null && i < index; i++)
current = current.next;
if(current == null)
return null;
else
return current.element;
}
// Return the index of the first matching element
// Return -1 if no match
@Override
public int indexOf (E e)
{
Node current = head;
for(int i = 0; i < size; i++)
{
if(current.element.equals(e))
return i;
current = current.next;
}
return -1;
}
// Return the index of the last matching element
// Return -1 if no match.
@Override
public int lastIndexOf (E e)
{
int lastIndex = -1;
Node current = head;
for (int i = 0; i < size; i++)
{
if (e.equals (current.element))
{
lastIndex = i;
}
current = current.next;
}
return lastIndex;
}
// Replace the element at the specified position
// with the specified element
@Override
public E set (int index, E e)
{
Node current = head;
for (int i = 0; i < index; i++)
{
current = current.next;
}
E old = current.element;
current.element = e;
return old;
}
public boolean remove(E e) {
if (head == null)
return false;
else if(head.element.equals(e))
{
removeFirst();
return true;
}
else if(tail.element.equals(e))
{
removeLast();
return true;
}
else
{
Node previous = head;
while(previous != tail)
{
if(previous.next.element.equals(e))
{
previous.next = previous.next.next;
return true;
}
previous = previous.next;
}
return false;
}
}
// Override iterator() defined in Iterable
@Override
public java.util.Iterator iterator()
{
return new LinkedListIterator();
}
// Throws IndexOutOfBoundsException, if needed
private void checkIndex (int index)
{
if (index < 0 || index >= size)
{
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
}
}
// The Node class
private static class Node
{
E element;
Node next;
public Node (E element)
{
this.element = element;
}
}
// Private iterator class for myArrayList class
private class LinkedListIterator implements java.util.Iterator
{
private Node current = head; // Current index
@Override
public boolean hasNext()
{
return(current != null);
}
@Override
public E next()
{
E e = current.element;
current = current.next;
return e;
}
@Override
public void remove()
{
MyLinkedList.this.remove(current.element);
}
}
}
output
(1) [America]
(2) [Canada, America]
(3) [Canada, America, Russia]
(4) [Canada, America, Russia, France]
(5) [Canada, America, Germany, Russia, France]
(6) [Canada, America, Germany, Russia, France, Norway]
(7) [Poland, America, Germany, Russia, France, Norway]
(8) [America, Germany, Russia, France, Norway]
(9) [America, Germany, France, Norway]
(10) [America, Germany, France]
(11) AMERICA GERMANY FRANCE
(12) list.contains(America): true
(13) list.contains(France) : true
(14) list.contains(Germany): true
(15) list.contains(Poland) : false
(16) [France, America, Germany, France]
(17) [France, America, Germany, France, Germany]
(18) Index of first France: 0
(19) Index of last France: 3
(20) Index of last Germany: 4
(21) Index of first Germany: 2
(22) [America, Germany, France, Germany]
(23) [America, Germany, France]
(24) America Germany France
After clearing the list, the list size is 0
Solution
Given below is the completed implementation of MyLinkedList class. Other classes are not
modified and remain same as in question - MyList, MyAbstractList, TestMyLinkedList.
The output of TestMyLinkedList is shown below. Please don't forget to rate the answer if it
helped. Thank you very much.
Since the MyList is not completely pasted in the question ... giving the complete class file below
MyList.java
public interface MyList> extends java.lang.Iterable
{
// Add a new element at the end of this list
public void add (E e);
// Add a new element at specified index in this list
public void add (int index, E e);
// Return true if this list contains the element
public boolean contains (E e);
// Return the element at the specified index
public E get (int index);
// Return the index of the first matching element
// Return -1 if no match.
public int indexOf (E e);
// Return the index of the last matching element
// Return -1 if no match.
public int lastIndexOf (E e);
E remove(int index);
void clear();
E set(int index, E e);
}
MyLinkedList.java
public class MyLinkedList> extends MyAbstractList
implements Comparable>
{
private Node head, tail; // head and tail pointers
// Create a default list
public MyLinkedList()
{
}
public int size()
{
return size;
}
// Need this for implementing Comparable
public int compareTo (MyLinkedList e)
{
return(size() - e.size());
}
// Create a list from an array of objects
public MyLinkedList (E[] objects)
{
super(objects); // Passes the array up to abstract parent
}
// Return the head element in the list
public E getFirst()
{
if (size == 0)
{
return null;
}
else
{
return head.element;
}
}
// Return the last element in the list
public E getLast()
{
if (size == 0)
{
return null;
}
else
{
return tail.element;
}
}
// Add an element to the beginning of the list
public void addFirst (E e)
{
// Create a new node for element e
Node newNode = new Node(e);
newNode.next = head; // link the new node with the head
head = newNode; // head points to the new node
size++; // Increase list size
if (tail == null) // new node is only node
{
tail = head;
}
}
// Add an element to the end of the list
public void addLast (E e)
{
// Create a new node for element e
Node newNode = new Node(e);
if (tail == null)
{
head = tail = newNode; // new node is only node
}
else
{
tail.next = newNode; // Link the new with the last node
tail = tail.next; // tail now points to the last node
}
size++; // Increase size
}
// Remove the head node and
// return the object from the removed node
public E removeFirst()
{
if (size == 0)
{
return null;
}
else
{
Node temp = head;
head = head.next;
size--;
if (head == null)
{
tail = null;
}
return temp.element;
}
}
// Remove the last node and return the object from it
public E removeLast()
{
if (size == 0)
{
return null;
}
else if (size == 1)
{
Node temp = head;
head = tail = null;
size = 0;
return temp.element;
}
else
{
Node current = head;
for (int i = 0; i < size - 2; i++)
{
current = current.next;
}
Node temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
// Remove the element at the specified position
// Return the element that was removed from the list.
@Override
public E remove (int index)
{
if (index < 0 || index >= size)
{
return null;
}
else if (index == 0)
{
return removeFirst();
}
else if (index == size - 1)
{
return removeLast();
}
else
{
Node previous = head;
for (int i = 1; i < index; i++)
{
previous = previous.next;
}
Node current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
// Return a String representation of the linked list elements
@Override
public String toString()
{
StringBuilder result = new StringBuilder("[");
Node current = head;
for (int i = 0; i < size; i++)
{
result.append (current.element);
current = current.next;
if (current != null)
{
result.append (", "); // Separate elements with a comma
}
else
{
result.append ("]"); // Insert the ] in the string
}
}
return result.toString();
}
// Clear the list
@Override
public void clear()
{
size = 0;
head = tail = null;
}
// Add a new element at specified index in this list
@Override
public void add(int index, E e)
{
if (index == 0)
{
addFirst (e);
}
else if (index == size)
{
addLast (e);
}
else
{
checkIndex (index);
// Create a new node for element e
Node newNode = new Node(e);
Node previous = head;
for (int i = 1; i < index; i++)
{
previous = previous.next;
}
newNode.next = previous.next;
previous.next = newNode;
size++; // Increase size
}
}
// Return true if this list contains the element e
@Override
public boolean contains(E e)
{
return indexOf(e) != -1;
}
// Return the element at the specified index
@Override
public E get (int index)
{
Node current = head;
for(int i = 0; current != null && i < index; i++)
current = current.next;
if(current == null)
return null;
else
return current.element;
}
// Return the index of the first matching element
// Return -1 if no match
@Override
public int indexOf (E e)
{
Node current = head;
for(int i = 0; i < size; i++)
{
if(current.element.equals(e))
return i;
current = current.next;
}
return -1;
}
// Return the index of the last matching element
// Return -1 if no match.
@Override
public int lastIndexOf (E e)
{
int lastIndex = -1;
Node current = head;
for (int i = 0; i < size; i++)
{
if (e.equals (current.element))
{
lastIndex = i;
}
current = current.next;
}
return lastIndex;
}
// Replace the element at the specified position
// with the specified element
@Override
public E set (int index, E e)
{
Node current = head;
for (int i = 0; i < index; i++)
{
current = current.next;
}
E old = current.element;
current.element = e;
return old;
}
public boolean remove(E e) {
if (head == null)
return false;
else if(head.element.equals(e))
{
removeFirst();
return true;
}
else if(tail.element.equals(e))
{
removeLast();
return true;
}
else
{
Node previous = head;
while(previous != tail)
{
if(previous.next.element.equals(e))
{
previous.next = previous.next.next;
return true;
}
previous = previous.next;
}
return false;
}
}
// Override iterator() defined in Iterable
@Override
public java.util.Iterator iterator()
{
return new LinkedListIterator();
}
// Throws IndexOutOfBoundsException, if needed
private void checkIndex (int index)
{
if (index < 0 || index >= size)
{
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
}
}
// The Node class
private static class Node
{
E element;
Node next;
public Node (E element)
{
this.element = element;
}
}
// Private iterator class for myArrayList class
private class LinkedListIterator implements java.util.Iterator
{
private Node current = head; // Current index
@Override
public boolean hasNext()
{
return(current != null);
}
@Override
public E next()
{
E e = current.element;
current = current.next;
return e;
}
@Override
public void remove()
{
MyLinkedList.this.remove(current.element);
}
}
}
output
(1) [America]
(2) [Canada, America]
(3) [Canada, America, Russia]
(4) [Canada, America, Russia, France]
(5) [Canada, America, Germany, Russia, France]
(6) [Canada, America, Germany, Russia, France, Norway]
(7) [Poland, America, Germany, Russia, France, Norway]
(8) [America, Germany, Russia, France, Norway]
(9) [America, Germany, France, Norway]
(10) [America, Germany, France]
(11) AMERICA GERMANY FRANCE
(12) list.contains(America): true
(13) list.contains(France) : true
(14) list.contains(Germany): true
(15) list.contains(Poland) : false
(16) [France, America, Germany, France]
(17) [France, America, Germany, France, Germany]
(18) Index of first France: 0
(19) Index of last France: 3
(20) Index of last Germany: 4
(21) Index of first Germany: 2
(22) [America, Germany, France, Germany]
(23) [America, Germany, France]
(24) America Germany France
After clearing the list, the list size is 0

Weitere ähnliche Inhalte

Ähnlich wie Given below is the completed implementation of MyLinkedList class. O.pdf

The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docxKomlin1
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxcgraciela1
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdfasarudheen07
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfJamesPXNNewmanp
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdffcaindore
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfdeepak596396
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfkingsandqueens3
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfaminbijal86
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docxVictorXUQGloverl
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxSHIVA101531
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfxlynettalampleyxc
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxVictorzH8Bondx
 
Complete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdfComplete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdfabbecindia
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfStewart29UReesa
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfseoagam1
 

Ähnlich wie Given below is the completed implementation of MyLinkedList class. O.pdf (20)

The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
Complete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdfComplete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 

Mehr von info430661

Yes, molality = moles of solute kg of solventSolutionYes, mo.pdf
Yes, molality = moles of solute  kg of solventSolutionYes, mo.pdfYes, molality = moles of solute  kg of solventSolutionYes, mo.pdf
Yes, molality = moles of solute kg of solventSolutionYes, mo.pdfinfo430661
 
When Laura left her job as a law firm manager to launch her own busi.pdf
When Laura left her job as a law firm manager to launch her own busi.pdfWhen Laura left her job as a law firm manager to launch her own busi.pdf
When Laura left her job as a law firm manager to launch her own busi.pdfinfo430661
 
WaterMolarity of pure water is 55.5 M. It is calculated as below..pdf
WaterMolarity of pure water is 55.5 M. It is calculated as below..pdfWaterMolarity of pure water is 55.5 M. It is calculated as below..pdf
WaterMolarity of pure water is 55.5 M. It is calculated as below..pdfinfo430661
 
The degree is 3 and the leading coefficient is -11.SolutionThe.pdf
The degree is 3 and the leading coefficient is -11.SolutionThe.pdfThe degree is 3 and the leading coefficient is -11.SolutionThe.pdf
The degree is 3 and the leading coefficient is -11.SolutionThe.pdfinfo430661
 
moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf
                     moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf                     moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf
moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdfinfo430661
 
Kc = [H2][I2][HI]^2 Kc = 1.84 x 10^-2 .pdf
                     Kc = [H2][I2][HI]^2  Kc = 1.84 x 10^-2          .pdf                     Kc = [H2][I2][HI]^2  Kc = 1.84 x 10^-2          .pdf
Kc = [H2][I2][HI]^2 Kc = 1.84 x 10^-2 .pdfinfo430661
 
Ionic bond 1.The atoms are bound together by the.pdf
                     Ionic bond 1.The atoms are bound together by the.pdf                     Ionic bond 1.The atoms are bound together by the.pdf
Ionic bond 1.The atoms are bound together by the.pdfinfo430661
 
Importance of Phosphorylated Intermediates Each o.pdf
                     Importance of Phosphorylated Intermediates Each o.pdf                     Importance of Phosphorylated Intermediates Each o.pdf
Importance of Phosphorylated Intermediates Each o.pdfinfo430661
 
Solution of the Questions with ExplanationPart AThe Domain Name.pdf
Solution of the Questions with ExplanationPart AThe Domain Name.pdfSolution of the Questions with ExplanationPart AThe Domain Name.pdf
Solution of the Questions with ExplanationPart AThe Domain Name.pdfinfo430661
 
Sexual selection is a type of natural selection. Sexual selection ac.pdf
Sexual selection is a type of natural selection. Sexual selection ac.pdfSexual selection is a type of natural selection. Sexual selection ac.pdf
Sexual selection is a type of natural selection. Sexual selection ac.pdfinfo430661
 
PPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdf
PPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdfPPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdf
PPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdfinfo430661
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfinfo430661
 
d. tyrosine (it acts as a nucleophile) .pdf
                     d. tyrosine (it acts as a nucleophile)           .pdf                     d. tyrosine (it acts as a nucleophile)           .pdf
d. tyrosine (it acts as a nucleophile) .pdfinfo430661
 
ORSolutionOR.pdf
ORSolutionOR.pdfORSolutionOR.pdf
ORSolutionOR.pdfinfo430661
 
It also leads to the loss of the privacy of the individual. If it is.pdf
It also leads to the loss of the privacy of the individual. If it is.pdfIt also leads to the loss of the privacy of the individual. If it is.pdf
It also leads to the loss of the privacy of the individual. If it is.pdfinfo430661
 
In pure water acidity or basness doesnt depend on tempratureReas.pdf
In pure water acidity or basness doesnt depend on tempratureReas.pdfIn pure water acidity or basness doesnt depend on tempratureReas.pdf
In pure water acidity or basness doesnt depend on tempratureReas.pdfinfo430661
 
I, alpha,II.SolutionI, alpha,II..pdf
I, alpha,II.SolutionI, alpha,II..pdfI, alpha,II.SolutionI, alpha,II..pdf
I, alpha,II.SolutionI, alpha,II..pdfinfo430661
 
Generally 12 cranial nerves are there. Among all these II optic cran.pdf
Generally 12 cranial nerves are there. Among all these II optic cran.pdfGenerally 12 cranial nerves are there. Among all these II optic cran.pdf
Generally 12 cranial nerves are there. Among all these II optic cran.pdfinfo430661
 
First, we need to forecast exchange rates for the next three years..pdf
First, we need to forecast exchange rates for the next three years..pdfFirst, we need to forecast exchange rates for the next three years..pdf
First, we need to forecast exchange rates for the next three years..pdfinfo430661
 
Family MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdf
Family  MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdfFamily  MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdf
Family MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdfinfo430661
 

Mehr von info430661 (20)

Yes, molality = moles of solute kg of solventSolutionYes, mo.pdf
Yes, molality = moles of solute  kg of solventSolutionYes, mo.pdfYes, molality = moles of solute  kg of solventSolutionYes, mo.pdf
Yes, molality = moles of solute kg of solventSolutionYes, mo.pdf
 
When Laura left her job as a law firm manager to launch her own busi.pdf
When Laura left her job as a law firm manager to launch her own busi.pdfWhen Laura left her job as a law firm manager to launch her own busi.pdf
When Laura left her job as a law firm manager to launch her own busi.pdf
 
WaterMolarity of pure water is 55.5 M. It is calculated as below..pdf
WaterMolarity of pure water is 55.5 M. It is calculated as below..pdfWaterMolarity of pure water is 55.5 M. It is calculated as below..pdf
WaterMolarity of pure water is 55.5 M. It is calculated as below..pdf
 
The degree is 3 and the leading coefficient is -11.SolutionThe.pdf
The degree is 3 and the leading coefficient is -11.SolutionThe.pdfThe degree is 3 and the leading coefficient is -11.SolutionThe.pdf
The degree is 3 and the leading coefficient is -11.SolutionThe.pdf
 
moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf
                     moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf                     moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf
moles of HCl = 500.1 = 5 moles of NaOH = 20.50..pdf
 
Kc = [H2][I2][HI]^2 Kc = 1.84 x 10^-2 .pdf
                     Kc = [H2][I2][HI]^2  Kc = 1.84 x 10^-2          .pdf                     Kc = [H2][I2][HI]^2  Kc = 1.84 x 10^-2          .pdf
Kc = [H2][I2][HI]^2 Kc = 1.84 x 10^-2 .pdf
 
Ionic bond 1.The atoms are bound together by the.pdf
                     Ionic bond 1.The atoms are bound together by the.pdf                     Ionic bond 1.The atoms are bound together by the.pdf
Ionic bond 1.The atoms are bound together by the.pdf
 
Importance of Phosphorylated Intermediates Each o.pdf
                     Importance of Phosphorylated Intermediates Each o.pdf                     Importance of Phosphorylated Intermediates Each o.pdf
Importance of Phosphorylated Intermediates Each o.pdf
 
Solution of the Questions with ExplanationPart AThe Domain Name.pdf
Solution of the Questions with ExplanationPart AThe Domain Name.pdfSolution of the Questions with ExplanationPart AThe Domain Name.pdf
Solution of the Questions with ExplanationPart AThe Domain Name.pdf
 
Sexual selection is a type of natural selection. Sexual selection ac.pdf
Sexual selection is a type of natural selection. Sexual selection ac.pdfSexual selection is a type of natural selection. Sexual selection ac.pdf
Sexual selection is a type of natural selection. Sexual selection ac.pdf
 
PPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdf
PPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdfPPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdf
PPSS x ppss is a dihybrid cross. P, p, S and s are alleles for corn .pdf
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
 
d. tyrosine (it acts as a nucleophile) .pdf
                     d. tyrosine (it acts as a nucleophile)           .pdf                     d. tyrosine (it acts as a nucleophile)           .pdf
d. tyrosine (it acts as a nucleophile) .pdf
 
ORSolutionOR.pdf
ORSolutionOR.pdfORSolutionOR.pdf
ORSolutionOR.pdf
 
It also leads to the loss of the privacy of the individual. If it is.pdf
It also leads to the loss of the privacy of the individual. If it is.pdfIt also leads to the loss of the privacy of the individual. If it is.pdf
It also leads to the loss of the privacy of the individual. If it is.pdf
 
In pure water acidity or basness doesnt depend on tempratureReas.pdf
In pure water acidity or basness doesnt depend on tempratureReas.pdfIn pure water acidity or basness doesnt depend on tempratureReas.pdf
In pure water acidity or basness doesnt depend on tempratureReas.pdf
 
I, alpha,II.SolutionI, alpha,II..pdf
I, alpha,II.SolutionI, alpha,II..pdfI, alpha,II.SolutionI, alpha,II..pdf
I, alpha,II.SolutionI, alpha,II..pdf
 
Generally 12 cranial nerves are there. Among all these II optic cran.pdf
Generally 12 cranial nerves are there. Among all these II optic cran.pdfGenerally 12 cranial nerves are there. Among all these II optic cran.pdf
Generally 12 cranial nerves are there. Among all these II optic cran.pdf
 
First, we need to forecast exchange rates for the next three years..pdf
First, we need to forecast exchange rates for the next three years..pdfFirst, we need to forecast exchange rates for the next three years..pdf
First, we need to forecast exchange rates for the next three years..pdf
 
Family MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdf
Family  MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdfFamily  MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdf
Family MyrtaceaeGenus EucalyptusSpecies globulusAuthority .pdf
 

Kürzlich hochgeladen

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Kürzlich hochgeladen (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

Given below is the completed implementation of MyLinkedList class. O.pdf

  • 1. Given below is the completed implementation of MyLinkedList class. Other classes are not modified and remain same as in question - MyList, MyAbstractList, TestMyLinkedList. The output of TestMyLinkedList is shown below. Please don't forget to rate the answer if it helped. Thank you very much. Since the MyList is not completely pasted in the question ... giving the complete class file below MyList.java public interface MyList> extends java.lang.Iterable { // Add a new element at the end of this list public void add (E e); // Add a new element at specified index in this list public void add (int index, E e); // Return true if this list contains the element public boolean contains (E e); // Return the element at the specified index public E get (int index); // Return the index of the first matching element // Return -1 if no match. public int indexOf (E e); // Return the index of the last matching element // Return -1 if no match. public int lastIndexOf (E e); E remove(int index); void clear(); E set(int index, E e); } MyLinkedList.java public class MyLinkedList> extends MyAbstractList implements Comparable> { private Node head, tail; // head and tail pointers // Create a default list public MyLinkedList() { }
  • 2. public int size() { return size; } // Need this for implementing Comparable public int compareTo (MyLinkedList e) { return(size() - e.size()); } // Create a list from an array of objects public MyLinkedList (E[] objects) { super(objects); // Passes the array up to abstract parent } // Return the head element in the list public E getFirst() { if (size == 0) { return null; } else { return head.element; } } // Return the last element in the list public E getLast() { if (size == 0) { return null; } else { return tail.element;
  • 3. } } // Add an element to the beginning of the list public void addFirst (E e) { // Create a new node for element e Node newNode = new Node(e); newNode.next = head; // link the new node with the head head = newNode; // head points to the new node size++; // Increase list size if (tail == null) // new node is only node { tail = head; } } // Add an element to the end of the list public void addLast (E e) { // Create a new node for element e Node newNode = new Node(e); if (tail == null) { head = tail = newNode; // new node is only node } else { tail.next = newNode; // Link the new with the last node tail = tail.next; // tail now points to the last node } size++; // Increase size } // Remove the head node and // return the object from the removed node public E removeFirst() { if (size == 0)
  • 4. { return null; } else { Node temp = head; head = head.next; size--; if (head == null) { tail = null; } return temp.element; } } // Remove the last node and return the object from it public E removeLast() { if (size == 0) { return null; } else if (size == 1) { Node temp = head; head = tail = null; size = 0; return temp.element; } else { Node current = head; for (int i = 0; i < size - 2; i++) { current = current.next; }
  • 5. Node temp = tail; tail = current; tail.next = null; size--; return temp.element; } } // Remove the element at the specified position // Return the element that was removed from the list. @Override public E remove (int index) { if (index < 0 || index >= size) { return null; } else if (index == 0) { return removeFirst(); } else if (index == size - 1) { return removeLast(); } else { Node previous = head; for (int i = 1; i < index; i++) { previous = previous.next; } Node current = previous.next; previous.next = current.next; size--; return current.element; }
  • 6. } // Return a String representation of the linked list elements @Override public String toString() { StringBuilder result = new StringBuilder("["); Node current = head; for (int i = 0; i < size; i++) { result.append (current.element); current = current.next; if (current != null) { result.append (", "); // Separate elements with a comma } else { result.append ("]"); // Insert the ] in the string } } return result.toString(); } // Clear the list @Override public void clear() { size = 0; head = tail = null; } // Add a new element at specified index in this list @Override public void add(int index, E e) { if (index == 0) {
  • 7. addFirst (e); } else if (index == size) { addLast (e); } else { checkIndex (index); // Create a new node for element e Node newNode = new Node(e); Node previous = head; for (int i = 1; i < index; i++) { previous = previous.next; } newNode.next = previous.next; previous.next = newNode; size++; // Increase size } } // Return true if this list contains the element e @Override public boolean contains(E e) { return indexOf(e) != -1; } // Return the element at the specified index @Override public E get (int index) { Node current = head; for(int i = 0; current != null && i < index; i++) current = current.next; if(current == null)
  • 8. return null; else return current.element; } // Return the index of the first matching element // Return -1 if no match @Override public int indexOf (E e) { Node current = head; for(int i = 0; i < size; i++) { if(current.element.equals(e)) return i; current = current.next; } return -1; } // Return the index of the last matching element // Return -1 if no match. @Override public int lastIndexOf (E e) { int lastIndex = -1; Node current = head; for (int i = 0; i < size; i++) { if (e.equals (current.element)) { lastIndex = i; } current = current.next; } return lastIndex; } // Replace the element at the specified position
  • 9. // with the specified element @Override public E set (int index, E e) { Node current = head; for (int i = 0; i < index; i++) { current = current.next; } E old = current.element; current.element = e; return old; } public boolean remove(E e) { if (head == null) return false; else if(head.element.equals(e)) { removeFirst(); return true; } else if(tail.element.equals(e)) { removeLast(); return true; } else { Node previous = head; while(previous != tail) { if(previous.next.element.equals(e)) { previous.next = previous.next.next; return true; }
  • 10. previous = previous.next; } return false; } } // Override iterator() defined in Iterable @Override public java.util.Iterator iterator() { return new LinkedListIterator(); } // Throws IndexOutOfBoundsException, if needed private void checkIndex (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); } } // The Node class private static class Node { E element; Node next; public Node (E element) { this.element = element; } } // Private iterator class for myArrayList class private class LinkedListIterator implements java.util.Iterator { private Node current = head; // Current index @Override public boolean hasNext()
  • 11. { return(current != null); } @Override public E next() { E e = current.element; current = current.next; return e; } @Override public void remove() { MyLinkedList.this.remove(current.element); } } } output (1) [America] (2) [Canada, America] (3) [Canada, America, Russia] (4) [Canada, America, Russia, France] (5) [Canada, America, Germany, Russia, France] (6) [Canada, America, Germany, Russia, France, Norway] (7) [Poland, America, Germany, Russia, France, Norway] (8) [America, Germany, Russia, France, Norway] (9) [America, Germany, France, Norway] (10) [America, Germany, France] (11) AMERICA GERMANY FRANCE (12) list.contains(America): true (13) list.contains(France) : true (14) list.contains(Germany): true (15) list.contains(Poland) : false (16) [France, America, Germany, France]
  • 12. (17) [France, America, Germany, France, Germany] (18) Index of first France: 0 (19) Index of last France: 3 (20) Index of last Germany: 4 (21) Index of first Germany: 2 (22) [America, Germany, France, Germany] (23) [America, Germany, France] (24) America Germany France After clearing the list, the list size is 0 Solution Given below is the completed implementation of MyLinkedList class. Other classes are not modified and remain same as in question - MyList, MyAbstractList, TestMyLinkedList. The output of TestMyLinkedList is shown below. Please don't forget to rate the answer if it helped. Thank you very much. Since the MyList is not completely pasted in the question ... giving the complete class file below MyList.java public interface MyList> extends java.lang.Iterable { // Add a new element at the end of this list public void add (E e); // Add a new element at specified index in this list public void add (int index, E e); // Return true if this list contains the element public boolean contains (E e); // Return the element at the specified index public E get (int index); // Return the index of the first matching element // Return -1 if no match. public int indexOf (E e); // Return the index of the last matching element // Return -1 if no match. public int lastIndexOf (E e); E remove(int index);
  • 13. void clear(); E set(int index, E e); } MyLinkedList.java public class MyLinkedList> extends MyAbstractList implements Comparable> { private Node head, tail; // head and tail pointers // Create a default list public MyLinkedList() { } public int size() { return size; } // Need this for implementing Comparable public int compareTo (MyLinkedList e) { return(size() - e.size()); } // Create a list from an array of objects public MyLinkedList (E[] objects) { super(objects); // Passes the array up to abstract parent } // Return the head element in the list public E getFirst() { if (size == 0) { return null; } else { return head.element;
  • 14. } } // Return the last element in the list public E getLast() { if (size == 0) { return null; } else { return tail.element; } } // Add an element to the beginning of the list public void addFirst (E e) { // Create a new node for element e Node newNode = new Node(e); newNode.next = head; // link the new node with the head head = newNode; // head points to the new node size++; // Increase list size if (tail == null) // new node is only node { tail = head; } } // Add an element to the end of the list public void addLast (E e) { // Create a new node for element e Node newNode = new Node(e); if (tail == null) { head = tail = newNode; // new node is only node }
  • 15. else { tail.next = newNode; // Link the new with the last node tail = tail.next; // tail now points to the last node } size++; // Increase size } // Remove the head node and // return the object from the removed node public E removeFirst() { if (size == 0) { return null; } else { Node temp = head; head = head.next; size--; if (head == null) { tail = null; } return temp.element; } } // Remove the last node and return the object from it public E removeLast() { if (size == 0) { return null; } else if (size == 1) {
  • 16. Node temp = head; head = tail = null; size = 0; return temp.element; } else { Node current = head; for (int i = 0; i < size - 2; i++) { current = current.next; } Node temp = tail; tail = current; tail.next = null; size--; return temp.element; } } // Remove the element at the specified position // Return the element that was removed from the list. @Override public E remove (int index) { if (index < 0 || index >= size) { return null; } else if (index == 0) { return removeFirst(); } else if (index == size - 1) { return removeLast(); }
  • 17. else { Node previous = head; for (int i = 1; i < index; i++) { previous = previous.next; } Node current = previous.next; previous.next = current.next; size--; return current.element; } } // Return a String representation of the linked list elements @Override public String toString() { StringBuilder result = new StringBuilder("["); Node current = head; for (int i = 0; i < size; i++) { result.append (current.element); current = current.next; if (current != null) { result.append (", "); // Separate elements with a comma } else { result.append ("]"); // Insert the ] in the string } } return result.toString(); } // Clear the list @Override
  • 18. public void clear() { size = 0; head = tail = null; } // Add a new element at specified index in this list @Override public void add(int index, E e) { if (index == 0) { addFirst (e); } else if (index == size) { addLast (e); } else { checkIndex (index); // Create a new node for element e Node newNode = new Node(e); Node previous = head; for (int i = 1; i < index; i++) { previous = previous.next; } newNode.next = previous.next; previous.next = newNode; size++; // Increase size } } // Return true if this list contains the element e @Override public boolean contains(E e)
  • 19. { return indexOf(e) != -1; } // Return the element at the specified index @Override public E get (int index) { Node current = head; for(int i = 0; current != null && i < index; i++) current = current.next; if(current == null) return null; else return current.element; } // Return the index of the first matching element // Return -1 if no match @Override public int indexOf (E e) { Node current = head; for(int i = 0; i < size; i++) { if(current.element.equals(e)) return i; current = current.next; } return -1; } // Return the index of the last matching element // Return -1 if no match. @Override public int lastIndexOf (E e) { int lastIndex = -1;
  • 20. Node current = head; for (int i = 0; i < size; i++) { if (e.equals (current.element)) { lastIndex = i; } current = current.next; } return lastIndex; } // Replace the element at the specified position // with the specified element @Override public E set (int index, E e) { Node current = head; for (int i = 0; i < index; i++) { current = current.next; } E old = current.element; current.element = e; return old; } public boolean remove(E e) { if (head == null) return false; else if(head.element.equals(e)) { removeFirst(); return true; } else if(tail.element.equals(e)) { removeLast();
  • 21. return true; } else { Node previous = head; while(previous != tail) { if(previous.next.element.equals(e)) { previous.next = previous.next.next; return true; } previous = previous.next; } return false; } } // Override iterator() defined in Iterable @Override public java.util.Iterator iterator() { return new LinkedListIterator(); } // Throws IndexOutOfBoundsException, if needed private void checkIndex (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); } } // The Node class private static class Node { E element;
  • 22. Node next; public Node (E element) { this.element = element; } } // Private iterator class for myArrayList class private class LinkedListIterator implements java.util.Iterator { private Node current = head; // Current index @Override public boolean hasNext() { return(current != null); } @Override public E next() { E e = current.element; current = current.next; return e; } @Override public void remove() { MyLinkedList.this.remove(current.element); } } } output (1) [America] (2) [Canada, America] (3) [Canada, America, Russia] (4) [Canada, America, Russia, France]
  • 23. (5) [Canada, America, Germany, Russia, France] (6) [Canada, America, Germany, Russia, France, Norway] (7) [Poland, America, Germany, Russia, France, Norway] (8) [America, Germany, Russia, France, Norway] (9) [America, Germany, France, Norway] (10) [America, Germany, France] (11) AMERICA GERMANY FRANCE (12) list.contains(America): true (13) list.contains(France) : true (14) list.contains(Germany): true (15) list.contains(Poland) : false (16) [France, America, Germany, France] (17) [France, America, Germany, France, Germany] (18) Index of first France: 0 (19) Index of last France: 3 (20) Index of last Germany: 4 (21) Index of first Germany: 2 (22) [America, Germany, France, Germany] (23) [America, Germany, France] (24) America Germany France After clearing the list, the list size is 0