队列:
package cn.queue;
import java.util.LinkedList;
import java.util.List;
public class Queue<E> {
private List<E> list;
public Queue() {
this.list =new LinkedList<E>();
}
int size(){
return list.size();
}
boolean isEmpty(){
return size()==0;
}
void clear(){
}
void enQueue(E element){
list.add(element);
}
E deQueue(){
return list.remove(0);
}
E front(){
return list.get(0);
}
public static void main(String[] args) {
Queue<Integer> queue=new Queue<>();
queue.enQueue(10);
queue.enQueue(20);
queue.enQueue(30);
queue.enQueue(40);
while (!queue.isEmpty()){
System.out.println(queue.deQueue());
}
}
}
双端队列
package cn.queue;
import java.util.LinkedList;
import java.util.List;
public class Deque<E> {
private List<E> list;
public Deque() {
this.list = new LinkedList<E>();
}
int size(){
return list.size();
}
boolean isEmpty(){
return this.size()==0;
}
void clear(){
}
void enQueueRear(E element){
list.add(element);
}
void enQueueFront(E element){
list.add(0,element);
}
E deQueueRear(){
return list.remove(list.size()-1);
}
E deQueueFront(){
return list.remove(0);
}
E front(){
return list.get(0);
}
E rear(){
return list.get(list.size()-1);
}
public static void main(String[] args) {
Deque<Integer> deque=new Deque<>();
deque.enQueueFront(10);
deque.enQueueFront(20);
deque.enQueueRear(30);
deque.enQueueRear(40);
while (!deque.isEmpty()){
System.out.println(deque.deQueueRear());
}
}
}
循环队列
package cn.queue;
public class ClicleQueue<E> {
private int size;
private E[] elements;
private int front;
private static final int DEFAULE_CAPACITY = 10;
ClicleQueue() {
this.elements = (E[]) new Object[DEFAULE_CAPACITY];
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return this.size;
}
public E front() {
return this.elements[this.front];
}
public void enQueue(E element) {
eusureCapacity(this.size + 1);
this.elements[index(this.size)] = element;
this.size++;
}
public int index(int index) {
return (this.front + index) % this.elements.length;
}
public E deQueue() {
E old = this.elements[this.front];
this.elements[this.front] = null;
this.front = index(1);
return old;
}
private void eusureCapacity(int capacity) {
int oldCapacity = elements.length;
if (capacity >= oldCapacity) {
return;
}
int newCapacity=oldCapacity+(oldCapacity>>1);
E[] newElementData= (E[]) new Object[newCapacity];
for(int i=0;i<size;i++){
newElementData[i]=elements[index(i)];
}
elements=newElementData;
this.front=0;
}
}
双端循环队列
package cn.queue;
public class ClicleDeque<E> {
private int size;
private E[] elements;
private int front;
private static final int DEFAULE_CAPACITY=10;
public ClicleDeque() {
this.elements= (E[]) new Object[DEFAULE_CAPACITY];
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return size == 0;
}
public E front() {
return this.elements[this.front];
}
public E rear(){
return this.elements[index(size-1)];
}
public void enQueueFront(E element) {
ensureCapacity(size + 1);
front = index(-1);
elements[front] = element;
size++;
}
public void enQueueRear(E element){
ensureCapacity(size + 1);
elements[index(size)] = element;
size++;
}
public E deQueueFront() {
E frontElement = elements[front];
elements[front] = null;
front = index(1);
size--;
return frontElement;
}
public E deQueueRear() {
int rearIndex = index(size - 1);
E rear = elements[rearIndex];
elements[rearIndex] = null;
size--;
return rear;
}
public int index(int index) {
return (this.front + index) % this.elements.length;
}
public E deQueue() {
E old = this.elements[this.front];
this.elements[this.front] = null;
this.front = index(1);
return old;
}
private void ensureCapacity(int capacity) {
int oldCapacity = elements.length;
if (capacity >= oldCapacity) {
return;
}
int newCapacity=oldCapacity+(oldCapacity>>1);
E[] newElementData= (E[]) new Object[newCapacity];
for(int i=0;i<size;i++){
newElementData[i]=elements[index(i)];
}
elements=newElementData;
this.front=0;
}
}