Brittney P.

asked • 09/24/21

Circular Linked List: indexOf(int)Return the location fo the first occurrence of the value in the list. Return 0 if it is the first item. Return -1 if the item is not in the list.

So throughout my code I did not keep track of the size of the list. My code does add and deletes, but I was wondering how I could implement code where I don't necessarily need the size of the list throughout the code in order to find the index of the value.



Here's what I tried, it might not make much sense:

/**

   * Returns the location of the first occurrence of the value in the list.

   * Returns 0 if it is the first item. return -1 if it isn't in the list

   * @param value 

   */

  public int indexOf(int value){

   int intIndex = 0;

   Node curr_node = tail.next;

   int count = 0;

   

   while(curr_node != null) {

   count++;

   }

   if(count == 0) {

   curr_node = tail;

   intIndex = -1;

   }

   if(curr_node.value == value) {

   intIndex = count - 1;

   }

   if(curr_node.value != value) {

   curr_node = curr_node.next;

   }

   return intIndex;

  }


When I run it, it does not print results (extremely long run time)

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.