Question

In: Computer Science

i'm getting an infinite loop on this code: can you explain what I should do? public...

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");

}

}

}

  

Solutions

Expert Solution

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.


Related Solutions

I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000...
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000 1 0 2 80400000 2 ffffffff 3 #include <stdio.h> #include <math.h> #include <stdlib.h> int count = 31; void toBinary(int num, int n){ for(int i = 1; i < n; i++){ if((int)(num/pow(2,(n-i))) > 0){ num = num - pow(2,(n-i)); printf("1"); count--; }else{ printf("0"); count--; } } } char checkSign (int sign) { char new_sign; if (sign == 0) { new_sign = '+'; return new_sign; }...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Question 1: What is For Loop? Explain with 2 Examples. You can write code in any...
Question 1: What is For Loop? Explain with 2 Examples. You can write code in any programming language.
The following is an infinite loop: for (int i = 0; i < 10; i++); System.out.println(i...
The following is an infinite loop: for (int i = 0; i < 10; i++); System.out.println(i + 4); TRUE OR FALSE?
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT