Search code examples
javalinked-listtestcase

how I can count the total number of nodes List Structures in java


I'm trying to write a function to return the number of nodes in the list in java.

I have a class name waypoint, it define the point and other class named TourElement. TourElement is used to create nodes which are contain a point.

//Waypoint

public class Waypoint {
    int x  ;
    int y  ;
    public int getX()
    {
        return this.x;
    }
    public int getY()
    {
        return this.y;
    }
    public void setXY(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

//Tour Element

public class TourElement {
 private Waypoint points;
 private TourElement next;
  public void setWaypoint( Waypoint points)
 {
   this.points = points; 
 }
  public void setTourElement(TourElement next)
  {
      this.next = next;
  }
 Waypoint getWaypoint()
 {
     return this.points;
 }

 TourElement getNext()
 {
     return this.next;
 }

// I have some trouble with getNoOfWaypoints() method, what is wrong with my code?. my method does not pass the test case :

int getNoOfWaypoints()
{
    int count = 1;
    TourElement current = getNext();
    while(current.next != null)
    {
        count++;
        System.out.println(count);
    }
    return count;
}

//test cases are provided by my teacher

  private Waypoint createWaypoint(int x, int y) {
        Waypoint wp = new Waypoint();
        wp.setXY(x, y);
        return wp;
    }


    private TourElement createElementList(int[][] waypoints){
        assert waypoints.length > 0;
        TourElement elem = new TourElement();
        int lastIndex = waypoints.length-1;
        Waypoint wp = createWaypoint(waypoints[lastIndex][0], waypoints[lastIndex][1]);
        elem.setWaypoint(wp);
        for (int i = lastIndex-1; i >= 0 ; i--) {
            wp = createWaypoint(waypoints[i][0], waypoints[i][1]);
            elem = elem.addStart(wp);
        }
        return elem;
    }



public void testGetNoOfWaypoints_NotChangingList() {
        TourElement elem = createElementList(new int[][] {{0, 0}, {1, 1}, {2, 2}});
        int unused = elem.getNoOfWaypoints();

        assertArrayEquals(new int[] {0, 0}, elem.getWaypoint().toArray());
        assertArrayEquals(new int[] {1, 1}, elem.getNext().getWaypoint().toArray());
        assertArrayEquals(new int[] {2, 2}, elem.getNext().getNext().getWaypoint().toArray());
        assertNull(elem.getNext().getNext().getNext());
    }

I dont know what is problem with my out put. and I really want to know how to pass the testcase. Please help me figure it out. thank u so much in advance!!


Solution

  • The condition (current.next != null) will be either always false or always true, since you never modify current inside the loop.

    It should be:

    int getNoOfWaypoints()
    {
        int count = 1;
        TourElement current = getNext();
        while(current.next != null)
        {
            count++;
            System.out.println(count);
            current = current.next;
        }
        return count;
    }