[英]Iterating a linked list
我在迭代任务的链表时遇到了一些麻烦。
链表类/节点类(无大小方法,不能修改/添加方法)
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;
}
}
这是我试图用来迭代列表但没有运行的内容
while(node.children.front != null) {
System.out.println(node.children.front);
node.children.remove(0);
}
我的完整java文件:
/*** 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;
}
}
根据您拥有迭代器的方式,在打印出来后可能会破坏链表。 通常,在使用链表时,您希望保留该列表。 下面是遍历链表的基本概念。
while(node.children.front != null) {
System.out.println(node.children.front);
node.children.front = node.children.front.next
}
在任何时候,您只能访问单个节点,因此如果您想转到下一个节点,则必须将当前节点设置为列表中的下一个。 由于它可能为空,这就是为什么您要条件检查节点是否设置为空值的原因。 如果它设置为空值,则您位于链表的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.