简体   繁体   English

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

[英]How to split a queue into two queues?

Problem问题

Given a queue, split the queue into two queues:给定一个队列,将队列分成两个队列:

  • one containing odd numbers and the other even numbers.一个包含奇数,另一个包含偶数。
  • The relative order of elements must be maintained in both the queues.元素的相对顺序必须在两个队列中保持。
  • Return an array containing the two queues, the 0th index should contain the queue of odd numbers and the 1st index should contain the queue of even numbers.返回一个包含两个队列的数组,第 0 个索引应包含奇数队列,第 1 个索引应包含偶数队列。

Code代码

Here is what I've tried so far.这是我迄今为止尝试过的。 I am currently getting empty queue.我目前正在排空队列。

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

I am currently doing a queue exercise and am currently stuck on this implementation.我目前正在做一个队列练习,目前坚持这个实现。 I would appreciate any help for this.我将不胜感激。

Edit: I've provided my solution on what I've tried so far.编辑:我已经提供了我迄今为止尝试过的解决方案。

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

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;

    }

I worked on your code and I got your errors 1] in while condition use the condition like (!queue.isEmpty()) 2] you are not creating the new queues .it will be created by new keyword only like, Queue oddQueue=new Queue(queue.getMaxSize() );我处理了你的代码,我得到了你的错误 1] 在 while 条件使用条件像 (!queue.isEmpty()) 2] 你没有创建新的队列。它只会由 new 关键字创建,比如 QueueoddQueue=新队列(queue.getMaxSize());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM