简体   繁体   中英

Iterating a linked list

I am having a little trouble iterating a linked list for an assignment.

Linked list class / Node class (no size methods & cannot modify/add methods)

class MyGenericLinkedList<S> {
    Node<S> front;
 
    public MyGenericLinkedList() {
        front = null;
    }
 
    public void add(S value) {
        if (front == null) {
            front = new Node<S>(value);
        } else {
            Node<S> current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node<S>(value);
        }
    }
 
    public S get(int index) {
        Node<S> current = front;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        return (S)current.data;
    }
 
    public void remove(int index) {
        if (index == 0) {
            front = front.next;
        } else {
            Node<S> current = front;
            for (int i = 0; i < index - 1; i++) {
                current = current.next;
            }
            current.next = current.next.next;
        }
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS Node  ***/
class Node<X> {
    X data;
    Node<X> next;
 
    public Node(X data) {
        this.data = data;
        this.next = null;
    }
 
    public Node(X data, Node<X> next) {
        this.data = data;
        this.next = next;
    }
}

Here is what I am trying to use to iterate the list but is not running

while(node.children.front != null) {
            System.out.println(node.children.front);
            node.children.remove(0);
        }

My full java file:

/***  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<TreeNode> 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<Integer> tree = new MyGenericTree<Integer>(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<T> {
    private TreeNode<T> root = null;
 
    public MyGenericTree(TreeNode<T> root) {
        this.root = root;
    }
 
    public void traverseInPostOrder() {
        traverseInPostOrder(root);
    }
 
    public void traverseInPostOrder(TreeNode<T> node) {     
        //---------------------------------------------------------------------
        System.out.println("1");
        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<Integer> currNode = new MyGenericTree<Integer>(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<N> {
    N data = null;
    TreeNode<N> parent = null;
    MyGenericLinkedList<TreeNode<N>> children = null;
 
    public TreeNode(N data) {
        this.data = data;
    }
 
    public void setChildren(MyGenericLinkedList<TreeNode<N>> children) {
        this.children = children;
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS MyGenericLinkedList  ***/
class MyGenericLinkedList<S> {
    Node<S> front;
 
    public MyGenericLinkedList() {
        front = null;
    }
 
    public void add(S value) {
        if (front == null) {
            front = new Node<S>(value);
        } else {
            Node<S> current = front;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node<S>(value);
        }
    }
 
    public S get(int index) {
        Node<S> current = front;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        return (S)current.data;
    }
 
    public void remove(int index) {
        if (index == 0) {
            front = front.next;
        } else {
            Node<S> current = front;
            for (int i = 0; i < index - 1; i++) {
                current = current.next;
            }
            current.next = current.next.next;
        }
    }
}
 
/***  DO NOT MAKE ANY CHANGE TO CLASS Node  ***/
class Node<X> {
    X data;
    Node<X> next;
 
    public Node(X data) {
        this.data = data;
        this.next = null;
    }
 
    public Node(X data, Node<X> next) {
        this.data = data;
        this.next = next;
    }
}

Based on how you have the iterator, that can potentially destroy the linked list after having it print out. Generally, when working with linked lists, you want to keep the list. Below is the basic concept of iterating through a linked list.

while(node.children.front != null) {
    System.out.println(node.children.front);
    node.children.front = node.children.front.next
}

At any point, you only have access to single node, so if you want to go to the next node, you will have to set the current node to the next in the list. Since it might be null, that is why you'd have a conditional checking if the node is set to a null value. If it is set to a null value, you are at the end of the linked list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM