顺序队列java代码_数据结构——顺序队列(Java实现)

此处包括一个泛型顺序队列抽象类,一个Integer型实现类,一个测试类。

实现了队列的以下功能:

1.队尾入队

2.队首出队

3.取队首

4.判空

5.求长度

6.删除队首元素

7.扩充长度

泛型顺序队列抽象类

Queue.java:

package queue;

import java.lang.reflect.Array;

import java.util.Arrays;

public abstract class Queue {

protected int front;

protected int rear;

protected T[] arr;

private int maxSize;

public Queue(Class componentType) {

this.front = 0;

this.rear = 0;

this.maxSize = 1024;

arr = (T[])Array.newInstance(componentType, this.maxSize);

}

public Queue(Class componentType, int maxSize) {

this.front = 0;

this.rear = 0;

this.maxSize = maxSize;

arr = (T[])Array.newInstance(componentType, this.maxSize);

}

public int getSize() {

return this.rear - this.front;

}

public void expandMaxSize(int maxSize) {

Arrays.copyOf(arr, this.maxSize + maxSize);

this.maxSize += maxSize;

}

public void offer(T data) {

if(this.getSize() > this.maxSize || this.rear > this.maxSize) {

throw new IllegalArgumentException("new element will be out of limits.");

}

arr[this.rear++] = data;

}

public void remove() {

if(this.front == this.rear) {

throw new IllegalArgumentException("this queue is already empty");

}

this.front++;

}

public T peek() {

if(this.front == this.rear) {

throw new IllegalArgumentException("this queue is already empty");

}

return arr[this.front];

}

public T poll() {

if(this.front == this.rear) {

throw new IllegalArgumentException("this queue is already empty");

}

return arr[this.front++];

}

}

Integer型实现类

IntegerQueue.java:

package queue;

public class IntegerQueue extends Queue {

public IntegerQueue() {

super(Integer.class);

}

public IntegerQueue(int maxSize) {

super(Integer.class, maxSize);

}

public void print(){

System.out.print("F-");

for(int i = this.rear - 1; i >= this.front; i--) {

System.out.print(arr[i] + "-");

}

System.out.println("R");

}

}

测试类

IntegerQueueDemo:

package queue;

public class IntegerQueueDemo {

public static void main(String[] args) {

IntegerQueue test = new IntegerQueue();

test.print();

System.out.println("size is: " + test.getSize());

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

test.offer(8);

test.offer(14);

test.offer(5);

test.offer(3);

test.print();

System.out.println("size is: " + test.getSize());

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

System.out.println("the element in front of queue: " + test.peek());

test.remove();

test.print();

System.out.println("size is: " + test.getSize());

System.out.println("the element in front of queue: " + test.peek());

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

System.out.println("the fetched element: " + test.poll());

test.print();

System.out.println("size is: " + test.getSize());

System.out.println("the element in front of queue: " + test.peek());

}

}

输出结果:

F-R

size is: 0

================

F-3-5-14-8-R

size is: 4

================

the element in front of queue: 8

F-3-5-14-R

size is: 3

the element in front of queue: 14

================

the fetched element: 14

F-3-5-R

size is: 2

the element in front of queue: 5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值