顺序存储
package linearlist;
/**
* 队列的数组实现
* @author mac
* */
public class ArrayQueue {
private static final int DEFAULT_SIZE = 10;
private Object[] items; //数组容器
private int front; //头元素:指向第一个元素的前一个位置
private int rear; //尾元素:指向最后一个元素
private int capacity;
private int size;
/**
* 初始化
* */
public ArrayQueue(){
items = new Object[DEFAULT_SIZE];
this.capacity = DEFAULT_SIZE;
front = rear = 0;
size = 0;
}
public ArrayQueue(int capacity) {
items = new Object[capacity];
this.capacity = capacity;
front = rear = 0;
size = 0;
}
/**
* 判断队列是否尾空
*
* 两种方法:
* 1)判断size
* 2)判断指针位置
* */
public boolean isEmpty() {
return size == 0;
}
/**
* 判断队列是否已满
*
* 两种方法:
* 1)判断size
* 2)判断指针
* */
public boolean isFull(){
return size == capacity;
}
/**
* 入队
* */
public boolean enqueue(Object elem) {
if(isFull()) {
return false;
}
items[rear] = elem;
//调整rear指针位置,向后移动或从头开始
rear = (rear + 1) % capacity;
size ++;
return true;
}
/**
* 出队
* */
public Object dequeue(){
if(isEmpty()) {
return null;
}
Object obj = items[front]; //返回队列的头节点
size --;
//调整front指针
front = (front + 1) % capacity;
return obj;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
if(isEmpty()) {
sb.append("]");
}else {
int i = front;
int loop = 0;
while (loop < size) {
sb.append(items[i]).append(",");
i = (i + 1) % capacity;
loop ++;
}
sb.append("]");
}
return sb.toString();
}
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(5);
arrayQueue.enqueue("a");
arrayQueue.enqueue("b");
arrayQueue.enqueue("d");
System.out.println(arrayQueue);
arrayQueue.enqueue("e");
arrayQueue.enqueue("f");
System.out.println(arrayQueue);
System.out.println(arrayQueue.dequeue());
arrayQueue.enqueue("g");
System.out.println(arrayQueue);
}
}
实现顺序存储结果的关键在于利于取模算法实现循环队列。
链式存储
package linearlist;
/**
* 链表实现队列
* @author mac
* */
public class LinkedQueue {
private Node front;
private Node rear;
private int size;
/**
* 初始化
* */
public LinkedQueue(){
this.front = this.rear = null;
this.size = 0;
}
/**
* 判断队列是否为空
* */
public boolean isEmpty(){
return this.front == null;
}
/**
* 入队
* */
public boolean enqueue(Object elem) {
//构造一个新的节点
Node newNode = new Node(elem, null);
if(isEmpty()) {
this.front = newNode;
this.rear = newNode;
}else{
//添加到节点尾部
this.rear.next = newNode;
this.rear = newNode;
}
return true;
}
/**
* 出队
* */
public Object dequeue(){
if(isEmpty()) {
return null;
}else{
Node node = this.front;
//判断是否只有一个元素i
if(this.front == this.rear) {
//将头节点和尾节点只为空
this.front = this.rear = null;
}else{
//调整头节点位置
this.front = node.next;
node.next = null;
}
return node.data;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
Node node = this.front;
while(node != null) {
sb.append(node.data).append(" ");
node = node.next;
}
sb.append("]");
return sb.toString();
}
//定义链表节点
private static class Node{
Object data;
Node next;
public Node(){}
public Node(Object data, Node next){
this.data = data;
this.next = next;
}
}
public static void main(String[] args) {
LinkedQueue queue = new LinkedQueue();
queue.enqueue("a");
queue.enqueue("b");
queue.enqueue("c");
System.out.println(queue);
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
System.out.println(queue);
queue.enqueue("d");
System.out.println(queue);
}
}
链式存储中需要注意的是,出队时,需要判断队列中是否只有一个元素,如果是,则需要同时调整头节点和为节点为null,入对时,需要判断队列是否为空,如果为空,则需要同时调整头节点和尾节点指向新加入对节点。