Java Implement the interface from Lab B (EntryWayListInterface). As a reminder,
ID: 3589549 • Letter: J
Question
Java
Implement the interface from 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 and 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
---------------------------------
EntryWayListInterface.java
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();
}
Explanation / Answer
Below is your implementation: -
ImplEntryWayListInterface.java
public class ImplEntryWayListInterface<T> implements EntryWayListInterface<T> {
private class Node {
private T data; // entry in list
private Node next; // link to next node
private Node(T dataPortion) {
data = dataPortion;
next = null;
} // end constructor
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
} // end Node
private Node firstNode; // reference to the first Node
private int length; // number of entries in the list
public ImplEntryWayListInterface() {
clear();
}// end default constructor
public final void clear() {
firstNode = null;
length = 0;
}// end clear
public boolean insertHead(T newEntry) {
// create a new node: newFirstNode
// newFirstNode links to firstNode
// assign newFirstNode to firstNode
Node newFirstNode = new Node(newEntry);
newFirstNode.next = firstNode;
firstNode = newFirstNode;
// increase length
length++;
return true;
}
public boolean insertTail(T newEntry) {
Node currentNode = firstNode;
Node newLastNode = new Node(newEntry);
// if length == 0 and isEmpty
// double check so everything is correct (use &&)
if (isEmpty() == true && length == 0) {
firstNode = newLastNode; // assign the newLastNode to firstNode
}
// if length != 0 and !isEmpty
// double check so everything is correct (use &&)
if (isEmpty() == false && length != 0) {
// as long as link has not end
while (currentNode.next != null) {
currentNode = currentNode.next;
}
currentNode.next = newLastNode;
}
// increase length
length++;
return true;
}
public T deleteHead() {
assert !isEmpty();
Node newFirstNode;
T result = null;
if (length != 0) {
if (length == 1) { // if list only have 1 item
firstNode = null;
length--; // decrease length
// System.out.println("isEmpty value "+ isEmpty());
return result;
}
if (length != 1) { // if list have more than 1 item
newFirstNode = firstNode.next;
newFirstNode.next = firstNode.next.next;
firstNode = newFirstNode;
length--; // decrease length
}
}
return result;
}
public T deleteTail() {
assert !isEmpty();
Node newLastNode = null;
Node currentNode = firstNode;
Node previousNode = firstNode;
T result = null;
for (int i = 1; i <= length; i++) {
if (currentNode.next != null) {
previousNode = currentNode;
currentNode = currentNode.next;
newLastNode = previousNode;
newLastNode.next = currentNode;
// System.out.print("loop #"+i + ": ");
// System.out.print("prevNode data is "+ previousNode.data+".
// ");
// System.out.println("currentNode data is" +currentNode.data+".
// ");
// System.out.println("newlastnode is "+ newLastNode.data);
}
}
newLastNode.next = null; // removing the last node
return result;
}
public void display() {
// iterative
Node currentNode = firstNode;
while (currentNode != null) {
System.out.println(currentNode.data);
currentNode = currentNode.next;
} // end while
if (firstNode == null) {
System.out.println("No display -- List is empty.");
}
} // end display
public int contains(T anEntry) {
assert !isEmpty();
Node currentNode = firstNode;
int position = -1; // if entry is not found
for (int i = 0; i < length; i++) {
// if currentNode.data is equal to the desired entry.
if (currentNode.data.equals(anEntry)) {
position = i + 1;
return position;
}
currentNode = currentNode.next;
}
return position;
}// end contains
public boolean isEmpty() {
boolean result;
if (length == 0) // or getLength() == 0
{
assert firstNode == null;
result = true;
} else {
assert firstNode != null;
result = false;
} // end if
return result;
} // end isEmpty
public boolean isFull() {
return false; // using linked list, list is never full
// as long as we have enough memory
} // end isFull
}
ListDriver.java
public class ListDriver {
public static void main(String[] args) {
ImplEntryWayListInterface<String> nameList = new ImplEntryWayListInterface<String>();
/* isEmpty() test before adding anything*/
System.out.println("1. testing isEmpty() method before adding anything.");
System.out.println("Output:");
boolean emptyTest = nameList.isEmpty();
System.out.println(emptyTest);
System.out.println("Output should be: true");
System.out.println();
/*display() test before adding anything*/
System.out.println("2. testing display() method.");
System.out.println("Output:");
nameList.display();
System.out.println("Output should be: No display -- List is empty.");
System.out.println();
/*insertHead() test*/
System.out.println("3. testing insertHead() method. Adding five elements to the head.");
System.out.println("Output:");
nameList.insertHead("Abby");
nameList.insertHead("Bobby");
nameList.insertHead("Carrie");
nameList.insertHead("Doug");
nameList.insertHead("Edgar");
nameList.display();
System.out.println("Output should be: Edgar, Doug, Carrie, Bobby, Abby");
System.out.println();
/* isEmpty() test after adding stuff*/
System.out.println("4. testing isEmpty() method after adding elements.");
System.out.println("Output:");
emptyTest = nameList.isEmpty();
System.out.println(emptyTest);
System.out.println("Output should be: false");
System.out.println();
/*insertTail() test*/
System.out.println("5. testing insertHead() method. Adding five elements to the end.");
System.out.println("Output:");
nameList.insertTail("Valentine");
nameList.insertTail("World");
nameList.insertTail("X-Ray");
nameList.insertTail("YOLO");
nameList.insertTail("Zebra");
nameList.display();
System.out.println("Output should be: Edgar, Doug, Carrie, Bobby, Abby, Valentine, World, X-Ray, YOLO, Zebra");
System.out.println();
/*deleteHead() test*/
System.out.println("6. testing deleteHead() method. Edgar should be removed from the list.");
System.out.println("Output:");
nameList.deleteHead();
nameList.display();
System.out.println("Output should be: Doug, Carrie, Bobby, Abby, Valentine, World, X-Ray, YOLO, Zebra");
System.out.println();
/*deleteHead() test*/
System.out.println("7. testing deleteHead() method. Doug should be removed from the list.");
System.out.println("Output:");
nameList.deleteHead();
nameList.display();
System.out.println("Output should be: Carrie, Bobby, Abby, Valentine, World, X-Ray, YOLO, Zebra");
System.out.println();
/*deleteHead() test*/
System.out.println("8. testing deleteHead() method when list only have 1 entry");
System.out.println("Output:");
nameList.deleteHead();
nameList.deleteHead();
nameList.deleteHead();
nameList.deleteHead();
nameList.deleteHead();
nameList.deleteHead();
nameList.deleteHead(); //by this line, list will only have 1 remaining entry: "Zebra"
nameList.deleteHead();
nameList.display();
System.out.println("Output should be: No display -- List is empty.");
System.out.println();
/*isEmpty() test after deleting all the elements*/
System.out.println("9. testing isEmpty() method after deleting all the elements.");
System.out.println(nameList.isEmpty());
System.out.println("Output should be: true");
System.out.println();
/*insertTail() test when list is empty*/
System.out.println("10. testing insertTail() method on an empty list.");
System.out.println("Output:");
nameList.insertTail("A");
nameList.display();
System.out.println("Output should be: A");
System.out.println();
/*insertTail() test when list is not empty*/
System.out.println("10. testing insertTail() method when list is NOT empty.");
System.out.println("Output:");
nameList.insertTail("B");
nameList.insertTail("C");
nameList.insertTail("D");
nameList.insertTail("E");
nameList.display();
System.out.println("Output should be: A, B, C, D, E");
System.out.println();
/*testing contains()*/
System.out.println("11. testing contains() method.");
System.out.println("Output:");
System.out.println(nameList.contains("A"));
System.out.println(nameList.contains("D"));
System.out.println(nameList.contains("Z"));
System.out.println("Output should be: 1, 4, -1");
System.out.println();
/*testing deleteTail()*/
System.out.println("12. testing deleteTail() method.");
System.out.println("Output:");
nameList.deleteTail();
nameList.deleteTail();
nameList.display();
System.out.println("Output should be: A, B, C");
System.out.println();
/*testing deleteTail() and deleteHead()*/
System.out.println("12. testing deleteTail() method and deleteHead().");
System.out.println("Output:");
nameList.deleteHead();
nameList.deleteTail();
nameList.display();
System.out.println("Output should be: B");
System.out.println();
}
}
Sample Output
1. testing isEmpty() method before adding anything.
Output:
true
Output should be: true
2. testing display() method.
Output:
No display -- List is empty.
Output should be: No display -- List is empty.
3. testing insertHead() method. Adding five elements to the head.
Output:
Edgar
Doug
Carrie
Bobby
Abby
Output should be: Edgar, Doug, Carrie, Bobby, Abby
4. testing isEmpty() method after adding elements.
Output:
false
Output should be: false
5. testing insertHead() method. Adding five elements to the end.
Output:
Edgar
Doug
Carrie
Bobby
Abby
Valentine
World
X-Ray
YOLO
Zebra
Output should be: Edgar, Doug, Carrie, Bobby, Abby, Valentine, World, X-Ray, YOLO, Zebra
6. testing deleteHead() method. Edgar should be removed from the list.
Output:
Doug
Carrie
Bobby
Abby
Valentine
World
X-Ray
YOLO
Zebra
Output should be: Doug, Carrie, Bobby, Abby, Valentine, World, X-Ray, YOLO, Zebra
7. testing deleteHead() method. Doug should be removed from the list.
Output:
Carrie
Bobby
Abby
Valentine
World
X-Ray
YOLO
Zebra
Output should be: Carrie, Bobby, Abby, Valentine, World, X-Ray, YOLO, Zebra
8. testing deleteHead() method when list only have 1 entry
Output:
No display -- List is empty.
Output should be: No display -- List is empty.
9. testing isEmpty() method after deleting all the elements.
true
Output should be: true
10. testing insertTail() method on an empty list.
Output:
A
Output should be: A
10. testing insertTail() method when list is NOT empty.
Output:
A
B
C
D
E
Output should be: A, B, C, D, E
11. testing contains() method.
Output:
1
4
-1
Output should be: 1, 4, -1
12. testing deleteTail() method.
Output:
A
B
C
Output should be: A, B, C
12. testing deleteTail() method and deleteHead().
Output:
B
Output should be: B
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.