title: Iterating a linked list
body: <p>I am having a little trouble iterating a linked list for an assignment.</p>
  <p>Linked list class / Node class
  (no size methods &amp; cannot modify/add methods)</p>
  <pre><code>class MyGenericLinkedList&lt;S&gt; {
      Node&lt;S&gt; front;
   
      public MyGenericLinkedList() {
          front = null;
      }
   
      public void add(S value) {
          if (front == null) {
              front = new Node&lt;S&gt;(value);
          } else {
              Node&lt;S&gt; current = front;
              while (current.next != null) {
                  current = current.next;
              }
              current.next = new Node&lt;S&gt;(value);
          }
      }
   
      public S get(int index) {
          Node&lt;S&gt; current = front;
          for (int i = 0; i &lt; index; i++) {
              current = current.next;
          }
          return (S)current.data;
      }
   
      public void remove(int index) {
          if (index == 0) {
              front = front.next;
          } else {
              Node&lt;S&gt; current = front;
              for (int i = 0; i &lt; index - 1; i++) {
                  current = current.next;
              }
              current.next = current.next.next;
          }
      }
  }
   
  /***  DO NOT MAKE ANY CHANGE TO CLASS Node  ***/
  class Node&lt;X&gt; {
      X data;
      Node&lt;X&gt; next;
   
      public Node(X data) {
          this.data = data;
          this.next = null;
      }
   
      public Node(X data, Node&lt;X&gt; next) {
          this.data = data;
          this.next = next;
      }
  }
  </code></pre>
  <p>Here is what I am trying to use to iterate the list but is not running</p>
  <pre><code>while(node.children.front != null) {
              System.out.println(node.children.front);
              node.children.remove(0);
          }
  </code></pre>
  <p>My full java file:</p>
  <pre><code>/***  DO NOT ADD A NEW IMPORT DECLARATION HERE  ***/
   
  /***  DO NOT MAKE ANY CHANGE TO CLASS A5 EXCEPT THE PLACEHOLDER TO FILL IN  ***/
  /***  YOU CANNOT ADD A NEW FIELD VARIABLE  ***/ 
  /***  YOU CANNOT ADD A NEW METHOD DECLARATION  ***/ 
  public class A5 {
      public static void main(String[] args) {
          //---------------------------------------------------------------------
          TreeNode root = new TreeNode(1);
          MyGenericLinkedList&lt;TreeNode&gt; children = new MyGenericLinkedList();
   
          TreeNode two = new TreeNode(2);
          TreeNode three = new TreeNode(3);
          TreeNode four = new TreeNode(4);
          TreeNode five = new TreeNode(5);
          TreeNode six = new TreeNode(6);
          TreeNode seven = new TreeNode(7);
          TreeNode eight = new TreeNode(8);
          TreeNode nine = new TreeNode(9);
          TreeNode ten = new TreeNode(10);
          TreeNode eleven = new TreeNode(11);
          TreeNode twelve = new TreeNode(12);
          TreeNode thirteen = new TreeNode(13);
          TreeNode fourteen = new TreeNode(14);
   
          children.add(two);
          children.add(three);
          children.add(four);
          root.setChildren(children);
          children.remove(0);
          children.remove(0);
          children.remove(0);
   
          children.add(five);
          children.add(six);
          two.setChildren(children);
          children.remove(0);
          children.remove(0);
   
          children.add(ten);
          children.add(eleven);
          four.setChildren(children);
          children.remove(0);
          children.remove(0);
   
          children.add(seven);
          children.add(eight);
          children.add(nine);
          six.setChildren(children);
          children.remove(0);
          children.remove(0);
          children.remove(0);
   
          children.add(twelve);
          ten.setChildren(children);
          children.remove(0);
   
          children.add(thirteen);
          children.add(fourteen);
          twelve.setChildren(children);
          children.remove(0);
          children.remove(0);
          //---------------------------------------------------------------------
   
          /***  DO NOT MAKE ANY CHANGE TO THE FOLLOWING CODE  ***/
          MyGenericTree&lt;Integer&gt; tree = new MyGenericTree&lt;Integer&gt;(root);
          tree.traverseInPostOrder();
      }
  }
   
  /***  DO NOT MAKE ANY CHANGE TO CLASS MyGenericTree EXCEPT THE PLACEHOLDER TO FILL IN  ***/
  /***  YOU CANNOT ADD A NEW FIELD VARIABLE  ***/ 
  /***  YOU CANNOT ADD A NEW METHOD DECLARATION  ***/ 
  class MyGenericTree&lt;T&gt; {
      private TreeNode&lt;T&gt; root = null;
   
      public MyGenericTree(TreeNode&lt;T&gt; root) {
          this.root = root;
      }
   
      public void traverseInPostOrder() {
          traverseInPostOrder(root);
      }
   
      public void traverseInPostOrder(TreeNode&lt;T&gt; node) {     
          //---------------------------------------------------------------------
          System.out.println(&quot;1&quot;);
          while(node.children.front != null) {
              System.out.println(node.children.front);
              node.children.remove(0);
          }
          
          /*  
          if(node.children == null){
                  System.out.print(node.data);
              }
              else{
                  TreeNode curr = node.children.get(0);
                  int i = 1;
                  while(curr != null) {
                  MyGenericTree&lt;Integer&gt; currNode = new MyGenericTree&lt;Integer&gt;(curr);
                  //curr = node.children.get(i);
                  currNode.traverseInPostOrder();
                  //curr = curr.next;\
                  i++;
              }
                  System.out.print(node.data);
              }
              */
          //---------------------------------------------------------------------
      }
  }
   
  /***  DO NOT MAKE ANY CHANGE TO CLASS TreeNode  ***/
  class TreeNode&lt;N&gt; {
      N data = null;
      TreeNode&lt;N&gt; parent = null;
      MyGenericLinkedList&lt;TreeNode&lt;N&gt;&gt; children = null;
   
      public TreeNode(N data) {
          this.data = data;
      }
   
      public void setChildren(MyGenericLinkedList&lt;TreeNode&lt;N&gt;&gt; children) {
          this.children = children;
      }
  }
   
  /***  DO NOT MAKE ANY CHANGE TO CLASS MyGenericLinkedList  ***/
  class MyGenericLinkedList&lt;S&gt; {
      Node&lt;S&gt; front;
   
      public MyGenericLinkedList() {
          front = null;
      }
   
      public void add(S value) {
          if (front == null) {
              front = new Node&lt;S&gt;(value);
          } else {
              Node&lt;S&gt; current = front;
              while (current.next != null) {
                  current = current.next;
              }
              current.next = new Node&lt;S&gt;(value);
          }
      }
   
      public S get(int index) {
          Node&lt;S&gt; current = front;
          for (int i = 0; i &lt; index; i++) {
              current = current.next;
          }
          return (S)current.data;
      }
   
      public void remove(int index) {
          if (index == 0) {
              front = front.next;
          } else {
              Node&lt;S&gt; current = front;
              for (int i = 0; i &lt; index - 1; i++) {
                  current = current.next;
              }
              current.next = current.next.next;
          }
      }
  }
   
  /***  DO NOT MAKE ANY CHANGE TO CLASS Node  ***/
  class Node&lt;X&gt; {
      X data;
      Node&lt;X&gt; next;
   
      public Node(X data) {
          this.data = data;
          this.next = null;
      }
   
      public Node(X data, Node&lt;X&gt; next) {
          this.data = data;
          this.next = next;
      }
  }
  </code></pre>