Implement the JAVA interface I wrote (EntryWayListInterface.java). As a reminder
ID: 669560 • Letter: I
Question
Implement the JAVA interface I wrote (EntryWayListInterface.java). As a reminder, EntryWayListInterface allows access to list elements only through the beginning and end of the list.
*****************************************************************************************
public interface EntryWayListInterface<T> {
/**
* Places a new object at beginning of list
*
* @param newEntry the item to be added to the list
* @return true if the item was successfully added to the list; false otherwise
*/
boolean insertHead(T newEntry);
/**
* Places a new object at the end of the list
*
* @param newEntry the item to be added to the list
* @return true if the item was successfully added to the list; false otherwise
*/
boolean insertTail(T newEntry);
/**
* Deletes the object at the beginning of the list
*
* @return the object that has been deleted or null if the list is empty
*/
T deleteHead();
/**
* Delete the object at the end of the list
*
* @return the object that has been deleted or null if the list is empty
*/
T deleteTail();
/**
* Displays the contents of the list on the console, in order, one per line
*/
void display();
/**
* Searches the list for the given object and return its position in the
* list, or -1 if it's not found
*
* @param anEntry the object to search for in the list
* @return the position of the entry that was found or -1 if the object is not in the list not found
*/
int contains(T anEntry);
/**
* Checks to see if list is empty
*
* @return true if list is empty, false if list contains one or more
* objects
*/
boolean isEmpty();
/**
* Check if list is full
*
* @return true if list is full, false if list has space for more objects
*/
boolean isFull();
}
**************************************************************************************
#The implementation can use either a resizeable 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 an array or one or more nodes. It should not be an AList or LList object.
The implementation must:
#compile
#contain these implemented methods:
**insertHead
**insertTail
**deleteHead
**deleteTail
**display
**contains
**isEmpty
**isFull
Explanation / Answer
Note: As per your requirement, java code has written.
CODE
package Sam;
public class Listinterface<T extends Comparable<T>> implements EntryWayListInterface<T>
{
//Iterator//
private Node firstNode;
//length of the String //
private int length;
//constructor//
public Listinterface()
{
clear();
}
//deleting the nodes//
public final void clear()
{
//object checking//
firstNode = null;
//check empty//
length = 0;
}
//List to sort//
public boolean add(T newEntry)
{
Node newNode = new Node(newEntry);
Node currentNode = firstNode;
newNode.next = firstNode;
firstNode = newNode;
length++;
//Implemented recursive method//
insertionSort(null,firstNode);
return true;
}
//Soting array//
private void insertionSort(Node nodeOne, Node nodeTwo)
{
//Condition
if(nodeTwo.next != null )
{
Node beforeNode = nodeOne;
Node currentNode = nodeTwo ;
Node nextNode = nodeTwo.next;
if(currentNode.data.compareTo(nextNode.data)>0 )
{
currentNode.next = nextNode.next;
nextNode.next = currentNode;
if(beforeNode != null)
{
beforeNode.next = nextNode;
}
else
{
firstNode = nextNode;
firstNode.next = currentNode;
}
//Call function//
insertionSort(nextNode,currentNode);
}
}
//Sorting ends//}
public boolean insertHead(T newEntry)
{
return true;
}
public boolean insertTail(T newEntry)
{
return true;
}
//removing the elements//
public T deleteHead()
{
int mypos=1;
Node curnode=getNodeAt(mypos);
firstNode=curnode.next;
curnode.next=null;
length--;
return curnode.data;
}
// deleting tail elements//
public T deleteTail()
{
int tail=length;
Node tailNode=getNodeAt(tail);
Node tailNode2=getNodeAt(tail-1);
tailNode2.next=null;
length--;
return tailNode.data;
}
//Interface with classes and methods//
public void display()
{
for (int position = 1; position <= length; position++)
{
Node currentNode = getNodeAt(position);
System.out.println(currentNode.data);
}
}
public boolean contains(T anEntry)
{
boolean found = false;
Node currentNode = firstNode;
// loop started//
while (!found && (currentNode != null))
{
if (anEntry.equals(currentNode.data))
found = true;
else
currentNode = currentNode.next;
} // loop ends//
return found;
}
//nodes return//
public int getLength()
{
return length;
}
//specified in interface BasicList //
public boolean isEmpty()
{
boolean result;
//Condition to check length//
if (length == 0)
{
assert firstNode == null;
result = true;
}
else
{
assert firstNode != null;
result = false;
} // end if loop//
return result;
}
public boolean isFull()
{
return true;
}
//Node situates in the position//
private Node getNodeAt(int givenPosition)
{
assert !isEmpty() &&(1 <= givenPosition) && (givenPosition <= length);
Node currentNode = firstNode;
//Loop//
for (int counter = 1; counter < givenPosition; counter++)
currentNode = currentNode.next;
assert currentNode != null;
return currentNode;
}
//Static nested class//
private class Node
{
private T data;
private Node next;
private Node(T dataPortion)
{
data = dataPortion;
next = null;
}
//Queue interface//
private Node(T dataPortion, Node nextNode)
{
//Object creation//
data = dataPortion;
next = nextNode;
}
}
}
Main
package Sam;
public class Listinterface
{
//Main function//
public static void main(String[] args)
{
//Linked list//
EntryWayListInterface<String> myList = new LList<String>();
System.out.println(" **********START************* ");
System.out.println("List should be empty; isEmpty returns :" +
myList.isEmpty());
//Printing in a new line//
System.out.println(" Testing add with insertion sorting:");
System.out.println("Add 45: returns " + myList.add("45"));
System.out.println("Add 55: returns " + myList.add("55"));
System.out.println("Add 75: returns " + myList.add("75"));
System.out.println("Add 10: returns " + myList.add("10"));
System.out.println("Add 20: returns " + myList.add("20"));
System.out.println(" Display the list: ");
myList.display();
System.out.println("Current head deleted " + myList.deleteHead());
myList.display();
System.out.println("Current tail deleted " + myList.deleteTail());
myList.display();
System.out.println(" ******** End*********");
} // end main
}