【发布时间】:2020-12-10 04:19:05
【问题描述】:
你好,我正在尝试生成一个随机双向链表,但我必须将具有负值的节点及其下一个节点(值无关紧要)插入到列表的 HEAD 中,但是当我编译程序时,我陷入了困境带有一个重复数字的无限循环。我想我把列表连接错了,但我不确定。对于上下文 LC 是 NODE 类,tete 是头 queue 是 tail,prev 和 suiv 以及 next 和 previous 指针。
class LC {
public int data;
public LC suiv;
public LC prec;
}
public class ChainesDouble {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Détermine an even number N between 10 and 30
int N = (int)(Math.random()*16)+5;
N = (N*2);
System.out.println("La valeur de N = " + N);
// Create a doubly linkedlist with N elements
LC tete = null;
LC queue = null;
for (int i = 0; i < N/2; i++) {
int valeur = getRandom();
int next = getRandom();
//If the generated number is negative insert that number and
//next value into the head of the list
if(valeur <0) {
LC temp = new LC();
temp.data = valeur;
if(tete == null) {
queue = temp;
}
temp = new LC();
temp.data = next ;
tete.prec = temp ;
temp.suiv = tete ;
tete = temp ;
tete.prec = temp ;
temp.suiv = tete ;
tete = temp ;
//If the number is positive, insert the element and the
//next element into the TAIL of the list
}
else {
LC temp = new LC();
temp.data = valeur;
if(queue == null) {
tete = temp;
queue = temp;
}else {
temp.prec = queue;
queue.suiv = temp ;
queue = temp ;
}
temp.prec = queue;
queue.suiv = temp ;
queue = temp ;
}
}
public static int getRandom(){
int N = (int)(Math.random()*42);
if(N<21) {
N -=30;//Rand(-10;-30)
}
else {
N-=11;//Rand(10;30)
}
return N;
}
}
【问题讨论】:
-
请务必提供完整的 sn-p。由于没有两个已关闭的
},您的甚至无法编译。LC的实现是什么? -
@oleg.cherednik 我刚刚编辑了 sn-p。对此感到抱歉
-
getRnadom()实施? -
我已经添加了@oleg.cherednik
-
如果您将每个句子的第一个字母大写,您的文本将更具可读性。
标签: java linked-list