Make the following into a circular linked list. Show what was done by highlighti
ID: 3673743 • Letter: M
Question
Make the following into a circular linked list. Show what was done by highlighting of making changes in bold. Thanks
public class LinkedList<AnyType> {
private ListNode<AnyType> header;
// construct the list
public LinkedList(){
header = new ListNode<AnyType>(null);
}
// test if the list is logically empty
public boolean isEmpty(){
return header.next == null;
}
public void makeEmpty(){
header.next = null;
}
// return an iterator representing the header node
public LinkedListIterator<AnyType> zeroth(){
return new LinkedListIterator<AnyType>(header);
}
// return an iterator representing the first node in the list
public LinkedListIterator<AnyType> first(){
return new LinkedListIterator<AnyType>(header.next);
}
public void insert(AnyType x, LinkedListIterator<AnyType> p){
// @param x the item to insert
// @param p the position prior to the newly inserted item
if( p != null && p.current != null)
p.current.next = new ListNode<AnyType>(x, p.current.next);
}
public LinkedListIterator<AnyType> find(AnyType x){
ListNode<AnyType> itr = header.next;
while(itr != null && !itr.element.equals(x))
itr = itr.next;
return new LinkedListIterator<AnyType>(itr);
}
// remove first occurrence of an item
public LinkedListIterator<AnyType> findPrevious(AnyType x){
ListNode<AnyType> itr = header;
while(itr.next != null && !itr.next.element.equals(x))
itr = itr.next;
return new LinkedListIterator<AnyType>(itr);
}
public void remove (AnyType x){
LinkedListIterator<AnyType> p = findPrevious(x);
if(p.current.next != null)
p.current.next = p.current.next.next; //bypass deleted node
}
// simple print method
public static <AnyType> void printList(LinkedList<AnyType> theList){
if(theList.isEmpty())
System.out.print("Empty list");
else{
LinkedListIterator<AnyType> itr = theList.first();
for( ; itr.isValid(); itr.advance())
System.out.print(" " + itr.retrieve() + "");
}
System.out.println();
}
public class ListNode<AnyType> {
public AnyType element;
public ListNode next;
public ListNode(AnyType theElement){
this(theElement, null);
}
public ListNode(AnyType theElement, ListNode<AnyType> n){
element = theElement;
next = n;
}
}
}
Explanation / Answer
Hi You can use This LinkedList.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.