In: Computer Science
I have added the MyList.java all the way on the bottom.
Thats all the detail I have for this and what kind of more detail information you need.
Plz Write the program in Java. And Plz post the running program with main and test.Thanks.
Implementing Lists:
Start by carefully reading Listing 24.5: MyLinkedList.java (on page 938 of the 11th Edition of the text). Note that the listing is incomplete. Your assignment is to implement a revised MyLinkedList class after you have included all the code needed to fill in and complete all the methods that were omitted. Next, write a (main) driver program that initializes a linked list with 10 names (your choice), and then completely tests every one of its methods of ensure that the class meets all its requirements.
Listing 24.5
MyLinkedList.java
1 public class MyLinkedList implements MyList {
2 private Node head, tail;
3 private int size = 0; // Number of elements in the list
4
5 /** Create an empty list */
6 public MyLinkedList() {
7 }
8
9 /** Create a list from an array of objects */
10 public MyLinkedList(E[] objects) {
11 for (int i = 0; i < objects.length; i++)
12 add(objects[i]);
13 }
14
15 /** Return the head element in the list */
16 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 */
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 */
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 */
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 */
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.
*/
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.
*/
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.
*/
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
*/
70 public String toString() {
71 StringBuilder result = new StringBuilder("[");
72
73 Node current = head;
74 for (int i = 0; i < size; i++) {
75 result.append(current.element);
76 current = current.next;
77 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 */
89 public void clear() {
90 size = 0;
91 head = tail = null;
92 }
93
94 @Override /** Return true if this list contains the element e
*/
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
*/
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. */
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. */
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. */
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 */
128 public java.util.Iterator iterator() {
129 return new LinkedListIterator();
130 }
131
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
154 private static class Node {
155 E element;
156 Node next;
157
158 public Node(E element) {
159 this.element = element;
160 }
161 }
162 }
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 the specified index in this list public void add(int index, E e); //Clear the list public void clear(); //Return true if this list contains the element public boolean contains(E e); //Return the element from this list at the specified index public E get(int index); //Return the index of the first matching element in this list. //Return -1 if no match. public int indexOf(E e); /** Return true if this list contains no elements */ public boolean isEmpty(); /** Return the index of the last matching element in this list * Return -1 if no match. */ public int lastIndexOf(E e); /** Remove the first occurrence of the element o from this list. * Shift any subsequent elements to the left. * Return true if the element is removed. */ public boolean remove(E e); /** Remove the element at the specified position in this list * Shift any subsequent elements to the left. * Return the element that was removed from the list. */ public E remove(int index); /** Replace the element at the specified position in this list * with the specified element and returns the new set. * */ public Object set(int index, E e); /** Return the number of elements in this list */ public int size(); }
public class MyLinkedList<E> implements MyList<E> {
private Node<E> head, tail;
private int size = 0; // Number of elements in the
list
/** Create an empty list */
public MyLinkedList() {
head = null;
tail = null;
}
/** Create a list from an array of objects */
public MyLinkedList(E[] objects) {
for (int i = 0; i <
objects.length; i++)
add(objects[i]);
}
/** 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) {
// Implemented in Section 24.4.3.1,
so omitted here
}
/** Add an element to the end of the list */
public void addLast(E e) {
// Implemented in Section 24.4.3.2,
so omitted here
}
@Override /** Add a new element at the specified
index
* in this list. The index of the head element is 0
*/
public void add(int index, E e) {
// Implemented in Section 24.4.3.3,
so omitted here
}
/** Remove the head node and
* return the object that is contained in the removed
node. */
public E removeFirst() {
// Implemented in Section 24.4.3.4,
so omitted here
return null;
}
/** Remove the last node and
* return the object that is contained in the removed
node. */
public E removeLast() {
// Implemented in Section 24.4.3.5,
so omitted here
return null;
}
@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) {
//Implemented earlier in Section
24.4.3.6, so omitted
return null;
}
@Override /** Override toString() to return elements
in the list */
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(Object e) {
Node<E> curr = head;
while(curr != null)
{
if(curr.element.equals(e))
return true;
curr =
curr.next;
}
return false;
}
@Override /** Return the element at the specified
index */
public E get(int index) {
if(index >=0 && index
< size)
{
int i=0;
Node<E>
curr = head;
while(i <
index)
{
curr = curr.next;
i++;
}
return
curr.element;
}
return null;
}
@Override /** Return the index of the head matching
element in
*this list. Return -1 if no match. */
public int indexOf(Object e) {
int index = 0;
Node<E> curr = head;
while(curr != null)
{
if(curr.element.equals(e))
return index;
curr =
curr.next;
index++;
}
return -1;
}
@Override /** Return the index of the last matching
element in
this list. Return -1 if no match. */
public int lastIndexOf(E e) {
int index = 0;
int lastIndex = -1;
Node<E> curr = head;
while(curr != null)
{
if(curr.element.equals(e))
lastIndex = index;
curr =
curr.next;
index++;
}
return lastIndex;
}
@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)
{
int i = 0;
Node<E>
curr = head;
while(i <
index)
{
curr = curr.next;
i++;
}
curr.element =
e;
return
curr.element;
}
return null;
}
@Override /** Override iterator() defined in Iterable
*/
public java.util.Iterator iterator() {
return new
LinkedListIterator();
}
private class LinkedListIterator implements
java.util.Iterator {
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() {
// check current
is not null
if(current !=
null)
{
// if head is the current node
if(head == current)
{
head = head.next; // set head
to node next to head
size--; // decrement the
size
// if now the list is empty,
set tail to null
if(head == null)
tail =
null;
current = head; //set current
to new head
}else
{
Node<E> prev =
head;
// loop to find the node
previous to current
while(prev.next !=
current)
{
prev =
prev.next;
}
// set next of previous to
node next of current
prev.next =
current.next;
current = current.next; //
set current to node next of current
size--;
// if current was the last
node, reset the tail
if(prev.next == null)
tail =
prev;
}
}
}
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element =
element;
}
}
}
//end of MyLinkedList.java
Since code for some of the functions already implemented in other exercise are not available. so the main method for testing cannot be written. Please provide those codes or details for those functions.