循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环
优化它为了让时间复杂度降到最低,本质上就是让指向队列头尾的指针随元素变化而变化,
将一个空间预留出来不存任何元素,尾指针始终指向这个null空间。
import p1.接口.Queue;
import java.util.Iterator;
//循环队列
public class ArrayLoopQueue<E> implements Queue<E> {
//存储数据的容器
private E[] data;
//队首指针(角标)
private int front;
//队尾指针
private int rear;
//元素个数
private int size;
//默默人容量
private static int DEFAULT_CAPACITY = 10;
public ArrayLoopQueue(){
data = (E[]) new Object[DEFAULT_CAPACITY + 1];//给rear预留一个空位
front = 0;
rear = 0;
size = 0;
}
@Override
public void offer(E element) {//进队
if((rear + 1 ) % data.length == front){//判满 扩容
resize(data.length * 2 - 1);
}
data[rear] = element;
rear = (rear + 1 ) % data.length;//循环
size++;
}
@Override
public E poll() {//出队
//判空
if (isEmpty()){
throw new IllegalArgumentException("queue is null");
}
E ret = data[front];
front = (front + 1) % data.length;
size--;
//缩容
if(size <= (data.length - 1) / 4 && data.length - 1 >DEFAULT_CAPACITY){
resize(data.length / 2 + 1);
}
return null;
}
private void resize(int newLen) {//改变容量
E[] newdata = (E[]) new Object[newLen];//建一个新栈,头对头转换
int index = 0;
for (int i = front ; i != rear ; i = (i+ 1) % data.length) {
newdata[index++] = data[i];
}
data = newdata;
front = 0;
rear = index;
}
@Override
public E peek() {//查看front
if (isEmpty()){
throw new IllegalArgumentException("queue is null");
}
return data[front];
}
@Override
public boolean isEmpty() {
return front == rear;
}
@Override
public void clear() {//相当于重来一遍
data = (E[]) new Object[DEFAULT_CAPACITY + 1];
front = 0;
rear = 0;
size = 0;
}
@Override
public int size() {
return size;
}
@Override
public String toString() {//输出字符串拼接
StringBuilder sb = new StringBuilder();
sb.append('[');
if (isEmpty()){//空直接加右括号
sb.append(']');
}
for (int i = front ; i != rear ; i = (i + 1) % data.length) {
sb.append(data[i]);
if ( (i + 1) % data.length == rear){
sb.append(']');
}else {
sb.append(',');
sb.append(' ');
}
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (o == null){
return false;
}
if (this == o) {
return true;
}
if (o instanceof ArrayLoopQueue){//拿出来挨个对比
ArrayLoopQueue<E> other = (ArrayLoopQueue<E>) o;
if (size != other.size) {
return false;
}
int i = front;
int j = other.front;
while (i != rear){
if (!data[i].equals(other.data[j])){
return false;
}
i = (i+1) % data.length;
j = (j+1) % other.data.length;
}
}
return false;
}
@Override
public Iterator<E> iterator() {//构造器写个新方法
return new ArrayLoopQueueIterator();
}
class ArrayLoopQueueIterator implements Iterator<E>{//迭代器
private int cur = front;
@Override
public boolean hasNext() {
return cur != rear;
}
@Override
public E next() {
E ret = data[cur];
cur = (cur + 1 ) % data.length;
return ret;
}
}
}
双端队列
思想与循环队列大致一样,差在需要在队首队尾都能添加删除的功能。
重新写一个接口
public interface Dequeue<E> extends Queue<E> {
public void addFirst(E element);//表头添加
public void addLast(E element);//表尾添加
public E removeFirst();//表头删除
public E reomveLast();//表尾删除
public E getFirst();//表头获取
public E getLast();//表尾获取
}
双端队列多在对角标的定位,对front、rear的位置有特殊要求
具体代码如下
import p1.接口.Dequeue;
import p1.接口.Stack;
import java.util.Iterator;
public class ArrayDeque<E> implements Dequeue<E> , Stack<E> {
private E[] data;
private int front;
private int rear;
private int size;
private static int DEFAULT_CAPACITY = 10;
public ArrayDeque(){//重置数据
data = (E[]) new Object[DEFAULT_CAPACITY + 1];
front = 0;
rear = 0;
size = 0;
}
@Override
public void addFirst(E element) {
if((rear + 1 ) % data.length == front){//判满
resize(data.length * 2 - 1);
}
front = (front - 1 + data.length) % data.length;//当前角标减一加数组长度对数组取余
data[front] = element;//给值
size++;
}
@Override
public void addLast(E element) {
if((rear + 1 ) % data.length == front){
resize(data.length * 2 - 1);
}
data[rear] = element;
rear = (rear + 1 ) % data.length;//向后推一
size++;
}
private void resize(int newLen) {//改变容量
E[] newdata = (E[]) new Object[newLen];//建一个新栈,头对头转换
int index = 0;
for (int i = front ; i != rear ; i = (i+ 1) % data.length) {
newdata[index++] = data[i];
}
data = newdata;
front = 0;
rear = index;
}
@Override
public E removeFirst() {//减前
if(isEmpty()){//判空
throw new IllegalArgumentException("queue 是空");
}
E ret = data[front];
front = (front + 1 ) % data.length;
size--;
if (size <= (data.length - 1) / 4 && data.length - 1 > DEFAULT_CAPACITY){//判缩容
resize(data.length / 2 + 1);
}
return null;
}
@Override
public E reomveLast() {//减尾
if(isEmpty()){//判空
throw new IllegalArgumentException("queue 是空");
}
rear = (rear - 1 + data.length) % data.length;//向前挪一格
E ret = data[rear];
size--;
if (size <= (data.length - 1) / 4 && data.length - 1 > DEFAULT_CAPACITY){//判缩容
resize(data.length / 2 + 1);
}
return ret;
}
@Override
public E getFirst() {//获取表头
if(isEmpty()){//判空
throw new IllegalArgumentException("queue 是空");
}
return data[front];
}
@Override
public E getLast() {//获取表尾
if(isEmpty()){//判空
throw new IllegalArgumentException("queue 是空");
}
return data[(rear - 1 + data.length) % data.length];
}
@Override
public void offer(E element) {//添加进表尾
addLast(element);
}
@Override
public E poll() {//从表头出
return removeFirst();
}
@Override
public E element() {//队列的表头
return getFirst();
}
@Override
public E peek() {//栈顶
return getLast();
}
@Override
public boolean isEmpty() {
return size == 0 && front == rear;
}
@Override
public void push(E element) {//栈的表尾添加
addLast(element);
}
@Override
public E pop() {//表尾删除
return reomveLast();
}
@Override
public void clear() {
E[] data = (E[]) new Object[DEFAULT_CAPACITY];
front = 0;
rear = 0;
size = 0;
}
@Override
public int size() {
return size;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (isEmpty()){//空直接加右括号
sb.append(']');
}
for (int i = front ; i != rear ; i = (i + 1) % data.length) {
sb.append(data[i]);
if ( (i + 1) % data.length == rear){
sb.append(']');
}else {
sb.append(',');
sb.append(' ');
}
}
return sb.toString();
}
@Override
public Iterator<E> iterator() {
return new ArrayDeque.ArrayDequeIterator();
}
class ArrayDequeIterator implements Iterator<E>{//迭代器
private int cur = front;
@Override
public boolean hasNext() {
return cur != rear;
}
@Override
public E next() {
E ret = data[cur];
cur = (cur + 1 ) % data.length;
return ret;
}
}
@Override
public boolean equals(Object o) {
if (o == null){
return false;
}
if (this == o) {
return true;
}
if (o instanceof ArrayDeque){//拿出来挨个对比
ArrayDeque<E> other = (ArrayDeque<E>) o;
if (size != other.size) {
return false;
}
int i = front;
int j = other.front;
while (i != rear){
if (!data[i].equals(other.data[j])){
return false;
}
i = (i+1) % data.length;
j = (j+1) % other.data.length;
}
}
return false;
}
}