如何将一个队列分成两个队列?

j7dteeu8  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(259)

问题
给定一个队列,将队列分为两个队列:
一个包含奇数,另一个包含偶数。
元素的相对顺序必须在两个队列中保持。
返回一个包含两个队列的数组,第0个索引应包含奇数队列,第1个索引应包含偶数队列。
代码
这是我到目前为止试过的。我当前正在获取空队列。

class Queue {

private int front;
private int rear;
private int maxSize;
private int arr[];

Queue(int maxSize) {
    this.front = 0;
    this.rear = -1;
    this.maxSize = maxSize;
    this.arr = new int[this.maxSize];
}

public boolean isFull() {
    if (rear == maxSize - 1) {
        return true;
    }
    return false;
}

public boolean enqueue(int data) {
    if (isFull()) {
        return false;
    } else {
        arr[++rear] = data;
        return true;
    }
}

public void display() {
    if(isEmpty())
        System.out.println("Queue is empty!");
    else {
        for (int index = front; index <= rear; index++) {
            System.out.println(arr[index]);
        }
    }
}

public boolean isEmpty() {
    if (front > rear)
        return true;
    return false;
}

public int dequeue() {
    if (isEmpty()) {
        return Integer.MIN_VALUE;
    } else {
        int data = arr[this.front];
        arr[front++] = Integer.MIN_VALUE;
        return data;
    }
}

public int getMaxSize() {
    return maxSize;
}
}

class Main {

public static void main(String[] args) {

    Queue queue = new Queue(7);
    queue.enqueue(2);
    queue.enqueue(7);
    queue.enqueue(9);
    queue.enqueue(4);
    queue.enqueue(6);
    queue.enqueue(5);
    queue.enqueue(10);

    Queue[] queueArray = splitQueue(queue);

    System.out.println("Elements in the queue of odd numbers");
    queueArray[0].display();

    System.out.println("\nElements in the queue of even numbers");
    queueArray[1].display();

}

// function to split the queue into two queues; 
    // 1. one even. one odd.
    // 2. relative elements must be maintained in both queues.
    // 3. 0th index returns queue of odd numbers
    // 4. 1st index returns queue of even numbers
public static Queue[] splitQueue(Queue queue) {

    // create queue array for 0th index for odd and 1st index for even
    Queue[] numberArr = new Queue[2];

    // create two queues - odd and even queue
    Queue oddQueue = queue;
    Queue evenQueue = queue;

    // seperate odd and even
    while (!oddQueue.isEmpty()) {
        // start dequeue
        int val = queue.dequeue();

        // check even odd
        if (val % 2 == 0) {
            System.out.println(val + " - Even");
            evenQueue.enqueue(val);
        }
        else {
            System.out.println(val + " - Odd");
            oddQueue.enqueue(val);
        }
    }
    evenQueue.display();
    oddQueue.display();

    // assign odd numbers queue to 0th index of array
    numberArr[0] = oddQueue;
    // assign even numbers queue to 1th index of array
    numberArr[1] = evenQueue;

    if ((!oddQueue.isEmpty() && !evenQueue.isEmpty())) {
        return numberArr;
    }
    return null;
}
}

我目前正在做一个队列练习,目前还停留在这个实现上。如果能帮上忙,我将不胜感激。
编辑:到目前为止,我已经提供了我的解决方案。

pengsaosao

pengsaosao1#

在while循环中检查形式参数“queue”是否为空=>while(!queue.isempty())

omvjsjqw

omvjsjqw2#

public static Queue[] splitQueue(Queue queue) {
       //Implement your code here and change the return value accordingly
    Queue[] numberArr = new Queue[2];
      //create two queues - odd and even queue
    Queue oddQueue=new Queue(queue.getMaxSize() );
    Queue evenQueue=new Queue(queue.getMaxSize());
    // seperate odd and even
   while(!(queue.isEmpty()))
   {
       int val=queue.dequeue();
       //System.out.println(val);
       if(val%2==0)
       {
           evenQueue.enqueue(val);
       }else
       {
           oddQueue.enqueue(val);
       }
   }
    // assign odd numbers queue to 0th index of array
    numberArr[0] = oddQueue;
    // assign even numbers queue to 1th index of array
    numberArr[1] = evenQueue;
    return numberArr;

    }

我编写了你的代码,在条件中得到了你的错误1]使用类似(!queue.isempty())2]您没有创建新队列。它将仅由new关键字创建,如queue oddqueue=new queue(queue.getmaxsize());

相关问题