我的任务是制作一个包含5, 7, 3,5, 7, 3
元素且5, 7, 3
重复100次的链接列表
IntNode first = new IntNode() ;
first.value = 5 ; //first -> 5
first.next = new IntNode() ;
first.next.value = 7 ; //first -> 5 -> 7
first.next.next.next = new IntNode() ;
first.next.next.next.value = 3 ;
而不是仅仅继续添加重复元素x次,是否有任何方法可以使用循环来创建链接列表。
答案 0 :(得分:0)
尝试这种方式:
// IntNode first = new IntNode();
// first.value = 5; // first -> 5
// first.next = new IntNode();
// first.next.value = 7; // first -> 5 -> 7
// first.next.next.next = new IntNode();
// first.next.next.next.value = 3;
int values[] = { 5, 7, 3 };
IntNode first = new IntNode();
for (int i = 0; i < 100; i++) {
IntNode temp = new IntNode();
temp.value = values[i % 3];
temp.next = new IntNode();
first.next = temp;
first = first.next;
}