gpt4 book ai didi

java - 如何在java中将队列的大小加倍

转载 作者:行者123 更新时间:2023-12-02 13:24:01 25 4
gpt4 key购买 nike

我一直在尝试将队列的大小加倍,并且尝试了几种不同的方法,这个方法给了我最接近我想要得到的结果,但是,它复制了队列中已有的所有值进入新创建的空间,而不是将其留空以供我添加更多对象。我有两个类,唯一需要更改的部分是入队方法。

enter image description here

最后一行应打印 50 40 30 20 60 70。

public class Queue{
private int QUEUE_SIZE = 5;
private Object[] items;
private int front, back, count;
public Queue() {
items = new Object[QUEUE_SIZE];
front = 0;
back = QUEUE_SIZE -1;
count =0;
}
public boolean isEmpty(){
return count ==0;
}
public boolean isFull(){
return count == QUEUE_SIZE;
}
public void enqueue(Object newItem){
if (!isFull()){
back = (back+1) % QUEUE_SIZE;
items[back] = newItem;
count++;
return;
} else
System.out.println("Queue is full. Doubling the size.");
count = (QUEUE_SIZE*2);
System.out.println("New max size is: " + QUEUE_SIZE);
}
public Object dequeue(){
if (!isEmpty()){
Object queueFront = items[front];
front = (front+1) % QUEUE_SIZE;
count--;
return queueFront;
}else
System.out.println("Trying to dequeue from empty queue");
return null;
}
public void dequeueAll(){
items = new Object[QUEUE_SIZE];
front = 0;
back = QUEUE_SIZE -1;
count =0;
}
public Object peek(){
if (!isEmpty()) {
return items[front];
}
else
System.out.println("Trying to peek with empty queue");
return null;
}

public int size(){
return count;
}

}






import java.util.Stack;

public class Runner {

public static void main(String[] args) {
Queue q = new Queue();
createQueue(q);
System.out.println("My queue is as follows: ");
printQueue(q);
System.out.println("I am going to dequque one element.");
q.dequeue();
System.out.println("My queue is as follows: ");
printQueue(q);
System.out.println("I am going to reverse my queue: ");
reverseQueue(q);
System.out.println("My queue is as follows: ");
printQueue(q);
System.out.println("I am going to enqueue 60.");
q.enqueue(60);
System.out.println("My queue is as follows: ");
printQueue(q);
System.out.println("I am going to enqueue 70.");
q.enqueue(70);
System.out.println("My queue is as follows: ");
printQueue(q);
}

public static Queue createQueue(Queue q){
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
return q;
}

public static void printQueue(Queue q){
int s = q.size();

for(int i=0; i<s; i++){
int temp = (int)q.dequeue();
q.enqueue(temp);
System.out.print(temp+ " ");
}
System.out.println();
}

public static void reverseQueue(Queue q){
Stack s = new Stack();
while(!q.isEmpty()){
s.push(q.dequeue());
}while(!s.isEmpty()){
q.enqueue(s.pop());
}
}

}

最佳答案

else阻止enqueue(Object newItem)不插入新对象,所以它应该是这样的:

public void enqueue(Object newItem) {
if (!isFull()) {
back = (back + 1) % QUEUE_SIZE;
items[back] = newItem;
count++;
return;
}
else {
System.out.println("Queue is full. Doubling the size.");
QUEUE_SIZE = (QUEUE_SIZE * 2); // double queue size not count
System.out.println("New max size is: " + QUEUE_SIZE);
Object[] temp = new Object[QUEUE_SIZE]; // temp array
System.arraycopy(items, front, temp, front, items.length - front); // copy the elements from front index to items.length-front
if (front != 0) {
System.arraycopy(items, 0, temp, items.length, front); // copy the elements in the range items[0] to items[back] into the new array
}
items = temp; // set items to temp array
back = front + count;
items[back] = newItem; // set new item
count++; // increment count
}

}

关于java - 如何在java中将队列的大小加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43445807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com