Implement the interface you wrote for Lab B EntryWayListInte

Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only through the beginning and end of the list.

Your implementation can use either an expandable array or linked nodes- it is your choice. You can decide what instance variables are needed. You must implement every method from the interface. Make sure to account for special conditions such as empty lists and singleton lists. Note: your instance data variable should be either a) an array or b) one or more nodes. It should not be an AList or LList object or an ArrayList object.

Your implementation must:

compile

contain these implemented methods:

insertHead

insertTail

deleteHead

deleteTail

display

contains

isEmpty

isFull

Also create a driver program to test your implementation. The driver program will operate from the client perspective. Your driver program should:

display an empty list

add five entries to the list- some at the head and some at the tail

display the list

remove the first entry

remove the last entry

display the list

test to see if elements are in the list (test one element that is in the list and one that is not)

remove the last three elements in the list

try to remove an element from the empty list

Write a second class to implement EntryWayListInterface. Instead of using an array or linked nodes, use either an AList or LList object as your instance data variable. In this way, you are using an existing data type (AList or LList) to implement a new data type (EntryWayListInterface). Inside the methods of this class, invoke methods on the AList or LList object to accomplish the task.

/** An interface for the ADT list.
Entries in a list have positions that begin with 1.
* Accesses only entries that are the head or tail of the list.
*/

public interface EntryWayInterface<T> {
  
/** Adds a new entry to this list T.
* @param newEntry The object to be added as a new entry.
* @return True if the addition is successful, or false if not. */
public boolean insertHead(T newEntry);

/** Deletes a new entry to this list T.
* @param newEntry The object to be added as a new entry.
* @return True if the addition is successful, or false if not. */
public boolean insertTail(T newEntry);

/** Deletes the object that is at the head of list T.
* @return the object that has been deleted and returns
* null if no object was deleted.
*/
public T deleteHead();

/** Deletes the object that is at the tail of list T.
* @return the object that has been deleted or else return null
* if no object was deleted.
*/
public T deleteTail();

/** Displays the contents of the list T.
*/
public void display();

/** See whether this list T contains a given entry.
* @param anEntry The object that is the desired entry.
* @return the position of the entry that was found.
* else returns false if the object is not in the list.
*/
public int contains(T anEntry);

/** Sees whether this list is empty.
* @return True if the list is empty, or false if not. */
public boolean isEmpty();

/** Sees whether this list is full.
* @return True if the list is full, or false if not. */
public boolean isFull();
  
}//end EntryWayInterface

Solution

public class LEntryWayList<T> implements EntryWayListInterface<T> {
   private Node firstNode;
   private Node lastNode;
   private int length;

   public LEntryWayList() {
       clear();
   }

   public final void clear() {
       firstNode = null;
       lastNode = null;
       length = 0;
   }

   /**
   * Task: Places a new object at beginning of list
   *
   * @param newEntry is a valid object
   * @return true if insert is successful; false otherwise
   */
   public boolean insertHead(T newEntry) {
       Node newNode = new Node(newEntry);
       if (isEmpty()) {
           firstNode = newNode;
           lastNode = newNode;
       } else {
           newNode.next = firstNode;
           firstNode = newNode;
       }
       length++;
       return true;
   }

   /**
   * Task: Places a new object at the end of the list
   *
   * @param newEntry is a valid object
   * @return true if insertion successful; false otherwise
   */
   public boolean insertTail(T newEntry) {
       Node newNode = new Node(newEntry);
       if (isEmpty()) {
           firstNode = newNode;
       } else {
           lastNode.next = newNode;
       }
       lastNode = newNode;
       length++;
       return true;
   }

   /**
   * Task: delete the object at the beginning of the list
   *
   * @return the object that has been deleted
   */
   public T deleteHead() {
       T result = null;
       if (length >= 1) {
           assert !isEmpty();
           if (length == 1) {
               result = firstNode.data;
               firstNode = null;
               lastNode = null;
           } else {
               result = firstNode.data;
               firstNode = firstNode.next;
           }
           length--;
       }
       return result;
   }

   /**
   * Task: delete the object at the end of the list
   *
   * @return the object that has been deleted, or null if the list was empty
   */
   public T deleteTail() {
       T result = null;
       if (length >= 1) {
           assert !isEmpty();
           if (length == 1) {
               result = firstNode.data;
               firstNode = null;
               lastNode = null;
           } else {
               Node nodeBefore = getNodeAt(length - 1);
               Node nodeToRemove = nodeBefore.next;
               nodeBefore.next = null;
               result = nodeToRemove.data;
               lastNode = nodeBefore;
           }
           length--;
       }
       return result;
   }

   /**
   * Task: display the contents of the list on the console, in order, one per
   * line
   */
   public void display() {
       Node currentNode = firstNode;
       while (currentNode != null) {
           System.out.println(currentNode.getData());
           currentNode = currentNode.getNext();
       }
   }

   /**
   * Task: search the list for the given object and return its position in the
   * list, or -1 if it\'s not found
   *
   * @param anEntry is a valid object to find in the list
   * @return the position of the entry that was found, or -1 if it\'s not found
   */
   public int contains(T anEntry) {
       int found = 0;
       Node currentNode = firstNode;

       while (currentNode != null) {
           found++;
           if (anEntry.equals(currentNode.getData())) {
               return found;
           } else {
               currentNode = currentNode.getNext();
           }
       }
       return -1;
   }

   /**
   * Task: check to see if list is empty
   *
   * @return true if list is empty, false if list contains one or more
   * objects.
   */
   public boolean isEmpty() {
       boolean result;

       if (length == 0) {
           assert firstNode == null;
           result = true;
       } else {
           assert firstNode != null;
           result = false;
       }
       return result;
   }

   /**
   * Task: check if list is full
   *
   * @return true if list is full, false if list has space for more objects
   */
   public boolean isFull() {
       return false; // Linked lists always return false
   }

   private Node getNodeAt(int givenPosition) {
       assert !isEmpty()
               && ((1 <= givenPosition) && (givenPosition <= length));

       Node currentNode = firstNode;
       for (int counter = 1; counter < givenPosition; counter++) {
           currentNode = currentNode.getNext();
       }
       assert currentNode != null;
       return currentNode;
   }

   private class Node {
       private T data;
       private Node next;

       private Node(T dataPortion) {
           data = dataPortion;
           next = null;
       }

       private Node(T dataPortion, Node nextNode) {
           data = dataPortion;
           next = nextNode;
       }

       public T getData() {
           return data;
       }

       public void setData(T data) {
           this.data = data;
       }

       public Node getNext() {
           return next;
       }

       public void setNext(Node next) {
           this.next = next;
       }
   }


public static void main(String []args){
LEntryWayList l = new LEntryWayList();
l.display();
  
}


}

Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only throug
Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only throug
Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only throug
Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only throug
Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only throug
Implement the interface you wrote for Lab B (EntryWayListInterface). As a reminder, EntryWayListInterface allows the user to access to list elements only throug

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site