Question: Write a method in the LList class that finds the first occurrence of a given value in a linked list and if found, returns its
Write a method in the LList class that finds the first occurrence of a given value in a linked list and if found, returns its position in the list, otherwise returns The targeted value should be a parameter of the method of the generic type T Include testing of the method in the main method of the LList package lab;
A doublylinked implementation of the ADT list.
@author Frank M Carrano
@author Joseph Erickson
@version
public class DoublyLList implements ListInterface
private Node firstNode; Reference to first node
private int numberOfEntries;
public DoublyLList
initializeDataFields;
end default constructor
public final void clear Note the final method
initializeDataFields;
end clear
public void addT newEntry OutOfMemoryError possible
Node newNode new NodenewEntry;
if isEmpty
firstNode newNode;
else Add to end of nonempty list
Node lastNode getNodeAtnumberOfEntries;
lastNode.setNextNodenewNode; Make last node reference new node
newNode.setPreviousNodelastNode;
end else
numberOfEntries;
end add
public void addint newPosition, T newEntry OutOfMemoryError possible
if newPosition && newPosition numberOfEntries
Node newNode new NodenewEntry;
if newPosition Case
newNode.setNextNodefirstNode;
if firstNode null
firstNode.setPreviousNodenewNode;
firstNode newNode;
end if
else Case : List is not empty
and newPosition
Node nodeBefore getNodeAtnewPosition ;
Node nodeAfter nodeBefore.getNextNode;
newNode.setNextNodenodeAfter;
newNode.setPreviousNodenodeBefore;
nodeBefore.setNextNodenewNode;
if nodeAfter null
nodeAfter.setPreviousNodenewNode;
end else
numberOfEntries;
end if
else
throw new IndexOutOfBoundsExceptionIllegal position given to add operation.";
end add
public T removeint givenPosition
T result null; Return value
if givenPosition && givenPosition numberOfEntries
assert isEmpty;
if givenPosition Case : Remove first entry
result firstNode.getData; Save entry to be removed
firstNode firstNode.getNextNode;
firstNode.setPreviousNodenull;
end if
else Case : Not first entry
Node nodeBefore getNodeAtgivenPosition ;
Node nodeToRemove nodeBefore.getNextNode;
Node nodeAfter nodeToRemove.getNextNode;
nodeBefore.setNextNodenodeAfter;
if nodeAfter null
nodeAfter.setPreviousNodenodeBefore;
result nodeToRemove.getData; Save entry to be removed
end else
numberOfEntries;
return result;
end if
else
throw new IndexOutOfBoundsExceptionIllegal position given to remove operation.";
end remove
public T replaceint givenPosition, T newEntry
if givenPosition && givenPosition numberOfEntries
assert isEmpty;
Node desiredNode getNodeAtgivenPosition;
T originalEntry desiredNode.getData;
desiredNode.setDatanewEntry;
return originalEntry;
end if
else
throw new IndexOutOfBoundsExceptionIllegal position given to replace operation.";
end replace
public T getEntryint givenPosition
if givenPosition && givenPosition numberOfEntries
assert isEmpty;
return getNodeAtgivenPositiongetData;
end if
else
throw new IndexOutOfBoundsExceptionIllegal position given to getEntry operation.";
end getEntry
public boolean containsT anEntry
boolean found false;
Node currentNode firstNode;
while found && currentNode null
if anEntryequalscurrentNodegetData
found true;
else
currentNode currentNode.getNextNode;
end while
return found;
end contains
public int getLength
return numberOfEntries;
end getLength
public boolean isEmpty
boolean result;
if numberOfEntries Or getLength
assert firstNode null;
result true;
end if
else
assert firstNode null;
result false;
end else
return result;
end isEmpty
public T toArray
The cast is safe because the new array contains null entries
@SuppressWarningsunchecked
T result Tnew ObjectnumberOfEntries; Unchecked cast
int index ;
Node currentNode firstNode;
while index numberOfEntries && currentNode null
resultindex currentNode.getData;
currentNode currentNode.getNextNode;
index;
end while
return result;
end toArray
Initializes the class's data fields to indicate an empty list.
private void initializeDataFields
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
