Question

In: Computer Science

******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...

******IN JAVA********

I need the following interface implemented accordingly. It is a linked list. The interface can be found below:

List.java

public interface List<T> extends Iterable<T> {
    /**
     * Insert an element at a specified location.
     * @param index
     * @param obj
     * @throws IndexOutOfBoundsException
     */
    public void add(int index, T obj);
    
    /**
     * Append an object to the end of the list.
     * @param obj
     */
    public boolean add(T obj);
    
    public void clear();
    public boolean contains(T obj);
    
    /**
     * If obj is in the list, return the 
     * index of the first occurrence.
     * Otherwise, return -1.
     * @param obj
     * @return 
     */
    public int indexOf(T obj);
    
    public boolean isEmpty();
    
    public int lastIndexOf(T obj);
    
    /**
     * Get and return the value stored at the index.
     * 
     * @param index
     * @return
     * @throws IndexOutOfBoundsException
     */
    public T get(int index);
        
    public T remove(int index);
    public boolean remove(T obj);
    
    /**
     * Update the value in the list at the specified index.
     * Return the old value
     * @throws IndexOutOfBoundsException
     * @param index
     * @param obj
     * @return 
     */
    public T set(int index, T obj);
    
    public int size();
    
    public Object[] toArray();
}

I also need the following driver modified so that it works with the implementation:

ListDriver.java

import java.util.Iterator;


public class ListDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int i = 0;
        List<String> names = new AList<>(5);
        names.add("Alice");
        names.add("Bob");
        names.add("Carol");
        names.add(1, "Eve");
        names.add("Eve");
        for (String name : names)
            System.out.println((i++) + ":" + name);
        i=0;
        System.out.println("Size (should be 5): " + names.size());
        System.out.println("IndexOf(Eve) (should be 1): " + names.indexOf("Eve"));
        System.out.println("LastIndexOf(Eve) (should be 4): " + names.lastIndexOf("Eve"));
        System.out.println("Remove Eve (should be true):" + names.remove("Eve"));
        System.out.println("Size (should be 4): " + names.size());
        System.out.println("IndexOf(Eve) (should be 3): " + names.indexOf("Eve"));
        System.out.println("LastIndexOf(Eve) (should be 3): " + names.lastIndexOf("Eve"));
        for (String name : names)
            System.out.println((i++) + ":" + name);
        i=0;
        System.out.println("Remove Eve (should be true):" + names.remove("Eve"));
        System.out.println("Size (should be 3): " + names.size());
        System.out.println("IndexOf(Eve) (should be -1): " + names.indexOf("Eve"));
        System.out.println("LastIndexOf(Eve) (should be -1): " + names.lastIndexOf("Eve"));
        System.out.println("Size (should be 3): " + names.size());
        System.out.println("Remove 0 (should be Alice): " + names.remove(0));
        System.out.println("Size (should be 2): " + names.size());
        names.add(0, "Alice");
        names.add(1, "Eve");
        names.add("Eve");
        names.add(1, "Eve");
        names.add("Eve");
        names.add(1, "Eve");
        names.add(names.indexOf("Carol"), "Eve");
        names.add(0, "Eve");
        names.add("Eve");
        System.out.println("Size: " + names.size());
        for (String name : names)
            System.out.println((i++) + ":" + name);
        i=0;
        System.out.println("Remove all instances of Eve using iterator... ");
        Iterator<String> it = names.iterator();
        while(it.hasNext()) {
            if (it.next().equals("Eve"))
                it.remove();
        }
        for (String name : names)
            System.out.println((i++) + ":" + name);
        i=0;
        System.out.println("Testing clear");
        names.clear();
        System.out.println("Size (should be 0): " + names.size());
        
    }
    
}

Thanks in advance. Will upvote!

Solutions

Expert Solution

// List.java
public interface List<T> extends Iterable<T> {
  
   /**
* Insert an element at a specified location.
* @param index
* @param obj
* @throws IndexOutOfBoundsException
*/
public void add(int index, T obj);
  
/**
* Append an object to the end of the list.
* @param obj
*/
public boolean add(T obj);
  
public void clear();
public boolean contains(T obj);
  
/**
* If obj is in the list, return the
* index of the first occurrence.
* Otherwise, return -1.
* @param obj
* @return
*/
public int indexOf(T obj);
  
public boolean isEmpty();
  
public int lastIndexOf(T obj);
  
/**
* Get and return the value stored at the index.
*
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
public T get(int index);
  
public T remove(int index);
public boolean remove(T obj);
  
/**
* Update the value in the list at the specified index.
* Return the old value
* @throws IndexOutOfBoundsException
* @param index
* @param obj
* @return
*/
public T set(int index, T obj);
  
public int size();
  
public Object[] toArray();

}

