如何破坏双链表中的随机元素

问题描述

你好,我试图生成一个随机的双链表,但是我必须将一个负值的节点和它的下一个节点(值无关紧要)插入到列表的头,但是当我编译程序时,我陷入了一个无限循环与一个重复的数字。我认为我连接列表错误,但我不确定。对于上下文LC,它是NODE类,tete是头队列,是tail,prev和suiv,以及下一个和上一个指针。

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;
    }
}

解决方法

我不知道我是否正确地满足了您的要求。但是,这是一个循环,可以使用tete和queue实现双链表。这些注释解释了逻辑。

class AppData {
    static let genericDict: Dictionary<String,Any> = [:]
    
    static func get<T>(_ objID: String) -> T {
        let retVal: T
        retVal = genericDict[objID] as! T
        return retVal
    }
}

class SomeClass {
    let product : String = AppData.get("yoho")
}
,
public static void main(String[] args) {
    Random random = new Random();
    int halfSize = random.nextInt(30) + 1;
    ListNode head = createLinkedList(halfSize,random);
    System.out.println(printToString(head));
}

private static String printToString(ListNode node) {
    StringBuilder buf = new StringBuilder();

    while (node != null) {
        if (buf.length() > 0)
            buf.append("->");
        buf.append(node.value);
        node = node.next;
    }

    return buf.toString();
}

public static ListNode createLinkedList(int halfSize,Random random) {
    ListNode head = null;
    ListNode tail = null;

    for (int i = 0; i < halfSize; i++) {
        int one = getRandomValue(random);
        int two = getRandomValue(random);

        if (one >= 0) {
            tail = addTail(one,tail);
            head = head == null ? tail : head;
            tail = addTail(two,tail);
        } else {
            head = addHead(one,head);
            head = addHead(two,head);
        }
    }

    return head;
}

private static ListNode addHead(int value,ListNode head) {
    ListNode node = new ListNode(value);
    node.next = head;

    if (head != null)
        head.prev = node;

    return node;
}

private static ListNode addTail(int value,ListNode tail) {
    ListNode node = new ListNode(value);
    node.prev = tail;

    if (tail != null)
        tail.next = node;

    return node;
}

private static int getRandomValue(Random random) {
    return (random.nextInt(30) + 1) * (random.nextBoolean() ? 1 : -1);
}

public static final class ListNode {

    public final int value;
    public ListNode next;
    public ListNode prev;

    public ListNode(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}