In: Computer Science
i'm getting an infinite loop on this code: can you explain what I should do?
public class TriviaLinkedList {
private TriviaNode head;
private int items;
public TriviaLinkedList(TriviaNode head, int items) {
this.head = head;
this.items = items;
}
public TriviaNode getHead() {
return head;
}
public void setHead(TriviaNode head) {
this.head = head;
}
public int getItems() {
return items;
}
public void setItems(int items) {
this.items = items;
}
public String toString() {
return "head=" + head + ", items=" + items;
}
public void insertList(TriviaNode node){
if(head == null){
head = node;
}
else {
TriviaNode node1 = head;
head = node1;
head.next = node1;
}
this.items++;
}
public void deleteList(int id){
TriviaNode first = head;
TriviaNode second = head;
while(first!=null) {
if(first.getGame().getId() == id) {
if(first == head) {
head = first.next;
}
else if(first.next != null) {
second.next = first.next;
}
else {
second.next = null;
this.items--;
System.out.println("Game: "+id+" was deleted");
return;
}
second = first;
first = first.next;
}
System.out.println("Unfortunately Game: "+id+" was not found");
}
}
}
public class TriviaLinkedList {
    private TriviaNode head;
    private int items;
    public TriviaLinkedList(TriviaNode head,
int items) {
        this.head =
head;
        this.items =
items;
    }
    public TriviaNode getHead() {
    return head;
    }
    public void setHead(TriviaNode head)
{
    this.head = head;
    }
    public int getItems() {
    return items;
    }
    public void setItems(int items) {
    this.items = items;
    }
    public String toString() {
    return "head=" + head + ", items=" +
items;
    }
      
    public void insertList(TriviaNode
node){
        if(head ==
null){
            head
= node;
        }
        else {
            TriviaNode
node1 = head;
            head
= node; // It should be node, not node1
            head.next
= node1;
        }
        this.items++;
    }
      
    public void deleteList(int id){
// empty list
        if(head == null)
{
            return;
        }
        TriviaNode current
= head;
        TriviaNode prev =
null;
        
        // check for hed
first, special case
        if(head.getGame().getId()
== id) {
            head
= head.next;
            this.items--;
            return;
        }
        
        // Now node to be
deleted can not be head
        while(current !=
null) {
            if(current.getGame().getId()
== id) {
                prev.next
= current.next;
                this.items--;
                System.out.println("Game:
"+id+" was deleted");
                return;
            }
            
            //
else, check next node.
            prev
= current;
            current
= current.next;
        }
        System.out.println("Unfortunately
Game: "+id+" was not found");
    }
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.