22Add a JAVA method to the LList class. LList implements ListInterface using lin
ID: 671922 • Letter: 2
Question
22Add a JAVA method to the LList class. LList implements ListInterface using linked nodes. You can assume the following methods are already implemented:
public boolean add(T newEntry)
public boolean add(int newPosition, T newEntry)
public boolean isEmpty()
public boolean contains(T anObject)
public boolean remove(int givenPosition)
public boolean replace(int givenPosition, T newEntry)
public T getEntry(int givenPosition)
public int getLength()
public void clear()
Write a complete JAVA method at the implementation level called display.
#The method header is:
public void display()
The method prints to the console the index of each element along with the element. Be sure to account for all possible times when the method could be invoked. For full credit, write a method that is efficient.
For this question, you are writing code at the implementation level. This means you have access to the private instance data variables:
private Node firstNode
private int numberOfEntries
Explanation / Answer
public void display(){
// declare a temporary node pointing to the head
Node temp = firstNode;
// loop through the list
for(int i = 0; i < numberOfEntries; i++){
// display the index and the data of the node
System.out.println(i + " " + temp.data);
// set temp = temp.next. Temp now points to the next node
temp = temp.next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.