//end of List.java

// Node.java : Java class to represent the Node of the linked list
public class Node<T>
{
   private T data;
   private Node<T> next;
  
   public Node(T data)
   {
       this.data = data;
       this.next = null;
   }
  
   public void setData(T data)
   {
       this.data = data;
   }
  
   public void setNext(Node<T> next)
   {
       this.next = next;
   }
  
   public T getData()
   {
       return data;
   }
  
   public Node<T> getNext()
   {
       return next;
   }
}

//end of Node.java

// AList.java : Java program to implement the List as linked list
import java.util.Iterator;

public class AList<T> implements List<T>{
  
  
   private Node<T> head;
   private int size ;
  
   public AList(int d)
   {
       head = null;
       size = 0;
   }

   @Override
   public Iterator<T> iterator() {
       return new ListIterator();
   }

   @Override
   public void add(int index, T obj) {
       if(index >=0 && index <= size())
       {
           Node<T> node = new Node<T>(obj);
           if(index == size())
               add(obj);
           else if(index == 0)
           {
               node.setNext(head);
               head = node;
           }else
           {
               int i = 0;
               Node<T> curr = head;
              
               while(i < index-1)
               {
                   curr = curr.getNext();
                   i++;
               }
              
               node.setNext(curr.getNext());
               curr.setNext(node);
           }
           size++;
       }else
           throw new IndexOutOfBoundsException();
   }

   @Override
   public boolean add(T obj) {
       Node<T> node = new Node<T>(obj);
       if(head == null)
           head = node;
       else
       {
           Node<T> curr = head;
           while(curr.getNext() != null)
               curr = curr.getNext();
           curr.setNext(node);
       }
       size++;
       return true;
   }

   @Override
   public void clear() {
       head = null;
       size = 0;
   }

   @Override
   public boolean contains(T obj) {
       Node<T> curr = head;
      
       while(curr != null)
       {
           if(curr.getData().equals(obj))
               return true;
           curr = curr.getNext();
       }
      
       return false;
   }

   @Override
   public int indexOf(T obj) {
       int i=0;
       Node<T> curr = head;
       while(curr != null)
       {
           if(curr.getData().equals(obj))
               return i;
           curr = curr.getNext();
           i++;
       }
      
       return -1;
   }

   @Override
   public boolean isEmpty() {
       return(head == null);
   }

   @Override
   public int lastIndexOf(T obj) {
       int index = -1;
       int i=0;
      
       Node<T> curr = head;
      
       while(curr != null)
       {
           if(curr.getData().equals(obj))
               index = i;
           i++;
           curr = curr.getNext();
       }
      
       return index;
   }

   @Override
   public T get(int index) {
      
       if(index >=0 && index < size())
       {
           int i=0;
           Node<T> curr = head;
           while((curr != null) && (i < index))
           {
               curr = curr.getNext();
               i++;
           }
          
           return curr.getData();
       }else
           throw new IndexOutOfBoundsException();
   }

   @Override
   public T remove(int index) {
       if(index >=0 && index < size())
       {
           int i=0;
           Node<T> curr = head;
           Node<T> prev = null;
           while((curr != null) && (i < index))
           {
               prev = curr;
               curr = curr.getNext();
               i++;
           }
          
           if(prev == null)
               head = curr.getNext();
           else
               prev.setNext(curr.getNext());
           size--;
           return curr.getData();
       }else
           throw new IndexOutOfBoundsException();
   }

   @Override
   public boolean remove(T obj) {
       Node<T> curr = head;
       Node<T> prev = null;
      
       while(curr != null)
       {
           if(curr.getData().equals(obj))
           {
               if(prev == null)
                   head = curr.getNext();
               else
                   prev.setNext(curr.getNext());
               size--;
               return true;
           }
           prev = curr;
           curr = curr.getNext();
       }
       return false;
   }

   @Override
   public T set(int index, T obj) {
       if(index >=0 && index < size())
       {
           int i = 0;
           Node<T> curr = head;
          
           while((curr != null) && (i < index))
           {
               i++;
               curr = curr.getNext();
           }
          
           T elem = curr.getData();
           curr.setData(obj);
           return elem;
       }else
           throw new IndexOutOfBoundsException();
   }

   @Override
   public int size() {
       return size;
   }

