(Test MyLinkedList) Design and write a complete test program to test if the MyLi
ID: 3858776 • Letter: #
Question
(Test MyLinkedList) Design and write a complete test program to test if the MyLinkedList class in Listing 24.6 meets all requirements.
1 public class MyLinkedList<E> extends MyAbstractList<E> {
2 private Node<E> head, tail;
3
4 /** Create a default list */
5 public MyLinkedList() {
6 }
7
8 /** Create a list from an array of objects */
9 public MyLinkedList(E[] objects) {
10 super(objects);
11 }
12
13 /** Return the head element in the list */
14 public E getFirst() {
15 if (size == 0) {
16 return null;
17 }
18 else {
19 return head.element;
20 }
21 }
22
23 /** Return the last element in the list */
24 public E getLast() {
25 if (size == 0) {
26 return null;
27 }
28 else {
29 return tail.element;
30 }
31 }
32
33 /** Add an element to the beginning of the list */
34 public void addFirst(E e) {
35 // Implemented in Section 24.4.3.1, so omitted here
36 }
37
38 /** Add an element to the end of the list */
39 public void addLast(E e) {
40 // Implemented in Section 24.4.3.2, so omitted here
41 }
42
43 @Override /** Add a new element at the specified index
44 * in this list. The index of the head element is 0 */
45 public void add(int index, E e) {
46 // Implemented in Section 24.4.3.3, so omitted here
47 }
48
49 /** Remove the head node and
50 * return the object that is contained in the removed node. */
51 public E removeFirst() {
52 // Implemented in Section 24.4.3.4, so omitted here
/** Remove the last node and
56 * return the object that is contained in the removed node. */
57 public E removeLast() {
58 // Implemented in Section 24.4.3.5, so omitted here
59 }
60
61 @Override /** Remove the element at the specified position in this
62 * list. Return the element that was removed from the list. */
63 public E remove(int index) {
64 // Implemented earlier in Section 24.4.3.6, so omitted here
65 }
66
67 @Override
68 public String toString() {
69 StringBuilder result = new StringBuilder("[");
70
71 Node<E> current = head;
72 for (int i = 0; i < size; i++) {
73 result.append(current.element);
74 current = current.next;
75 if (current != null) {
76 result.append(", "); // Separate two elements with a comma
77 }
78 else {
79 result.append("]"); // Insert the closing ] in the string
80 }
81 }
82
83 return result.toString();
84 }
85
86 @Override /** Clear the list */
87 public void clear() {
88 size = 0;
89 head = tail = null;
90 }
91
92 @Override /** Return true if this list contains the element e */
93 public boolean contains(E e) {
94 System.out.println("Implementation left as an exercise");
95 return true;
96 }
97
98 @Override /** Return the element at the specified index */
99 public E get(int index) {
100 System.out.println("Implementation left as an exercise");
101 return null;
102 }
103
104 @Override /** Return the index of the head matching element
105 * in this list. Return -1 if no match. */
106 public int indexOf(E e) {
107 System.out.println("Implementation left as an exercise");
108 return 0;
109 }
110
111 @Override /** Return the index of the last matching element
112 * in this list. Return -1 if no match. */
113 public int lastIndexOf(E e) {
114 System.out.println("Implementation left as an exercise");
115 return 0;
116 }
117
118 @Override /** Replace the element at the specified position
119 * in this list with the specified element. */
120 public E set(int index, E e) {
121 System.out.println("Implementation left as an exercise");
122 return null;
123 }
124
125 @Override /** Override iterator() defined in Iterable */
126 public java.util.Iterator<E> iterator() {
127 return new LinkedListIterator();
128 }
129
130 private class LinkedListIterator
131 implements java.util.Iterator<E> {
132 private Node<E> current = head; // Current index
133
134 @Override
135 public boolean hasNext() {
136 return (current != null);
137 }
138
139 @Override
140 public E next() {
141 E e = current.element;
142 current = current.next;
143 return e;
144 }
145
146 @Override
147 public void remove() {
148 System.out.println("Implementation left as an exercise");
149 }
150 }
151
152 // This class is only used in LinkedList, so it is private.
153 // This class does not need to access any
154 // instance members of LinkedList, so it is defined static.
155 private static class Node<E> {
156 E element;
157 Node<E> next;
158
159 public Node(E element) {
160 this.element = element;
Explanation / Answer
The code is given below as per your requrement so please hit like button
MyLinkedList.java
public class MyLinkedList<E> extends MyAbstractList<E> {
private Node<E> head, tail;
/** Create a default list */
public MyLinkedList() {
}
/** Create a list from an array of objects */
public MyLinkedList(E[] objects) {
super(objects);
}
/** 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) {
Node<E> newNode = new Node<>(e); // Create a new node
newNode.next = head; // link the new node with the head
head = newNode; // head points to the new node
size++; // Increate list size
if (tail == null) // The new node is the only node in list
tail = head;
}
/** Add an element to the end of the list */
public void addLast(E e) {
Node<E> newNode = new Node<>(e); // Create a new node for e
if (tail == null) {
head = tail = newNode; // The on;y node in list
}
else {
tail.next = newNode; // Link the new node with the last node
tail = tail.next; // tail now points to the last node
}
size++; // Increase size
}
@Override /** Add a new element at the specified index
* in this list. The lndex of the head element is 0 */
public void add(int index, E e) {
if (index == 0) addFirst(e); // Insert first
else if (index >= size) addLast(e); // Insert last
else { // Insert in the middle
Node<E> current = head;
for (int i = 1; i < index; i++)
current = current.next;
Node<E> temp = current.next;
current.next = new Node<>(e);
(current.next).next = temp;
size++;
}
}
/** Remove the head node and
* return the object that is contained in the removed node. */
public E removeFirst() {
if (size == 0) return null; // Nothing to delete
else {
Node<E> temp = head; // keep the first node temporarily
head = head.next; // Move head to point to next node
size--; // Reduce size by 1
return temp.element; // Return the deleted element
}
}
/** Remove the last node and
* return the object that is contained in the removed node. */
public E removeLast() {
if (size == 0) return null; // Nothing to remove
else if (size == 1) { // Only one element in the list
Node<E> temp = head;
head = tail = null; // list becomes empty
size = 0;
return temp.element;
}
else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++)
current = current.next;
Node<E> temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
@Override /** Remove the element at the specified position in this
* list. Return the element that was removed from the list. */
public E remove(int index) {
if (index < 0 || index >= size) return null; // Out of range
else if (index == 0) return removeFirst(); // Remove first
else if (index == size - 1) return removeLast(); // Remove last
else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", "); // Separate two elements with a comma
}
else {
result.append("]"); // Insert the closing ] in the string
}
}
return result.toString();
}
@Override /** Clear the list */
public void clear() {
size = 0;
head = tail = null;
}
@Override /** Return true if this list contains the element e */
public boolean contains(E e) {
if (size == 0) return false;
else {
Node<E> current = head;
while (current != null) {
if (current.element == e)
return true;
current = current.next;
}
}
return false;
}
@Override /** Return the element at the specified index */
public E get(int index) {
if (index < 0 || index >= size) return null; // Out of range
else if (index == 0) return getFirst();
else if (index == size - 1) return getLast();
else {
Node<E> current = head.next;
for (int i = 1; i < index; i++)
current = current.next;
return current.element;
}
}
@Override /** Return the index of the head matching element
* in this list. Return -1 if no match. */
public int indexOf(E e) {
if (head.element == e) return 0;
else if (tail.element == e) return size - 1;
else {
Node<E> current = head.next;
int index = 1;
while (current != null) {
if (current.element == e)
return index;
current = current.next;
index++;
}
}
return -1;
}
@Override /** Return the index of the last matching element
* in this list. Rreturn -1 if on match. */
public int lastIndexOf(E e) {
int index = -1;
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (current.element == e)
index = i;
current = current.next;
}
return index;
}
@Override /** Replace the element at the specified position
* in this list with the specified element. */
public E set(int index, E e) {
if (index < 0 || index > size - 1) return null;
else {
Node<E> current = head;
for (int i = 0; i < index; i++)
current = current.next;
current.element = e;
return current.element;
}
}
@Override /** Override iterator() defined in Iterable */
public java.util.Iterator<E> iterator() {
return new LinkedListIterator();
}
public class LinkedListIterator
implements java.util.Iterator<E> {
private Node<E> 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() {
System.out.println("Implementation left as an exercise");
}
}
// This class is only used in LinkedList, so it is private.
// This class does not need to access any
// instance members of LinkedList, so it is defined static.
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
}
Hope this Helps please thumbs up.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.