5.1、顺序队列(java实现)

java

1、实现代码

public class SeqQueue {

private final int MaxSize = 8;

private int rear; //队尾指针

private int front; //队头指针

private int size; //计数器

private Object seqQueueArray[];

/**

* 初始化

*/

public SeqQueue() {

this.size = 0;

this.front = 0;

this.rear = 0;

seqQueueArray = new Object[MaxSize];

}

public boolean isEmpty(SeqQueue seqQueue) {

if (seqQueue.size == 0){

return true;

}

return false;

}

public boolean isFull(SeqQueue seqQueue) {

if (seqQueue.size > 0 && seqQueue.rear == seqQueue.front){

return true;

}

return false;

}

public void queueAppend(SeqQueue seqQueue,Object element) {

if (isFull(seqQueue)){

System.out.println("已满,无法插入");

return;

}

System.out.println(element+"元素入队列");

seqQueue.seqQueueArray[seqQueue.rear] = element;

seqQueue.rear = (seqQueue.rear +1 )%MaxSize;

seqQueue.size++;

}

public void queueDelete(SeqQueue seqQueue) {

if (isEmpty(seqQueue)){

System.out.println("已空,无法出队列");

return;

}

System.out.print(seqQueue.seqQueueArray[seqQueue.front]+" ");

seqQueue.front = (seqQueue.front +1)%MaxSize;

seqQueue.size--;

}

public void getFront(SeqQueue seqQueue) {

if (isEmpty(seqQueue)){

System.out.println("已空,无法获取队列头");

return;

}

System.out.println("队头元素: "+ seqQueue.seqQueueArray[seqQueue.rear]);

}

public static void main(String[] args) {

SeqQueue seqQueue = new SeqQueue();

seqQueue.getFront(seqQueue);

seqQueue.queueDelete(seqQueue);

for (int i = 0; i < 9; i++) {

seqQueue.queueAppend(seqQueue,i);

}

System.out.println("===========");

seqQueue.queueDelete(seqQueue);

System.out.println("===========");

int number = seqQueue.size;

for (int i = 0; i < number; i++) {

seqQueue.queueDelete(seqQueue);

}

System.out.println();

seqQueue.queueDelete(seqQueue);

}

}

2、实现结果

已空,无法获取队列头

已空,无法出队列

0元素入队列

1元素入队列

2元素入队列

3元素入队列

4元素入队列

5元素入队列

6元素入队列

7元素入队列

已满,无法插入

===========

0 ===========

1 2 3 4 5 6 7

已空,无法出队列

以上是 5.1、顺序队列(java实现) 的全部内容, 来源链接: utcz.com/z/391883.html

回到顶部