   @Override
   public Object[] toArray() {
      
       if(size() > 0)
       {
           Object list[] = new Object[size()];
           Node<T> curr = head;
           int i=0;
           while(curr != null)
           {
               list[i] = curr.getData();
               i++;
               curr = curr.getNext();
           }
          
           return list;
       }
      
       return null;
   }

   private class ListIterator implements Iterator<T>
   {
      
       private Node<T> curr;
       private Node<T> prev;
      
       public ListIterator()
       {
           curr = null;
           prev = null;
       }
      
       @Override
       public boolean hasNext() {
           if(curr == null)
           {
               if(head == null)
                   return false;
               else
                   return true;
           }else
           {
               return(curr.getNext() != null);
           }
       }

       @Override
       public T next() {
           prev = curr;
           if(curr == null)
               curr = head;
           else
               curr = curr.getNext();
           return curr.getData();
       }
      
       public void remove()
       {
           if(prev != null)
           {
               prev.setNext(curr.getNext());
               curr = curr.getNext();
           }else
           {
               head = curr.getNext();
           }
           size--;
       }
      
   }
}
//end of AList.java

// ListDriver.java : Java program to implement AList class
import java.util.Iterator;


public class ListDriver {
  
   /**
* @param args the command line arguments
*/
public static void main(String[] args) {
int i = 0;
List<String> names = new AList<>(5);
names.add("Alice");
names.add("Bob");
names.add("Carol");
names.add(1, "Eve");
names.add("Eve");
for (String name : names)
System.out.println((i++) + ":" + name);
i=0;
System.out.println("Size (should be 5): " + names.size());
System.out.println("IndexOf(Eve) (should be 1): " + names.indexOf("Eve"));
System.out.println("LastIndexOf(Eve) (should be 4): " + names.lastIndexOf("Eve"));
System.out.println("Remove Eve (should be true):" + names.remove("Eve"));
System.out.println("Size (should be 4): " + names.size());
System.out.println("IndexOf(Eve) (should be 3): " + names.indexOf("Eve"));
System.out.println("LastIndexOf(Eve) (should be 3): " + names.lastIndexOf("Eve"));
for (String name : names)
System.out.println((i++) + ":" + name);
i=0;
System.out.println("Remove Eve (should be true):" + names.remove("Eve"));
System.out.println("Size (should be 3): " + names.size());
System.out.println("IndexOf(Eve) (should be -1): " + names.indexOf("Eve"));
System.out.println("LastIndexOf(Eve) (should be -1): " + names.lastIndexOf("Eve"));
System.out.println("Size (should be 3): " + names.size());
System.out.println("Remove 0 (should be Alice): " + names.remove(0));
System.out.println("Size (should be 2): " + names.size());
names.add(0, "Alice");
names.add(1, "Eve");
names.add("Eve");
names.add(1, "Eve");
names.add("Eve");
names.add(1, "Eve");
names.add(names.indexOf("Carol"), "Eve");
names.add(0, "Eve");
names.add("Eve");
System.out.println("Size: " + names.size());
for (String name : names)
System.out.println((i++) + ":" + name);
i=0;
System.out.println("Remove all instances of Eve using iterator... ");
Iterator<String> it = names.iterator();
while(it.hasNext()) {
if (it.next().equals("Eve"))
   it.remove();
}
for (String name : names)
System.out.println((i++) + ":" + name);
i=0;
System.out.println("Testing clear");
names.clear();
System.out.println("Size (should be 0): " + names.size());
  
}
}
//end of ListDriver.java

Output:



Related Solutions

(Java) Building a Doubly Linked List off of an interface I am having trouble with these...
(Java) Building a Doubly Linked List off of an interface I am having trouble with these two methods @Override public void add(T newEntry) { DoubleLinkedNode newNode = new DoubleLinkedNode(newEntry); if (!isEmpty()) { } if (isEmpty()) { first = newNode; last = newNode; } else { last.setNextNode(newNode); newNode.setPreviousNode(last); last = newNode; } // length++; numElements++; } @Override public void add(int newPosition, T newEntry) { DoubleLinkedNode newAdder = new DoubleLinkedNode(newEntry); if ((newPosition >= 0) && (newPosition <= numElements)) { numElements++; if (newPosition...
I need this written in Java, it is a Linked List and each of it's Methods....
I need this written in Java, it is a Linked List and each of it's Methods. I am having trouble and would appreciate code written to specifications and shown how to check if each method is working with an example of one method being checked. Thank you. public interface Sequence <T> { /** * Inserts the given element at the specified index position within the sequence. The element currently at that * index position (and all subsequent elements) are shifted...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT