概述
双端队列定义
Deque接口
public interface Deque<E> extends Queue<E> {
public void addFirst(E element);
public void addLast(E element);
public E removeFirst();
public E removeLast();
public E getFirst();
public E getLast();
public int size();
public boolean isEmpty();
public void clear();
}
ArrayDeque类
package shixianClass;
import shujujiegou_interface.Deque;
import shujujiegou_interface.Stack;
import java.util.Arrays;
import java.util.Iterator;
//双端队列 除了有自身的特点外,也可以当做队列和栈使用
//没有提供在任意角标处操作元素的功能
public class ArrayDeque<E> implements Deque<E>, Stack<E> {
private E []data;
private int front;//队首指针 front==rear 双端队列为空
private int rear; //队尾指针 (rear+1)%data.length==front 双端队列为满
private int size; //有效元素的个数
private static int DEFAULT_SIZE=10;
public ArrayDeque(){
this(DEFAULT_SIZE);
}
public ArrayDeque(int capacity){
data=(E[])new Object[capacity+1];
front=0;
rear=0;
size=0;
}
//在Deque的队首添加一个元素
@Override
public void addFirst(E element) {
//先判断Deque是否满的
if(isExpansion()){
resize(data.length*2-1);
}
front= (front-1+data.length)%data.length ;
data[front]=element;
size++;
}
private void resize(int newLength) {
E[] newData=(E[])new Object[newLength];
int index=0;
for(int i=front;i!=rear;i=(i+1)%data.length){
newData[index++]=data[i];
}
front=0;
rear=index;
data=newData;
}
private boolean isExpansion() {
return (rear+1)%data.length==front;
}
//在Deque的队尾添加一个元素
@Override
public void addLast(E element) {
//先判断Deque是否满的
if(isExpansion()){
resize(data.length*2-1);
}
data[rear]=element;
rear=(rear+1)%data.length;
size++;
}
//在Deque队首删除一个元素
@Override
public E removeFirst() {
if(isEmpty()){
throw new NullPointerException("Deque is null");
}
E ret =data[front];
front=(front+1)%data.length;
size--;
//在判断是否需要缩容
if(isShrink()){
resize(data.length/2+1);
}
return ret;
}
private boolean isShrink() {
return size==(data.length-1)/4&&data.length-1>DEFAULT_SIZE;
}
//在Deque队尾删除一个元素
@Override
public E removeLast() {
if(isEmpty()){
throw new NullPointerException("Deque is null");
}
rear=(rear-1+data.length)/data.length;
E ret=data[rear];
size--;
//在判断是否需要缩容
if(isShrink()){
resize(data.length/2+1);
}
return ret;
}
//获取队首元素
@Override
public E getFirst() {
return data[front];
}
//获取队尾元素
@Override
public E getLast() {
return data[(rear-1+data.length)%data.length];
}
@Override
public int size() {
return size;
}
//判断是否为空
@Override
public boolean isEmpty() {
return size==0&&front==rear;
}
//栈 入栈->addLast() front栈底 rear栈顶
@Override
public void push(E element) {
addLast(element);
}
//栈 出栈->removeLast() front栈底 rear栈顶
@Override
public E pop() {
return removeLast();
}
//栈 获取栈顶元素 ->getLast() front栈底 rear栈顶
@Override
public E peek() {
return getLast();
}
//队列 入队-> addLast() front队首 rear 队尾
@Override
public void offer(E element) {
addLast(element);
}
//队列 入队-> removeFirst() front队首 rear 队尾
@Override
public E poll() {
return removeLast();
}
//队列 获取队首元素-> getFirst() front队首 rear 队尾
@Override
public E element() {
return getFirst();
}
@Override
public void clear() {
data=(E[])new Object[DEFAULT_SIZE+1];
front=0;
rear=0;
size=0;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder(String.format("ArrayDeque:%d/%d[",size,data.length-1));
if (isEmpty()){
sb.append(']');
}else {
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(',');
}
}
}
return sb.toString();
}
@Override
public Iterator<E> iterator() {
return new ArrDequeIterator();
}
//默认的迭代方向是从front-rear
class ArrDequeIterator 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;
}
}
}
测试类
import shixianClass.ArrayDeque;
public class TestArrayDeque {
public static void main(String[] args) {
ArrayDeque<Integer> deque=new ArrayDeque<Integer>();
System.out.println(deque);
deque.addFirst(1);
deque.addFirst(2);
deque.addFirst(3);
System.out.println(deque);
deque.addLast(4);
deque.addLast(5);
deque.addLast(6);
System.out.println(deque);
System.out.println(deque.getFirst());
System.out.println(deque.getLast());
System.out.println(deque.removeFirst());
System.out.println(deque.removeLast());
System.out.println(deque);
//Deque->stack
for(int i=10;i<=15;i++){
deque.push(i);
}
System.out.println(deque);
System.out.println(deque.pop());
System.out.println(deque);
//Deque->Queue
for(int i=100;i<=105;i++){
deque.offer(i);
}
System.out.println(deque);
}
}
最后
以上就是优秀荷花为你收集整理的动态数组-----双端队列的实现的全部内容,希望文章能够帮你解决动态数组-----双端队列的实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复