PROBLEM STATEMENT:
In this assignment, you will complete DoubleEndedList.java that implements the ListInterface as
well as an interface called DoubleEndedInterface which represents the list's entries by using a
chain of nodes that has both a head reference and a tail reference. Be sure to read through the
code and understand the implementation.
WHAT IS PROVIDED:
- A driver class to test your code. You should not modify this file!
- A list interface (ListInterface.java)
- A double ended interface (DoubleEndedInterface.java)
- An incomplete DoubleEndedList class (DoubleEndedList.java)
WHAT YOU NEED TO DO:
4. Complete the DoubleEndedList class
4. Run the driver and make sure your output is exactly the same as mine (at the bottom of
Driver.java)
} // end else numberofEntries--; else throw new IndexOut0fBoundsException("Illegal position
given to remove operation."); return result; // Return removed entry }//endreturnreve public T
replace(int givenPosition, T newEntry) { T replace(int givenPosition, T newEntry) { if
((givenPosition >=1)&& (givenPosition <= numberOfEntries)) { // Assertion: The list is not
empty Node desiredNode = getNodeAt (givenPosition); ToriginalEntry = desiredNode.getData();
desiredNode.setData(newEntry); return originalEntry; f // end if else throw new
IndexOut0fBoundsException("Illegal position given to replace operation."); replace if ((
givenPosition >=1)&& (givenPosition <= number0fEntries)) { // Assertion: The list is not
empty Node desiredNode = getNodeAt ( givenPosition); T originalEntry = desiredNode.
getData( ); desiredNode.setData(newEntry); return originalentry; } // end if throw new
Index0ut0fBoundsException("Illegal position given to replace operation."); } // end replace
public T getEntry(int givenPosition) { if ((givenPosition >=1) && (givenPosition < =
number0fEntries ) ) { // Assertion: The list is not empty return getNodeAt (givenPosition).
getData(); else // end if throw new IndexOut0fBoundsException("Illegal position given to
getEntry operation."); } // end getEntry public boolean contains ( T anEntry) { boolean found =
false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if
(anEntry.equals (currentNode.getData())) else found = true; } // end while currentNode =
currentNode. getNextNode () ; return found; } // end contains public int getLength() { return
numberofEntries; } // end getLength public boolean isEmpty() { return number0fEntries ==0;
} // end isEmpty public T[] toArray() { // The cast is safe because the new array contains null
entries aSuppressWarnings ("unchecked") T[] result =(T[]) new 0bject [numberofEntries]; //
Unchecked cast int index =0; Node currentNode = firstNode; while ((index < numberOfEntries)
&& (currentNode != null)) & result [ index ]= currentNode. getData () ; currentNode =
currentNode.getNextNode ( ); index++; 3 // end while return result; 3 // end toArray // Returns a
reference to the node at a given position. // Precondition: List is not empty; 1<= givenPosition <
= numberofEntries. // Assertion: (firstNode != null) and (1<= givenPosition) and (givenPosition
<= // numberofEntries) Node currentNode = firstNode; if (givenPosition == numberofEntries)
currentNode = lastNode; else if (givenPosition >1 ) // Traverse the chain to locate the desired
node for (int counter =1; counter < givenPosition; counter++) currentNode = currentNode.
getNextNode () ; assert currentNode != null; return currentNode; } // end getNodeAt private
class Node { private T data; // Data portion private Node next; // Next to next node data =
dataPortion; } // end constructor data = dataPortion; } // end constructor private T getData() {
} // end return data; private void setData( T newData) { data = newData; } // end setData
private Node getNextNode() { return next; } // end getNextNode private void
setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node
1** A class that implements the ADT double-ended list by using a chain of nodes. * The chain
has both a head reference and a tail reference. * Position numbers begin with 1 . * Q eauthor
Frank M. Carrano * dauthor Joseph Erickson */ aversion 5.0 public class DoubleEndedList
implements ListInterface , DoubleEndedInterface { private Node firstNode; // Head reference to
first node private Node lastNode; // Ta private int numberofEntries; public DoubleEndedList() {
} clear (); // ============= // ADDED METHODS: public void addFirst(T newEntry) f } //
end addFirst public void addLast (T newEntry) { } // end addLast public T removefirst() { } //
end removefirst public T removelast() { } // end removelast public T getFirst() { } // end
getFirst public T getLast () { } // end getLast public void moveToEnd() { } // end moveToEnd
// ====================== public final void clear() firstNode = null; lastNode = null;
numberOfEntries =0 } // end clear public void add ( T newEntry) { Node newNode = new Node
(newEntry); // Create new node if (isEmpty()) else firstNode = newNode; las tNode.
setNextNode (newNode); lastNode = newNode; numberOfEntries++; 3 // end add if ((
newPosition >=1)&& (newPosition <= numberofEntries +1)){ Node newNode = new Node
(newEntry); if (isEmpty()) firstNode = newNode lastNode = newNode; }// end if else if
(newPosition =1 ) { newNode. SetNextNodel first Node = newNode; 3 // end else if
(newPosition == numberOfEntries +1 ) { lastNode. setNextNode (newNode); last Node =
newNode; } // end else { Node nodeBefore = getNodeAt ( newPosition -1); Node nodeAfter =
nodeBefore.getNextNode () newNode. setNextNode (nodeAfter); 3 // end else } // end if else }
// end add throw new IndexOut0fBoundsException("Illegal position given to add operation.");
public T remove(int givenPosition) { T result = null; // Return value if ((givenPosition >=1)&&
(givenPosition <= number0fEntries)) { // Assertion: The list is not empty result = firstNode.
getData ();// Save entry to be removed firstNode = firstNode.getNextNode( ); if (numberofentries
=1 ) } // end if lastNode = null; // Solitary entry was removed else // Case 2: givenPosition > 1
Node nodeBefore = getNodeAt (givenPosition -1); Node nodeToRemove = nodeBefore.
getNextNode (); Node nodeAfter = nodeToRemove. getNextNode (); Node nodeAfter =
nodeToRemove.getNextNode (); nodeBefore.setNextNode(nodeAfter); // Disconnect the node to
be removed result = nodeToRemove.getData(); // Save entry to be removed if (givenPosition ==
numberofentries) lastNode = nodeBefore; // Last node was removed } // end else
numberofEntries--; else throw new Indexout0fBoundsException("Illegal position given to
remove operation."); return result; // Return removed entry remove } // end remove public T
replace(int givenPosition, T newEntry) { venPosition >=1)&& (givenPosition <=
numberofentries) ) { Node desiredNode = getNodeAt (givenPosition); ToriginalEntry =
desiredNode.getData( ); return originalData (newEntry); } // end if throw new
IndexOut0fBoundsException("Illegal position given to replace operation.");
An interface for a collection that has a first entry and a last entry. @author Frank M. Carrano
Qversion 5.0 / ablic interface DoubleEndedInterface {/ * Adds a new entry to the beginning of
this collection. * @param newEntry The object to be added as a new entry. 1 public void
addFirst(T newEntry); /** * Adds a new entry to the end of this collection. * Qparam newEntry
The object to be added as a new entry. / public void addLast( T newEntry); /** * Removes and
returns the first entry in this collection. * areturn A reference to the removed entry or null, if *
the list was empty. / public T removefirst(); /** * Removes and returns the last entry in this
collection. * Qreturn A reference to the removed entry or null, if * the list was empty. / public T
removelast(); /** * Retrieves the first entry in this collection. * areturn A reference to the first
entry or null, if * the list is empty. 1 public T getFirst(); /** * Retrieves the last entry in this
collection. * Qreturn A reference to the last entry or null, if * the list is empty. / public T
getLast(); /** Moves the first entry in this collection to the end of the list. */ public void
moveToEnd(); // end DoubleEndedInterface
ver that demonstrates the class DoubleEndedList. thest only the additional methods, since the
methods or Frank M. Carrano ion 5.0 lass Driver { lass Driver { public static void
main(String[] args) { System.out.println("Create an empty list."); System. out.print ln("Create
an empty list."); DoubleEndedList < String > myList = new DoubleEndedList <();
System.out.println("List should be empty; isEmpty() returns " + myList.isEmpty() + ".");
System.out.print ln("nTesting addFirst:"); myList.addFirst ("15"); myList. addFirst (" 25 ")
mylist.addFirst (" 45) System.out.println("List should contain 453525 15."); myList.clear();
mylist.clear(); myList. addLast ("15"); myList.addLast ("35") myList.addLast (" 45)
myList.addLast ("55"); myList. addLast ("65"); displaylist (myList); System.out.print
ln("nTesting removeFirst with previous list:"); System.out.println("removeFirst (): " + myList.
removeFirst ()); System. out.println("nList should containn45 5565 "); displayList (myList);
System. out.println("removefirst(): " + myList. removeFirst()); System.
out.println("removefirst(): " + myList. removefirst()); System. out.println("List should be empty;
isEmpty() returns" + myList.isEmpty() + "."); myList.clear(); myList. addLast ("15");
myList.addLast (" 25 ") myList. addLast ("45"); myList. addLast ("55"); System.out.println("List
should contain 1525354555 65."); displaylist (myList); System.out.println("removeLast (): " +
myList.removeLast()); System.out.println("removeLast (): " + myList.removeLast ()); System.
out.print ln ("removeLast (): " + myList. removeLast ()) System. out.print ln ("removeLast (): " +
mylist.removelast ()); System.out.println("nList should contain n15"); displaylist (myList); ( )
System.out.print ln("nTesting getFirst and getlast with the following list:"); myList.clear()
myList.addLast ("15"); myList.addLast (" 25 "); myList. addLast ("35"); myList. addLast (" 55)
myList.addLast ("65"); displayList (myList): System.out.println("Retrieving the first entry :
returns " + myList.getFirst()); System.out.print ln("nTesting moveToEnd with the following
list:"); displayList (myList); myList. moveToEnd () System. out.println("After moveToEnd, the
list is:"); displaylist (myList); myList. moveToEnd ( ) System.out.println("After moveToEnd, the
list is:"); displaylist (myList); myList. moveToEnd ( ) System.out.println("After moveToEnd, the
list is:"); displaylist (mylist); System. out.print ln ("nnDone."); public static void
displayList(ListInterface aList) { System.out.println("The list contains " + aList.getLength() +
"string(s), as follows:"); Object [] listArray = aList. toArray(); for (int index =0; index <
listarray. length; index++) { } // end for System.out.print ln(); for (int position =1; position <=
listarray. length; position++) { if (position < 10) else System.out.print(" " + position);
System.out.print (position); System.out.print(" "); }//endfor System.out.print ln(); H/ end Driver
e an empty list. should be empty; isEmpty() returns true. son list. ng addFirst: should contain
45352515 ist contains 4 string(s), as follows: 4 ng addlast with the following list: should contain
152535455565. ist contains 456
* Testing removefirst with previous list: * removefirst(): 15 * removeFirst(): 25 * removeFirst():
35 * List should contain * 455565 * The list contains 3 string(s), as follows: * 455565 * 123 *
removeFirst(): 45 * removeFirst(): 55 * removeFirst(): 65 * removefirst(): null * List should be
empty; isEmpty() returns true. * * Testing removelast with the following list: * List should
contain 152535455565 . * The list contains 6 string(s), as follows: * 152535455565 * 123456 *
removeLast (): 65 * removelast (): 55 * removelast (): 45 * removelast (): 35 * removeLast (): 25
* List should contain * 15 * The list contains 1 string(s), as follows: * 15 * 1 * removelast (): 15
* removelast (): null * List should be empty; isEmpty() returns true. * Testing getFirst and
getlast with the following list: * The list contains 6 string(s), as follows: * 152535455565 *
123456 * Retrieving the first entry : returns 15 * Retrieving the last entry : returns 65 * Testing
moveToEnd with the following list: * The list contains 6 string(s), as follows: * 152535455565 *
123456 * After moveToEnd, the list is: * The list contains 6 string(s), as follows: *
253545556515 * 123456 * After moveToEnd, the list is: * The list contains 6 string(s), as
follows: * 354555651525 * 123456 * After moveToEnd, the list is: * The list contains 6
string(s), as follows: * 455565152535 * 123456 * * * Done. *I
* Entries in a list have positions that begin with 1. * eauthor Frank M. Carrano * dauthor
Timothy M. Henry * aversion 5.0 */ ublic interface ListInterface { /** * Adds a new entry to the
end of this list. * Entries currently in the list are unaffected. * The list's size is increased by 1 . *
Gparam newEntry The object to be added as a new entry. */ public void add( T newEntry); /** *
Adds a new entry at a specified position within this list. * Entries originally at and above the
specified position * are at the next higher position within the list. * The list's size is increased by
1 . * Caparam newPosition An integer that specifies the desired * position of the new entry. *
Caparam newEntry The object to be added as a new entry. * Gathrows
IndexoutofBoundsException if either newPosition < 1 or newPosition > */ getLength ()+1.
public void add(int newPosition, T newEntry); /*** * Removes the entry at a given position from
this list. * Entries originally at positions higher than the given * position are at the next lower
position within the list, * and the list's size is decreased by 1 . * eparam givenPosition An integer
that indicates the position of the entry to be removed. * ereturn A reference to the removed entry.
* athrows IndexOutofBoundsException if either * givenPosition <1 or givenPosition > */
getLength(). public T remove(int givenPosition); /** Removes all entries from this list. */ public
void clear(); /** * Replaces the entry at a given position in this list. * Gparam givenPosition An
integer that indicates the position of * the entry to be replaced. * eparam newEntry The object
that will replace the entry at the * areturn The original position givenPosition. * athrows
Index0utal entry that was replaced. * givenPosition <1 or givenPosition > */ givenPosition < 1
or givenPosition > getLength(). public T replace(int givenPosition, T newEntry); / ** Retrieves
the entry at a given position in this list. * @param givenPosition An integer that indicates the
position of the desired entry. * areturn A reference to the indicated entry. * Gthrows
Index0ut0fBoundsException if either givenPosition < 1 or givenPosition > */ getLength ( ).
public T getEntry(int givenPosition); /** * Retrieves all entries that are in this list in the order in
which * they occur in the list. * Greturn A newly allocated array of all the entries in the list. * If
the list is empty, the returned array is empty. public T[] toArray(); /*** * Sees whether this list
contains a given entry. * eparam anEntry The object that is the desired entry. * Greturn True if
the list contains anEntry, or false if not. */ public boolean contains ( T anEntry); /*** * Gets the
length of this list. * areturn The integer number of entries currently in the list. public int
getLength(); /** * Sees whether this list is empty. * ereturn True if the list is empty, or false if
not. * a public boolean isEmpty(); // end ListInterface

PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf

  • 1.
    PROBLEM STATEMENT: In thisassignment, you will complete DoubleEndedList.java that implements the ListInterface as well as an interface called DoubleEndedInterface which represents the list's entries by using a chain of nodes that has both a head reference and a tail reference. Be sure to read through the code and understand the implementation. WHAT IS PROVIDED: - A driver class to test your code. You should not modify this file! - A list interface (ListInterface.java) - A double ended interface (DoubleEndedInterface.java) - An incomplete DoubleEndedList class (DoubleEndedList.java) WHAT YOU NEED TO DO: 4. Complete the DoubleEndedList class 4. Run the driver and make sure your output is exactly the same as mine (at the bottom of Driver.java) } // end else numberofEntries--; else throw new IndexOut0fBoundsException("Illegal position given to remove operation."); return result; // Return removed entry }//endreturnreve public T replace(int givenPosition, T newEntry) { T replace(int givenPosition, T newEntry) { if ((givenPosition >=1)&& (givenPosition <= numberOfEntries)) { // Assertion: The list is not empty Node desiredNode = getNodeAt (givenPosition); ToriginalEntry = desiredNode.getData(); desiredNode.setData(newEntry); return originalEntry; f // end if else throw new IndexOut0fBoundsException("Illegal position given to replace operation."); replace if (( givenPosition >=1)&& (givenPosition <= number0fEntries)) { // Assertion: The list is not empty Node desiredNode = getNodeAt ( givenPosition); T originalEntry = desiredNode. getData( ); desiredNode.setData(newEntry); return originalentry; } // end if throw new Index0ut0fBoundsException("Illegal position given to replace operation."); } // end replace public T getEntry(int givenPosition) { if ((givenPosition >=1) && (givenPosition < = number0fEntries ) ) { // Assertion: The list is not empty return getNodeAt (givenPosition). getData(); else // end if throw new IndexOut0fBoundsException("Illegal position given to getEntry operation."); } // end getEntry public boolean contains ( T anEntry) { boolean found = false; Node currentNode = firstNode; while (!found && (currentNode != null)) { if (anEntry.equals (currentNode.getData())) else found = true; } // end while currentNode = currentNode. getNextNode () ; return found; } // end contains public int getLength() { return
  • 2.
    numberofEntries; } //end getLength public boolean isEmpty() { return number0fEntries ==0; } // end isEmpty public T[] toArray() { // The cast is safe because the new array contains null entries aSuppressWarnings ("unchecked") T[] result =(T[]) new 0bject [numberofEntries]; // Unchecked cast int index =0; Node currentNode = firstNode; while ((index < numberOfEntries) && (currentNode != null)) & result [ index ]= currentNode. getData () ; currentNode = currentNode.getNextNode ( ); index++; 3 // end while return result; 3 // end toArray // Returns a reference to the node at a given position. // Precondition: List is not empty; 1<= givenPosition < = numberofEntries. // Assertion: (firstNode != null) and (1<= givenPosition) and (givenPosition <= // numberofEntries) Node currentNode = firstNode; if (givenPosition == numberofEntries) currentNode = lastNode; else if (givenPosition >1 ) // Traverse the chain to locate the desired node for (int counter =1; counter < givenPosition; counter++) currentNode = currentNode. getNextNode () ; assert currentNode != null; return currentNode; } // end getNodeAt private class Node { private T data; // Data portion private Node next; // Next to next node data = dataPortion; } // end constructor data = dataPortion; } // end constructor private T getData() { } // end return data; private void setData( T newData) { data = newData; } // end setData private Node getNextNode() { return next; } // end getNextNode private void setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node 1** A class that implements the ADT double-ended list by using a chain of nodes. * The chain has both a head reference and a tail reference. * Position numbers begin with 1 . * Q eauthor Frank M. Carrano * dauthor Joseph Erickson */ aversion 5.0 public class DoubleEndedList implements ListInterface , DoubleEndedInterface { private Node firstNode; // Head reference to first node private Node lastNode; // Ta private int numberofEntries; public DoubleEndedList() { } clear (); // ============= // ADDED METHODS: public void addFirst(T newEntry) f } // end addFirst public void addLast (T newEntry) { } // end addLast public T removefirst() { } // end removefirst public T removelast() { } // end removelast public T getFirst() { } // end getFirst public T getLast () { } // end getLast public void moveToEnd() { } // end moveToEnd // ====================== public final void clear() firstNode = null; lastNode = null; numberOfEntries =0 } // end clear public void add ( T newEntry) { Node newNode = new Node (newEntry); // Create new node if (isEmpty()) else firstNode = newNode; las tNode.
  • 3.
    setNextNode (newNode); lastNode= newNode; numberOfEntries++; 3 // end add if (( newPosition >=1)&& (newPosition <= numberofEntries +1)){ Node newNode = new Node (newEntry); if (isEmpty()) firstNode = newNode lastNode = newNode; }// end if else if (newPosition =1 ) { newNode. SetNextNodel first Node = newNode; 3 // end else if (newPosition == numberOfEntries +1 ) { lastNode. setNextNode (newNode); last Node = newNode; } // end else { Node nodeBefore = getNodeAt ( newPosition -1); Node nodeAfter = nodeBefore.getNextNode () newNode. setNextNode (nodeAfter); 3 // end else } // end if else } // end add throw new IndexOut0fBoundsException("Illegal position given to add operation."); public T remove(int givenPosition) { T result = null; // Return value if ((givenPosition >=1)&& (givenPosition <= number0fEntries)) { // Assertion: The list is not empty result = firstNode. getData ();// Save entry to be removed firstNode = firstNode.getNextNode( ); if (numberofentries =1 ) } // end if lastNode = null; // Solitary entry was removed else // Case 2: givenPosition > 1 Node nodeBefore = getNodeAt (givenPosition -1); Node nodeToRemove = nodeBefore. getNextNode (); Node nodeAfter = nodeToRemove. getNextNode (); Node nodeAfter = nodeToRemove.getNextNode (); nodeBefore.setNextNode(nodeAfter); // Disconnect the node to be removed result = nodeToRemove.getData(); // Save entry to be removed if (givenPosition == numberofentries) lastNode = nodeBefore; // Last node was removed } // end else numberofEntries--; else throw new Indexout0fBoundsException("Illegal position given to remove operation."); return result; // Return removed entry remove } // end remove public T replace(int givenPosition, T newEntry) { venPosition >=1)&& (givenPosition <= numberofentries) ) { Node desiredNode = getNodeAt (givenPosition); ToriginalEntry = desiredNode.getData( ); return originalData (newEntry); } // end if throw new IndexOut0fBoundsException("Illegal position given to replace operation."); An interface for a collection that has a first entry and a last entry. @author Frank M. Carrano Qversion 5.0 / ablic interface DoubleEndedInterface {/ * Adds a new entry to the beginning of this collection. * @param newEntry The object to be added as a new entry. 1 public void addFirst(T newEntry); /** * Adds a new entry to the end of this collection. * Qparam newEntry The object to be added as a new entry. / public void addLast( T newEntry); /** * Removes and returns the first entry in this collection. * areturn A reference to the removed entry or null, if * the list was empty. / public T removefirst(); /** * Removes and returns the last entry in this collection. * Qreturn A reference to the removed entry or null, if * the list was empty. / public T removelast(); /** * Retrieves the first entry in this collection. * areturn A reference to the first entry or null, if * the list is empty. 1 public T getFirst(); /** * Retrieves the last entry in this collection. * Qreturn A reference to the last entry or null, if * the list is empty. / public T getLast(); /** Moves the first entry in this collection to the end of the list. */ public void
  • 4.
    moveToEnd(); // endDoubleEndedInterface ver that demonstrates the class DoubleEndedList. thest only the additional methods, since the methods or Frank M. Carrano ion 5.0 lass Driver { lass Driver { public static void main(String[] args) { System.out.println("Create an empty list."); System. out.print ln("Create an empty list."); DoubleEndedList < String > myList = new DoubleEndedList <(); System.out.println("List should be empty; isEmpty() returns " + myList.isEmpty() + "."); System.out.print ln("nTesting addFirst:"); myList.addFirst ("15"); myList. addFirst (" 25 ") mylist.addFirst (" 45) System.out.println("List should contain 453525 15."); myList.clear(); mylist.clear(); myList. addLast ("15"); myList.addLast ("35") myList.addLast (" 45) myList.addLast ("55"); myList. addLast ("65"); displaylist (myList); System.out.print ln("nTesting removeFirst with previous list:"); System.out.println("removeFirst (): " + myList. removeFirst ()); System. out.println("nList should containn45 5565 "); displayList (myList); System. out.println("removefirst(): " + myList. removeFirst()); System. out.println("removefirst(): " + myList. removefirst()); System. out.println("List should be empty; isEmpty() returns" + myList.isEmpty() + "."); myList.clear(); myList. addLast ("15"); myList.addLast (" 25 ") myList. addLast ("45"); myList. addLast ("55"); System.out.println("List should contain 1525354555 65."); displaylist (myList); System.out.println("removeLast (): " + myList.removeLast()); System.out.println("removeLast (): " + myList.removeLast ()); System. out.print ln ("removeLast (): " + myList. removeLast ()) System. out.print ln ("removeLast (): " + mylist.removelast ()); System.out.println("nList should contain n15"); displaylist (myList); ( ) System.out.print ln("nTesting getFirst and getlast with the following list:"); myList.clear() myList.addLast ("15"); myList.addLast (" 25 "); myList. addLast ("35"); myList. addLast (" 55) myList.addLast ("65"); displayList (myList): System.out.println("Retrieving the first entry : returns " + myList.getFirst()); System.out.print ln("nTesting moveToEnd with the following list:"); displayList (myList); myList. moveToEnd () System. out.println("After moveToEnd, the list is:"); displaylist (myList); myList. moveToEnd ( ) System.out.println("After moveToEnd, the list is:"); displaylist (myList); myList. moveToEnd ( ) System.out.println("After moveToEnd, the list is:"); displaylist (mylist); System. out.print ln ("nnDone."); public static void displayList(ListInterface aList) { System.out.println("The list contains " + aList.getLength() + "string(s), as follows:"); Object [] listArray = aList. toArray(); for (int index =0; index < listarray. length; index++) { } // end for System.out.print ln(); for (int position =1; position <= listarray. length; position++) { if (position < 10) else System.out.print(" " + position); System.out.print (position); System.out.print(" "); }//endfor System.out.print ln(); H/ end Driver e an empty list. should be empty; isEmpty() returns true. son list. ng addFirst: should contain 45352515 ist contains 4 string(s), as follows: 4 ng addlast with the following list: should contain
  • 5.
    152535455565. ist contains456 * Testing removefirst with previous list: * removefirst(): 15 * removeFirst(): 25 * removeFirst(): 35 * List should contain * 455565 * The list contains 3 string(s), as follows: * 455565 * 123 * removeFirst(): 45 * removeFirst(): 55 * removeFirst(): 65 * removefirst(): null * List should be empty; isEmpty() returns true. * * Testing removelast with the following list: * List should contain 152535455565 . * The list contains 6 string(s), as follows: * 152535455565 * 123456 * removeLast (): 65 * removelast (): 55 * removelast (): 45 * removelast (): 35 * removeLast (): 25 * List should contain * 15 * The list contains 1 string(s), as follows: * 15 * 1 * removelast (): 15 * removelast (): null * List should be empty; isEmpty() returns true. * Testing getFirst and getlast with the following list: * The list contains 6 string(s), as follows: * 152535455565 * 123456 * Retrieving the first entry : returns 15 * Retrieving the last entry : returns 65 * Testing moveToEnd with the following list: * The list contains 6 string(s), as follows: * 152535455565 * 123456 * After moveToEnd, the list is: * The list contains 6 string(s), as follows: * 253545556515 * 123456 * After moveToEnd, the list is: * The list contains 6 string(s), as follows: * 354555651525 * 123456 * After moveToEnd, the list is: * The list contains 6 string(s), as follows: * 455565152535 * 123456 * * * Done. *I * Entries in a list have positions that begin with 1. * eauthor Frank M. Carrano * dauthor Timothy M. Henry * aversion 5.0 */ ublic interface ListInterface { /** * Adds a new entry to the end of this list. * Entries currently in the list are unaffected. * The list's size is increased by 1 . * Gparam newEntry The object to be added as a new entry. */ public void add( T newEntry); /** * Adds a new entry at a specified position within this list. * Entries originally at and above the specified position * are at the next higher position within the list. * The list's size is increased by 1 . * Caparam newPosition An integer that specifies the desired * position of the new entry. * Caparam newEntry The object to be added as a new entry. * Gathrows IndexoutofBoundsException if either newPosition < 1 or newPosition > */ getLength ()+1. public void add(int newPosition, T newEntry); /*** * Removes the entry at a given position from this list. * Entries originally at positions higher than the given * position are at the next lower position within the list, * and the list's size is decreased by 1 . * eparam givenPosition An integer that indicates the position of the entry to be removed. * ereturn A reference to the removed entry. * athrows IndexOutofBoundsException if either * givenPosition <1 or givenPosition > */ getLength(). public T remove(int givenPosition); /** Removes all entries from this list. */ public void clear(); /** * Replaces the entry at a given position in this list. * Gparam givenPosition An integer that indicates the position of * the entry to be replaced. * eparam newEntry The object that will replace the entry at the * areturn The original position givenPosition. * athrows
  • 6.
    Index0utal entry thatwas replaced. * givenPosition <1 or givenPosition > */ givenPosition < 1 or givenPosition > getLength(). public T replace(int givenPosition, T newEntry); / ** Retrieves the entry at a given position in this list. * @param givenPosition An integer that indicates the position of the desired entry. * areturn A reference to the indicated entry. * Gthrows Index0ut0fBoundsException if either givenPosition < 1 or givenPosition > */ getLength ( ). public T getEntry(int givenPosition); /** * Retrieves all entries that are in this list in the order in which * they occur in the list. * Greturn A newly allocated array of all the entries in the list. * If the list is empty, the returned array is empty. public T[] toArray(); /*** * Sees whether this list contains a given entry. * eparam anEntry The object that is the desired entry. * Greturn True if the list contains anEntry, or false if not. */ public boolean contains ( T anEntry); /*** * Gets the length of this list. * areturn The integer number of entries currently in the list. public int getLength(); /** * Sees whether this list is empty. * ereturn True if the list is empty, or false if not. * a public boolean isEmpty(); // end ListInterface