【问题标题】:Can't print elements in a linked list after merging two arrays together into one list将两个数组合并到一个列表后无法打印链表中的元素
【发布时间】:2020-02-02 19:12:17
【问题描述】:

我正在为 DS 和 Algo 分配两个排序整数列表或数组,然后我必须将它们合并到一个列表中。我决定为每个数组使用两个队列,方法是将每个元素放入队列中,然后进行一些比较并将它们放入一个单链表中。现在我面临的问题是它只是在第一个数组中一遍又一遍地打印第一个元素。我的目标是删除队列中的元素并将它们从最小到最大放入链表中。 此外,while 循环中的计数用于在等于两个数组的长度 - 1 时停止循环。

MergeQueue.java

    int[] A = {1, 3, 5, 7, 9}; // our first array A
    int[] B = {2, 3, 6, 8, 10}; // our second array B
    int length = A.length + B.length - 1; // get the length of both A and B
    //System.out.println(length);
    int count = 0; // this is a counter used to check that once count is equal to the length(A+B) then we break the loop
    int frontA, dequeueA;
    int frontB, dequeueB;

    // we can use a singly linked list to store elements of A and B
    SingleLinkedList S = new SingleLinkedList();
    QueueLinkList queueA = new QueueLinkList(); // our queue for A type integer
    QueueLinkList queueB = new QueueLinkList(); // our queue for B type integer

    // Add elements from A to the queue
    for(int i = 0; i < A.length; i++) {
        // so add element from A to the queue
        queueA.enqueue(A[i]);
    }

    // Add elements from B to the queue
    for(int i = 0; i < B.length; i++) {
        // so add element from B to the queue
        queueB.enqueue(B[i]);
    }
    // now begins the conditions
    while(count != length) {
        // only return the element in the front not remove it
        frontA = queueA.front(); 
        frontB = queueB.front(); 

        if(frontA < frontB) {
            dequeueA = queueA.dequeue(); // remove the element and add it to the linked list
            // add dequeueA to list S
            S.add(dequeueA); // add the element to the singly linked list
            count++; // increment the counter
        }
        else if(frontB < frontA) {
            dequeueB = queueB.dequeue();
            // add dequeueB to list S
            S.add(dequeueB); // add the element to the singly linked list
            count++; // increment the counter
        }
        // if the elements are the same then remove from queue and add anyone.
        else if(frontA == frontB || frontB == frontA) {
            dequeueA = queueA.dequeue();
            dequeueB = queueB.dequeue();
            // add either dequeueA or B
            S.add(dequeueA); // add the element to the singly linked list
            count++; // increment the counter
        }
        // if queue A is empty and queue B is not then add remaining elements from B to S.
        else if(queueA.isEmpty() && !queueB.isEmpty()){
            // add remaining elements from B to the list
            dequeueA = queueA.dequeue(); 
            S.add(dequeueA);
            count++; // increment the counter
        }
        // if queue B is empty and queue A is not then add remaining elements from B to S.
        else if(queueB.isEmpty() && !queueA.isEmpty()) {
            // add remaining elements from B to the list
            dequeueB = queueB.dequeue();
            S.add(dequeueB);
            count++;
        }
    }
    System.out.println("Our set S:");
    S.print(); // call the print method which displays every element in the singly linked list

【问题讨论】:

  • 上面使用的结构,如 SingleLinkedList 和 QueueLinkList 是您的自定义实现,因为在 java 中,对于内置链接列表列表实现,我们执行 LinkedList S = new LinkedList() 和 Queue它是 Queue queueA = new LinkedList();。如果是您的自定义实现,请同时粘贴该代码。

标签: data-structures merge linked-list queue


【解决方案1】:

在这部分代码中

else if(frontA == frontB || frontB == frontA) {
            dequeueA = queueA.dequeue();
            dequeueB = queueB.dequeue();
            // add either dequeueA or B
            S.add(dequeueA); // add the element to the singly linked list
            count++; // increment the counter
}

您需要将计数增加 2 倍,因为即使您在从两个队列中删除相同元素时添加链接列表,因此两个元素也将被删除

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多