0

when i run my program from the main i get the error on this line: private class Node

I have a (circular) linked list class with a bunch of methods (only the first one shown here) and then a separate main. I'd like to know what is causing this issue. Any help would be greatly appreciated. thanks.

    public class CircleGame 
    {
        //Node class (inner class)
        private class Node
        {
            private int data;
            private Node next;

            //constructor of Node
            private Node(int data, Node next)
            {
                this.data = data;
                this.next = next;

            }
        }

        /****************************************************/

        private Node head;
        int size =0;
        int k = 0;
        int s = 0;

        //constructor of list
        public CircleGame()
        {
            head = null;

        }

        /****************************************************/

        public void addToEnd(int data)
        {
            //insert at beginning
            if (head == null)
                head = new Node(data, null);

            //insert at other positions
            else 
            { Node temp = head;
                int i=0;
                while(i <size)
                { 
                    temp = temp.next;
                    temp.next = new Node(data, head); 

                    i++;
                }
            }

            size ++;
        }

        /**************************************************/

    //main 
   import java.util.*;

public class CircleGameMain
{
    public static void main (String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        CircleGame game = new CircleGame();

        System.out.println("***********WELCOME TO The CIRCLE GAME************");
        System.out.print("How many players are in the circle? ");
        int size = keyboard.nextInt();
        System.out.println();
        int data;

        for(int i = 0; i < size; i++) 
        {
            data = i+1;
            game.addToEnd(data);
        }

        game.print();
        System.out.print("What position would you like to start at? ");
        int s = keyboard.nextInt();
        System.out.println();

        System.out.print("Eliminate the kth player. k= ? ");
        int k =keyboard.nextInt();
        System.out.println();
        game.game(s,k);

    }
}

1 Answers1

1

One bug that I can spot is that the following line:

temp.next = new Node(data, head);

should be executed right after the while loop - not inside it!

Nir Alfasi
  • 49,889
  • 11
  • 75
  • 119
  • I think the problem is in addToEnd() , head is null but it's being treated as if it's not. i need a way to make the linked list circular while adding data. – Eternal Punishment Jan 21 '15 at 04:41
  • To make it circular, after you add the new node at the end - make its next to point to head: before `size++;` add the line: `temp.next = head;` – Nir Alfasi Jan 21 '15 at 06:50