简单队列是一种特殊的线性表,只允许在表的前端(front)进行删除操作,只允许在表的后端(rear)进行插入操作,进行插入操作的端称为队尾,进行删除操作的端称为队头。队列简称为先进先出(FIFO)的线性表。
简单demo:
import java.util.Arrays;
public class SequenceQueue<T> {
private int DEFAULT_SIZE = 10;
private int capacity;
private Object[] elementData;
//弹出端记录数
private int front = 0;
//插入端记录数
private int rear = 0;
public SequenceQueue() {
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public SequenceQueue(T element) {
this();
elementData[0] = element;
rear++;
}
public SequenceQueue(T element, int initSize) {
this.capacity = initSize;
elementData = new Object[capacity];
elementData[0] = element;
rear++;
}
public int length() {
return rear - front;
}
public void add(T element) {
if (rear > capacity - 1) {
throw new IndexOutOfBoundsException("队列已满...");
}
elementData[rear++] = element;
}
public T remove() {
if (empty()) {
throw new IndexOutOfBoundsException("队列为空。。。");
}
T oldValue = (T) elementData[front];
elementData[front++] = null;
return oldValue;
}
public T element() {
if (empty()) {
throw new IndexOutOfBoundsException("队列为空。。。");
}
return (T) elementData[front];
}
public boolean empty() {
return rear == front;
}
public void clear() {
Arrays.fill(elementData, null);
front = 0;
rear = 0;
}
public String toString() {
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[");
for (int i = front; i < rear; i++) {
if (i > front) {
sb.append(", ");
}
sb.append(elementData[i].toString());
}
sb.append("]");
return sb.toString();
}
}
public static void main(String[] args) {
SequenceQueue<String> queue = new SequenceQueue();
queue.add("aaaa");
queue.add("bbbb");
queue.add("cccc");
queue.add("cccc");
queue.add("cccc");
queue.add("cccc");
queue.add("cccc");
queue.add("cccc");
queue.add("cccc");
queue.add("cccc");
System.out.println(queue);
System.out.println("访问队列的front端元素:" + queue.element());
System.out.println("移除队列的front端元素:" + queue.remove());
System.out.println("移除队列的front端元素:" + queue.remove());
System.out.println(queue);
queue.add("cccc");
queue.add("dddd");
System.out.println(queue);
}
}
循环队列:
import java.util.Arrays;
public class LoopQueue<T> {
private int DEFAULT_SIZE = 10;
private int capacity;
private Object[] elementData;
private int front = 0;
private int rear = 0;
public LoopQueue() {
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
public LoopQueue(T element) {
this();
elementData[0] = element;
rear++;
}
public LoopQueue(T element, int initSize) {
this.capacity = initSize;
elementData = new Object[capacity];
elementData[0] = element;
rear++;
}
public int length() {
if (empty()) {
return 0;
}
return rear > front ? rear - front : capacity - (front - rear);
}
public void add(T element) {
if (rear == front && elementData[front] != null) {
throw new IndexOutOfBoundsException("queue already fills...");
}
elementData[rear++] = element;
rear = rear == capacity ? 0 : rear;
}
public T remove() {
if (empty()) {
throw new IndexOutOfBoundsException("empty queue...");
}
T oldValue = (T) elementData[front];
elementData[front++] = null;
front = front == capacity ? 0 : front;
return oldValue;
}
public T element() {
if (empty()) {
throw new IndexOutOfBoundsException("empty queue...");
}
return (T) elementData[front];
}
public boolean empty() {
return rear == front && elementData[rear] == null;
}
public void clear() {
Arrays.fill(elementData, null);
front = 0;
rear = 0;
}
public String toString() {
if (empty()) {
return "[]";
} else {
if (front < rear) {
StringBuilder sb = new StringBuilder("[");
for (int i = front; i < rear; i++) {
if (i > front) {
sb.append(", ");
}
sb.append(elementData[i].toString());
}
sb.append("]");
return sb.toString();
} else {
StringBuilder sb = new StringBuilder("[");
for (int i = front; i < capacity; i++) {
if (i > front) {
sb.append(", ");
}
sb.append(elementData[i].toString());
}
if (rear > 0) {
sb.append(", ");
}
for (int i = 0; i < rear; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(elementData[i].toString());
}
sb.append("]");
return sb.toString();
}
}
}
public static void main(String[] args) {
LoopQueue<String> queue = new LoopQueue<>("aaaa", 3);
queue.add("bbbb");
queue.add("cccc");
System.out.println(queue);
queue.remove();
System.out.println(queue);
queue.add("dddd");
System.out.println(queue);
System.out.println(queue.length());
queue.remove();
queue.add("eeee");
System.out.println(queue);
}
}
链式存储实现:
/**
* Created by Administrator on 2018/3/13.
*/
public class LinkQueue<T> {
private class Node {
private T data;
private Node next;
public Node() {
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
private Node front;
private Node rear;
private int size;
public LinkQueue() {
front = null;
rear = null;
}
public LinkQueue(T element) {
front = new Node(element, null);
rear = front;
size++;
}
public int length() {
return size;
}
public void add(T element) {
if (front == null) {
front = new Node(element, null);
rear = front;
} else {
Node newNode = new Node(element, null);
rear.next = newNode;
rear = newNode;
}
size++;
}
public T remove() {
Node oldFront = front;
front = front.next;
oldFront.next = null;
size--;
return oldFront.data;
}
public T element() {
return rear.data;
}
public boolean empty() {
return size == 0;
}
public void clear() {
front = null;
rear = null;
size = 0;
}
public String toString() {
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[");
Node current = front;
for (int i = 0; i < size && current != null; i++, current = current.next) {
if (i > 0) {
sb.append(", ");
}
sb.append(current.data.toString());
}
sb.append("]");
return sb.toString();
}
}
public static void main(String[] args) {
LinkQueue<String> queue = new LinkQueue<>("aaaa");
queue.add("bbbb");
queue.add("cccc");
System.out.println(queue);
queue.remove();
System.out.println(queue);
queue.add("dddd");
System.out.println(queue);
queue.remove();
queue.add("eeee");
System.out.println(queue);
}
}