问题
我正在尝试实现一个循环队列,我想在其中显示我的队列,但唯一显示的是“这里是我的队列”,它只显示“空”。而不是打印出添加到队列中的客户端。我无法弄清楚为什么我的队列没有显示。我在此处插入图像以显示我想说的内容,请在此处输入图像描述。
我的队列将包括从 waitingRoom 移动到 CustomerQueue 的客户。最大客户数为 33。
这是我的代码:
- package hotelbooking;
- public class HotelBooking {
- static int ROOM_CAPACITY = 33;
- private static Customer[] waitingRoom = new Customer[ROOM_CAPACITY];
- private static CustomerQueue hotelQueue = new CustomerQueue();
复制代码
这就是用户显示队列的方式。
- private static void ViewhotelQueue(Customer[] waitingRoom) {
- System.out.println("Here is the queue: ");
- hotelQueue.display();
- }
复制代码- package hotelbooking;
- public class CustomerQueue {
- private Customer[] queArray = new Customer[HotelBooking.ROOM_CAPACITY];
- private int front = 0;
- private int end = 0;
- private int maxLength = 0; //maximum length that was reached by the queue
- private int currentSize = 0; //current circular Queue Size
- public void add(Customer next) {
- //if the queue is not full - check for the circular queue
- if (!isFull()){
- //add next customer
- end = (end + 1)% queArray.length;
- queArray[end] = next;
- currentSize++;
- //check the maxLength of the queue
- }
- }
- public Customer remove() {
- //if the queArray is not empty
- if (!isEmpty()){
- //remove customer
- Customer removedCustomer = queArray[front];
- //inform that not customer (return null)
- queArray[front] = null;
- front = (front + 1) % queArray.length;
- currentSize--;
- return removedCustomer;
- }
- return null;
- }
- public void display() {
- //list elements from front to end in the queArray
- for (int i = front; i < currentSize; i++) {
- System.out.println(queArray[(front+i)%queArray.length] + "");
- queArray[i].display();
- }
- }
复制代码
有人可以帮助我,因为我一直在努力。
回答
太好了,您正在学习 java 和数据结构。 .
这可能会奏效。请将该元素添加到队列中并尝试显示
和
客户端队列类的一些更改
- private int end = -1;
- public void display() {
- //list elements from front to end in the queArray
- for (int i = front; i < currentSize; i++) {
- System.out.println(queArray[(front+i)%queArray.length] + "");
- }
- }
复制代码
|