【问题标题】:How to break random elements in doubly linkedList如何打破双向链表中的随机元素
【发布时间】: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


【解决方案1】:
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);
    }
}

【讨论】:

    【解决方案2】:

    我不知道我是否正确地满足了您的要求。但是这里有一个循环,可以用你的 tete 和队列实现一个双向链表。 cmets解释了逻辑。

    class LC {
       public int data;
       public LC suiv;
       public LC prec;
    }
    
    public class ChainesDouble {
    public static int getRandom(){      
        int N = (int)(Math.random()*42);        
        if(N<21) {          
            N -=30;
        }       else {          
            N-=11;          
        }       
        return N;   
    }
    
    public static void main(String[] args) {
        int N = (int)(Math.random()*16)+5;
        N = (N*2);
        System.out.println("La valeur de N = " + N);
    
        LC tete = null;
        LC queue = null;
        for (int i = 0; i < N/2; i++) {
            int valeur = getRandom();
            int next = getRandom();
            
            //get the two random values
            LC temp_a = new LC();
            LC temp_b = new LC();
            
            //Store the data in the two nodes
            temp_a.data = valeur;
            temp_b.data = next ;  
            
            //link the two nodes
            temp_a.suiv = temp_b;
            temp_b.prec = temp_a;
            
            //If the list is empty, then initialize tete(head) and queue(tail)
            if(tete == null) {
                tete = temp_a;
                queue = temp_b;
            } else {
                
                if(valeur <0) { //If valeur is negative, add to tete
                        temp_b.suiv = tete;
                        tete.prec = temp_b;
                        tete = temp_a;
                }
                else { //If valeur is positive, add to queue
                       queue.suiv = temp_a;           
                       temp_a.prec =  queue;
                       queue = temp_b;
                }
            }
            
        }
        
        //Test Program
        LC temp = tete;
        while (temp!=null) {
            System.out.println(temp.data);
            temp=temp.suiv;
        }
    
        //Search for second multiple of 5
        LC search = tete;
        int count = 0;
        boolean found = false;
        while (search!=null) {
            if (search.data%5==0)
                count++;
            if (count==2) {
                found = true;
                System.out.println("Found "+search.data);
                break;
            }
            search = search.suiv;
        }
        
        //if found
        if (found) {
            int position = 5;
            if (search.data%10==0) position = 10;
            System.out.println("Position "+position);
            
           //remove search for current position
            if (search.suiv!=null) {
                LC prev = search.prec;
                LC next = search.suiv;
                prev.suiv = next;
                next.prec = prev;
            } else {
                search.prec.suiv = null;
            }
            
            //move pointer to desired position
            LC move = tete;
            int cur = 1;
            while(move!=null) {
                move = move.suiv;
                cur++;
                if (cur==(position - 1)) {
                    break;
                }
            }
            System.out.println("shifting "+search.data+" to after "+move.data);
            //link searched item into desired position
    
                search.suiv = move.suiv;
                move.suiv.prec = search;
                move.suiv = search;
                search.prec = move;
    
            
        }
     }
     }
    

    【讨论】:

    • 我刚刚使用了你的方法,它有效,但它没有打印出 N 个元素的整个列表
    • @QuangCao,我测试了它,它运行良好。它展示了多少元素,你期待什么?
    • 例如当它说 N=10 时它只打印出 3 个元素,有时它会抛出一个空指针异常
    • 让我粘贴整个程序
    • 这可能是因为我的程序的其他部分,但我不能在这里发布整个内容它太长了哈哈@Kasalwe
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多