Question

In: Computer Science

I have added the MyList.java all the way on the bottom. Thats all the detail I...

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();
}

Solutions

Expert Solution


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.


Related Solutions

Let’s say you push the block from the bottom of the plane all the way up...
Let’s say you push the block from the bottom of the plane all the way up to the top of the plane, then let it slide back down. 11-1. While you are pushing the block up the plane, is the work done by gravity positive or negative? Why? 11-2. While you are pushing the block up the plane, is the work done by friction positive or negative? Why? 11-3. As the block slides back down the plane, is the work...
I have two parts. Please explain all parts in detail. (1). Poor countries have lower capital...
I have two parts. Please explain all parts in detail. (1). Poor countries have lower capital stock levels compared to wealth countries. Therefore in order for a developement policy to be effective it must target the saving rate. (2). The prediction of solow growth model are inconsistent with the empirical evidence regarding income convergence.
I have to answer these questions in a way that I can write out in a...
I have to answer these questions in a way that I can write out in a word document. Previous answer make little sense. I need to interpret (A) and I am just not understanding what the y intercept of -23 is indicating. I understand question b, not 100% on C please see below:   A criminologist is interested in the effects of unemployment and policing on murder and has run the following multiple regression: Summary Output Regression Statistics Multiple R 0.90303...
Can I get all of the question answered in a way that I know under which...
Can I get all of the question answered in a way that I know under which sub question the answer goes please. Can I get all of the questions answered in a way that I know how it fits in the question. Some parts are explanations. Thank you. Q. When a pink aqueous solution of potassium permanganate, faintly acidified with dilute sulfuric acid was treated with 10% aq. hydrogen peroxide, the reaction took place with the evolution of gas bubbles,...
2. Describe in detail i)what information should be added in which DNS servers for your own...
2. Describe in detail i)what information should be added in which DNS servers for your own start-up company (say ‘nwguru.com’) that has a webserver and email service to its employees. ii) What are companies you can contact for domain name registration and how much are the fees?
Please answer in detail. Management Issues for Non-Depository Institutions I have completed all the necessary ratios/calculations....
Please answer in detail. Management Issues for Non-Depository Institutions I have completed all the necessary ratios/calculations. I need help with answering the questions. A Insurance Company has the following financial statements. 2017 2016 Net Premiums Written 48,612 47,398 Income Statement ($ mils.) Premiums Earned 42,624 48,321 Loss Expenses 30,746 34,364 Operating Expenses 17,720 17,693 Total Policy Expenses 48,466 52,057 Net Underwriting Gain/Loss -5,842 -3,736 42,624 48,321 Net Investment Income 15,700 19,995 Operating Income before taxes 9,858 16,259 Dividends to Policyholders...
I want to arrange my books in a self in a certain way. Suppose I have...
I want to arrange my books in a self in a certain way. Suppose I have 30 books (distinct), and of these books, 10 are statistics books, 5 are forest ecology books, 4 are forest genetics, 3 are soil biology and 8 are economics books. I want to keep the books in the same subjects together. How many ways can I arrange these 30 books? Hint 1: Books in the same subject are not the same book. For example, ten...
in this code I have used array two times . I need a way to make...
in this code I have used array two times . I need a way to make the program functional using a string to store the results and print it again later . this game should be array free. import java.util.Scanner; import java.util.Random;//starter code provided public class inputLap { public static char roller; public static String playerName; public static int printed=0; public static int rounds=8,lives=0,randy; public static int tries[]=new int[4];//use arrays to store number of tries in each life public static...
summary of chapter 7 in thats the joint hip hop studies (i use to love H.E.R.)...
summary of chapter 7 in thats the joint hip hop studies (i use to love H.E.R.) 300 words (original)
Sorry, this is all the info I have on this problem, I have. If you cannot...
Sorry, this is all the info I have on this problem, I have. If you cannot do it please cancel/refund the question so we both can save time :) 5. A type of order that becomes a market order when a round lot trades at or through a particular price is called a a. market order b. limit order c. stop order d. none of the above 6. A customer's initial transaction in a margin account is the purchase of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT