title: Circular linked list help (Java)
body: <p>I'm having some difficulty with a program I'm working on. I've tried to get help from some of my peers, but we can't figure it out. I'm sure its something simple, but I don't have quite enough experience to figure it out on my one. Here's the assignment:</p>
  
  <p>In an ancient land, the beautiful princess Eve had many suitors. She decided on the following procedure to determine which suitor she would marry. First, all of the suitors would be lined up one after the other and assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at the suitor in the first position, she would then count three suitors down the line (because of the three letters in her name), and the third suitor would be eliminated from winning her hand and removed from the line. Eve would then continue, counting three more suitors, and eliminate every third suitor. When she reached the end of the line, she would continue counting from the beginning.</p>
  
  <p>For example, if there were six suitors, the elimination process would proceed as follows:</p>
  
  <pre><code>123456   Initial list of suitors: start counting from 1.
  12456       Suitor 3 eliminated:  continue counting from 4.
  1245        Suitor 6 eliminated:  continue counting from 1.
  125     Suitor 4 eliminated:  continue counting from 5.
  15      Suitor 2 eliminated:  continue counting from 5.
  1       Suitor 5 eliminated:  1 is the lucky winner.
  </code></pre>
  
  <p>Write a program that creates a circular linked list of nodes to determine which position you should stand in to marry the princess if there are n suitors. Your program should simulate the elimination process by deleting the node that corresponds to the suitor that is eliminated for each step in the process.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>    public class CircularLinkedList
      {
        private class Node
        {
          private int candidate;
          private Node next;
          public Node()
          {
            candidate = 0;
            next = null;
          }
          public Node(int newCandidate)
          {
            candidate = newCandidate;
            next = null;
          }
          public Node(int newCandidate, Node nextValue)
          {
            candidate = newCandidate;
            next = nextValue;
          }
        }//end inner class
        private Node first;
        private Node last;
        public CircularLinkedList()
        {
          first = null;
          last = null;
        }
        public boolean isEmpty() //checks to see if the list is empty
        {
          return first == null;
        }
        public int size() //checks the size
        {
          int count = 0; 
          Node p = first;
          while(p != last)
          {
            count++;
            p = p.next;
          } 
          return count;
        }
        public void add(int e) //adds a Node to the list
        {
          Node newEntry = new Node(e, first);
          if(first==null)
          {
            last = newEntry;
            first = last;
          }
          else
          {
            last.next = newEntry;
            last = last.next;
          }
        }
        public void removeAfter(Node e) //removes the Node after the current Node
        {
          e.next = e.next.next;
        }
        public void remove() //removes every third Node 
        {
          Node curNode = first;
          int size = size();
          while(size&gt;1)
          {
            curNode = curNode.next;
            removeAfter(curNode);
            size -= 1;
          }
        }
        public void print() //prints the list
        {
          Node ref = first;
          for(int index = -1;index&lt;size();index++)
          {
            System.out.print(ref.candidate + " ");
            ref = ref.next;
          }
        }
      }
  </code></pre>
  
  <p>And the main method...</p>
  
  <pre><code>    import java.util.Scanner;
      public class CircularDemo
      {
        public static void main(String[] args)
        {
          CircularLinkedList c = new CircularLinkedList();
          System.out.println("How many candidates are there?");
          Scanner keyboard = new Scanner(System.in);
          int input = keyboard.nextInt();
          if(input&lt;=0)
            System.out.println("No candidates presented.");
          else if(input==1)
            System.out.println("Candidate 1 is the only candidate and the winner.");
          else
          {
            for(int i = 1; i&lt;=input; i++)
            {       
              c.add(i);
            }
            System.out.print("The candidates are: ");
            c.print();
            System.out.println("\n" + c.size()); //tests size
            while(c.size()!=1)
            {
              c.remove();
              c.print();
            }
            System.out.print("\nThe winner is: ");
            c.print();
          }
        }
      }
  </code></pre>
  
  <p>The main problem I'm having is the part where it tries to remove every 3rd Node in the main method (the while(c.size()!=1)). It'll print everything before this point correctly. Once it reaches this point, its like the program dies. If I take the while loop out, the winner part will print out, so something in the loop is messing up.</p>
  
  <p>I think I know where the problem is. The first one is in the size() method. It prints one less than there really is. If I put 6, it thinks there are 5. Since this is a circular linked list, I can't just let it continue counting until it reaches a null. There will never be one. This is one issue, but I'm not sure how to fix it.</p>
  
  <p>The second, I think is in the remove() and removeAfter() methods. I tried testing it out, but the program just ends when it tries to read it. I'm not really sure what's going on with these programs, and I had a difficult enough time trying to figure out how to create it... So this is the biggest issue.</p>
  
  <p>Plus here's the output I've gotten so far, just to give you an idea:</p>
  
  <pre><code>    How many candidates are there?
      6 //user input
      The candidates are: 1 2 3 4 5 6
      5 //output for size()
      1 2 6 //output first time through while loop
      //output for winner still not showing up
  </code></pre>
  
  <p><em>edit 1</em> - updated while loop in delete. Output updated.</p>