publicintgetQueueSize(){ if (front == -1) { return0; } int size = (capacity - front + rear + 1) % capacity; if (size == 0) { return capacity; } else { return size; } }
//入队 publicvoidenQueue(int data){ if (isFull()) { System.err.println("error : the queue is overflow"); } else { rear = (rear + 1) % capacity; array[rear] = data; if (front == -1) { front = rear; } System.out.println("enter the data into the queue successfully : " + data); } }
//出队 publicintdeQueue(){ int data = 0; if (isEmpty()) { System.err.println("error : the queue is empty"); } else { data = array[front]; array[front] = 0; if (front == rear) { front = rear = -1; } else { front = (front + 1) % capacity; } System.out.println("delete the data from the queue successfully : " + data); } return data; }
enter the data into the queue successfully : 1 enter the data into the queue successfully : 2 enter the data into the queue successfully : 3 enter the data into the queue successfully : 4 enter the data into the queue successfully : 5 the size of queue : 5 traverse the queue : [1, 2, 3, 4, 5]
delete the data from the queue successfully : 1 delete the data from the queue successfully : 2 delete the data from the queue successfully : 3 delete the data from the queue successfully : 4 delete the data from the queue successfully : 5 the size of queue : 0 traverse the queue : [0, 0, 0, 0, 0]
publicintgetQueueSize(){ if (front == -1) { return0; } int size = (capacity - front + rear + 1) % capacity; if (size == 0) { return capacity; } else { return size; } }
//扩容 privatevoidresizeQueue(){ int initCapacity = capacity; capacity *= 2; int[] oldArray = array; array = newint[this.capacity]; for (int i = 0; i < oldArray.length; i++) { array[i] = oldArray[i]; } //important if (rear < front) { for (int i = 0; i < front; i++) { array[i + initCapacity] = this.array[i]; array[i] = 0; } rear += initCapacity; }
}
//入队 publicvoidenQueue(int data){ if (isFull()) { resizeQueue(); } rear = (rear + 1) % capacity; array[rear] = data; if (front == -1) { front = rear; } System.out.println("enter a data into the queue successfully : " + data); }
//出队 publicintdeQueue(){ int data = -1; if (isEmpty()) { System.err.println("error : the queue is empty"); } else { data = array[front]; array[front] = 0; //delete the data if (front == rear) { front = rear = -1;
} else { front = (front + 1) % capacity; } System.out.println("delete a data from the queue successfully : " + data); } return data; }
enter a data into the queue successfully : 1 enter a data into the queue successfully : 2 enter a data into the queue successfully : 3 enter a data into the queue successfully : 4 enter a data into the queue successfully : 5 the size of queue : 5 traverse the queue : [1, 2, 3, 4, 5, 0, 0, 0]
delete a data from the queue successfully : 1 delete a data from the queue successfully : 2 delete a data from the queue successfully : 3 delete a data from the queue successfully : 4 delete a data from the queue successfully : 5 the size of queue : 0 traverse the queue : [0, 0, 0, 0, 0, 0, 0, 0]
//遍历 publicvoidtraverse(){ if (isEmpty()) { System.err.println("error : the queue is empty !"); return; } LLNode currentNode = frontNode; System.out.print("the all data of queue : [ "); while (currentNode != null) { System.out.print(currentNode.getData() + " ,"); currentNode = currentNode.getNext(); } System.out.print(" ]\n"); }
//入队 publicvoidenQueue(int data){ LLNode newNode = new LLNode(data); if (rearNode != null) { rearNode.setNext(newNode); } rearNode = newNode; if (frontNode == null) { frontNode = rearNode; } System.out.println("enter the data into the queue successfully : " + data); }
//出队 publicintdeQueue(){ int data = 0; if (isEmpty()) { System.err.println("error : the queue is empty"); } else { data = frontNode.getData(); frontNode = frontNode.getNext(); } System.out.println("delete the data from the queue successfully : " + data); return data; }
//test publicstaticvoidmain(String[] args){ LLQueue queue = LLQueue.createQueue(); //enter queue.enQueue(1); queue.enQueue(2); queue.enQueue(3); queue.enQueue(4); queue.enQueue(5); System.out.println("the size of the queue : " + queue.getQueueSize()); System.out.print("traverse the queue : "); queue.traverse(); //delete queue.deQueue(); queue.deQueue(); queue.deQueue(); queue.deQueue(); queue.deQueue(); System.out.println("the size of the queue : " + queue.getQueueSize()); System.out.print("traverse the queue : "); queue.traverse();
}
}
程序运行结果如下所示 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
enter the data into the queue successfully : 1 enter the data into the queue successfully : 2 enter the data into the queue successfully : 3 enter the data into the queue successfully : 4 enter the data into the queue successfully : 5 the size of the queue : 5 traverse the queue : the all data of queue : [ 1 ,2 ,3 ,4 ,5 , ]
delete the data from the queue successfully : 1 delete the data from the queue successfully : 2 delete the data from the queue successfully : 3 delete the data from the queue successfully : 4 delete the data from the queue successfully : 5 the size of the queue : 0 traverse the queue : error : the queue is empty !