Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

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 next; Node previous; publie Node (E el element = e: Implement a new class named TwoWayLinkedlist that uses a doubly linked list to store elements. Define TwoWayLinkedList to implements MyList. You need to implement all the methods defined in MyLinkedlist as well as the methods listIterator() and listIterator (int Index). Both return an instance of Java.util.Listi We've updated our read aloud featurel 3. The former sets the cursor to the head of the list and the latter to the element at the specified Give it's try here! Listing 24.5 MyLinked List.java 1 public class MyLinkedList implements MyList private Node head, tail; head, tail number of elements private int size = 0; // Number of elements in the list 44 Create an empty list * public MyLinkedList 0 { no-arg construct constructor It create a 19h from an array of objects public MyLinkedlist (eti objects) for (int HD; objects.length; x++) add objectID: tienent the get First public E get First rn null get Last 1 null; 35 addFirst 36 / Add an element to the beginning of the lat public void addFirst(e) 1 Implemented in Section 24.4.3.10.30 omitted here 7** Add an element to the end of the list - adabast public void addlast(e) { // Implemented in Section 24.4.3.20.30 omitted here 1 43 45 46 47 46 49 @Override./ Add a new element at the specified index in this list. The index of the head element 10 0 +7 public void add(int index, E e) { Implemented in Section 24.4.3.3 D. 30 omitted here 51 7 Remove the head node and return the object that is contained in the removed node. public EremoveFirst() { 1 Implemented in Section 24.4.3.4 U. 30 omitted here remove Pirat 53 54 55 57 Remove the last node and 58 - return the object that is contained in the removed node. clear @Override / clear the lat public void clear() Bize = 0; head = tail = null; 91 92 @Override / Return true if this list contains the contains 95 public boolean contains (Object e)! // Left as an exercise return true; @Override / Return the element at the specified index 100 101 get public Eget (int index)! // Left as an exercise return null; 104 ) @Override / Return the index of the head matching element in this list. Return -1 1 no match./ public int indexOf(object e) { // Left as an exercise indexof 107 108 109 110 111 112 return 0; Goverride /* Return the index of the last matching element in - this list. Return -1 if no match. */ 114 115 LastIndexof public int lastIndexOf( e) { 116 // Left as an exercise 117 return 0; 118 119 120 121 @Override /** Replace the element at the speed in this list with the specified element set public E set (int index, E e) { // Left as an exercise return null; 125 override /** Override iterator()' defined in Iterable iterator 127 128 129 130 public java.util. Iterator iterator() return new LinkedListIterator 131 LinkedListIterator class 132 private class LinkedListIterator 133 implements java.util.Iterator private Node current = head; // Current Index 134 135 136 137 Coverride public boolean hasNext() { return (current != null); 138 139 140 @Override 141 142 public E next() { Ee = current.element; 143 144 current = current.next; 145 return e; 146 147 148 149 @Override public void remove() { override Il public void remove teft a IS Node inner class 154 | private static class Node 155 E elemen Node next; public Node (E element) { this.element element; override / Return the number of elements in this list public int size () { return size

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions