Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please I need help with this java exercise 24.3. please show comments and show output. thank you very much. I took a picture and also
please I need help with this java exercise 24.3. please show comments and show output. thank you very much.
I took a picture and also copied the source code at the bottom. If the picture seemed hard to read.
I have added the list 24.5 in the following picture
below I have added the source code. hope that helps.
Listing 24.5MyLinkedList.java
1 public class MyLinkedList implements MyList { head, tail 2 private Node head, tail; number of elements 3 private int size = 0; // Number of elements in the list 4 5 /** Create an empty list */ no-arg constructor 6 public MyLinkedList() { 7 } 8 9 /** Create a list from an array of objects */ constructor 10 public MyLinkedList(E[] objects) { 11 for (int i = 0; i public E getFirst() { 17 if (size == 0) { 18 return null; 19 } 20 else { 21 return head.element; 22 } 23 } 24 25 /** Return the last element in the list */ getLast 26 public E getLast() { 27 if (size == 0) { 28 return null; 29 } 30 else { 31 return tail.element; 32 } 33 } 34 35 /** Add an element to the beginning of the list */ addFirst 36 public void addFirst(E e) { 37 // Implemented in Section 24.4.3.1, so omitted here 38 } 39 40 /** Add an element to the end of the list */ addLast 41 public void addLast(E e) { 42 // Implemented in Section 24.4.3.2, so omitted here 43 } 44 45 @Override /** Add a new element at the specified index 46 * in this list. The index of the head element is 0 */ add 47 public void add(int index, E e) { 48 // Implemented in Section 24.4.3.3, so omitted here 49 } 50 51 /** Remove the head node and 52 * return the object that is contained in the removed node. */ removeFirst 53 public E removeFirst() { 54 // Implemented in Section 24.4.3.4, so omitted here 55 } 56 57 /** Remove the last node and 58 * return the object that is contained in the removed node. */ removeLast 59 public E removeLast() { 60 // Implemented in Section 24.4.3.5, so omitted here 61 } 62 63 @Override /** Remove the element at the specified position in this 64 * list. Return the element that was removed from the list. */ remove 65 public E remove(int index) { 66 // Implemented earlier in Section 24.4.3.6, so omitted 67 } 68 69 @Override /** Override toString() to return elements in the list */ toString 70 public String toString() { 71 StringBuilder result = new StringBuilder("["); 72 73 Node current = head; 74 for (int i = 0; i if (current != null) { 78 result.append(", "); // Separate two elements with a comma 79 } 80 else { 81 result.append("]"); // Insert the closing ] in the string 82 } 83 } 84 85 return result.toString(); 86 } 87 88 @Override /** Clear the list */ clear 89 public void clear() { 90 size = 0; 91 head = tail = null; 92 } 93 94 @Override /** Return true if this list contains the element e */ contains 95 public boolean contains(Object e) { 96 // Left as an exercise 97 return true; 98 } 99 100 @Override /** Return the element at the specified index */ get 101 public E get(int index) { 102 // Left as an exercise 103 return null; 104 } 105 106 @Override /** Return the index of the head matching element in 107 * this list. Return 1 if no match. */ indexOf 108 public int indexOf(Object e) { 109 // Left as an exercise 110 return 0; 111 } 112 113 @Override /** Return the index of the last matching element in 114 * this list. Return 1 if no match. */ lastIndexOf 115 public int lastIndexOf(E e) { 116 // Left as an exercise 117 return 0; 118 } 119 120 @Override /** Replace the element at the specified position 121 * in this list with the specified element. */ set 122 public E set(int index, E e) { 123 // Left as an exercise 124 return null; 125 } 126 127 @Override /** Override iterator() defined in Iterable */ iterator 128 public java.util.Iterator iterator() { 129 return new LinkedListIterator(); 130 } 131 LinkedListIterator class 132 private class LinkedListIterator 133 implements java.util.Iterator { 134 private Node current = head; // Current index 135 136 @Override 137 public boolean hasNext() { 138 return (current != null); 139 } 140 141 @Override
142 public E next() { 143 E e = current.element; 144 current = current.next; 145 return e; 146 } 147 148 @Override 149 public void remove() { 150 // Left as an exercise 151 } 152 } 153 Node inner class 154 private static class Node { 155 E element; 156 Node next; 157 158 public Node(E element) { 159 this.element = element; 160 } 161 } 162 163 @Override /** Return the number of elements in this list */ 164 public int size() { 165 return size; 166 } 167 }*24.3 (Implement a doubly linked list) The MyLinkedlist class used in Listing 24.5 Ois a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows: public class ModeCE> E elementi Node
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started