java中队列实现

package queue;

public class Queue {
	public int maxSize;
	private int[] array;
	private int front;
	private int rear;
	private int n;

	public Queue(int maxSize) {
		this.maxSize = maxSize;
		array = new int[maxSize];
		front = 0;
		rear = -1;
		n = 0;
	}

	public void insert(int i) {
		if (rear == maxSize - 1)
			rear = -1;
		array[++rear] = i;
		n++;
	}

	public int remove() {
		if (front == maxSize)
			front = 0;
		n--;
		return (array[front++]);
	}

	public int peek() {
		return (array[front]);
	}

	public boolean isEmpty() {
		return (n == 0);
	}

	public boolean isFull() {
		return (n == maxSize);
	}

	public int size() {
		return n;
	}
}

package queue;

public class QueueTest {
	public static void main(String[] args) {
		Queue queue = new Queue(10);
		for (int i = 0; i < 10; i++) {
			queue.insert(i);
		}
		while (!queue.isEmpty()) {
			System.out.print(queue.remove() + "\t");
		}
	}
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券