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;
tai.
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