//判断队列是否为空 publicbooleanisEmpty() { return front == rear; }
//添加数据 publicvoidaddQueue(int n) { if (isFull()) { System.out.println("This queue is full and you cannot add data in it."); return; } rear++; arr[rear] = n; }
//出队列 publicintgetQueue() { if (isEmpty()) { thrownewRuntimeException("This queue is empty. You cannot get data from it."); } front++; return arr[front]; }
//显示队列的所有数据 publicvoidshowQueue() { if (isEmpty()) { System.out.println("This queue is empty."); return; } for (inti=0; i < arr.length; i++) { System.out.printf("arr[%d] = %d\n", i, arr[i]); } }
//显示队列头数据 publicintpeed() { if (isEmpty()) { thrownewRuntimeException("This queue is empty."); } return arr[front + 1]; } } }
数组模拟环形队列(Circular Queue)
front: point to the first element. Initiated as 0.
rear: point to the place after the last element. Initiated as 0.
//判断队列是否为空 publicbooleanisEmpty() { return front == rear; }
//添加数据 publicvoidaddQueue(int n) { if (isFull()) { System.out.println("This queue is full and you cannot add data in it."); return; } arr[rear] = n; rear = (rear + 1) % maxSize; }
//出队列 publicintgetQueue() { if (isEmpty()) { thrownewRuntimeException("This queue is empty. You cannot get data from it."); } intvalue= arr[front]; front = (front + 1) % maxSize; return value; }