基于数组的循环队列
/**
* 基于数组的循环队列实现
*
* @param <E> 泛型
* @author ZhuZongxing
*/
public class LoopQueue<E> implements Queue<E> {
private E[] data;
private int front;
private int tail;
private int size;
public LoopQueue(int capacity) {
data = (E[]) new Object[capacity + 1];
front = 0;
tail = 0;
size = 0;
}
public LoopQueue() {
this(10);
}
public int getCapacity() {
return data.length - 1;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return tail == front;
}
@Override
public void enQueue(E e) {
if ((tail + 1) % data.length == front) {
resize(2 * getCapacity());
}
data[tail] = e;
tail = (tail + 1) % data.length;
size++;
}
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < getSize(); i++) {
newData[i] = data[(i + front) % data.length];
}
data = newData;
front = 0;
tail = getSize();
}
@Override
public E deQueue() {
if (isEmpty()) {
throw new IllegalArgumentException("队列中没有数据了!");
}
E ret = data[front];
front = (front + 1) % data.length;
size--;
if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
resize(getCapacity() / 2);
}
return ret;
}
@Override
public E getFront() {
return data[front];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("LoopQueue: Capacity: %d , Size: %d \nfront [", getCapacity(), getSize()));
for (int i = front; i != tail; i = (i + 1) % data.length) {
sb.append(data[i]);
if (tail != (i + 1) % data.length) {
sb.append(", ");
}
}
sb.append("] tail");
return sb.toString();
}
}
基于链表的队列实现
package queue;
/**
* 基于链表的队列实现
*
* @param <E> 泛型
* @author ZhuZongxing
*/
public class LinkedListQueue<E> implements Queue<E> {
private class Node {
public E e;
public Node next;
public Node(E e) {
this(e, null);
}
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
@Override
public String toString() {
return e.toString();
}
}
private Node head, tail;
private int size;
public LinkedListQueue() {
head = null;
tail = null;
size = 0;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public void enQueue(E e) {
if (tail == null) {
tail = new Node(e);
head = tail;
} else {
tail.next = new Node(e);
tail = tail.next;
}
size++;
}
@Override
public E deQueue() {
if (isEmpty()) {
throw new IllegalArgumentException("队列中已经没有元素了!!!");
}
Node retNode = head;
head = head.next;
retNode.next = null;
if (head == null) {
tail = null;
}
size--;
return retNode.e;
}
@Override
public E getFront() {
if (isEmpty()) {
throw new IllegalArgumentException("队列中已经没有元素了!!!");
}
return head.e;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("LinkedListQueue: Front [");
Node curNode = this.head;
while (curNode != null) {
sb.append(curNode.e + "-->");
curNode = curNode.next;
}
sb.append("Null] Tail");
return sb.toString();
}
public static void main(String[] args) {
LinkedListQueue<Integer> linkedListQueue = new LinkedListQueue<>();
for (int i = 0; i < 10; i++) {
linkedListQueue.enQueue(i);
}
System.out.println(linkedListQueue);
linkedListQueue.deQueue();
System.out.println(linkedListQueue);
System.out.println(linkedListQueue.getFront());
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。