队列实现------数组
代码的核心公式 | 数组ASize 跟队列最大有效QSize QSize=ASize- 1 |
---|---|
计算有效元素(不含数组最后一个null)个数 | (rear-front+maxSize)%maxSize |
判断是否已满 | (rear+1)%maxSize==front 满足则满 |
判断是否为空 | rear==front 满足为空 |
元素入队 | rear=(rear+1)%maxSize |
元素出队 | front=(front+1)%maxSize |
简单队列数组实现
package com.xm;
/**
* 队列数组形式
*
* @param <T>
*/
public class ArrayQueue<T> {
public ArrayQueue(int maxSize) throws IllegalAccessException {
if (maxSize < 0)
throw new IllegalAccessException("队列长度为负数");
this.maxSize = maxSize;
this.elementData = (T[]) new Object[maxSize];
this.front = 0;
this.rear = 0;
}
/**
* 队列信息数组
*/
private T[] elementData;
/**
* 队头元素位置
*/
private int front;
/**
* 指向最后一个元素的下一个位置
*/
private int rear;
/**
* 最大数组长度
*/
private final int maxSize;
/**
* 判断队列是否为满
*
* @return 是否为满
*/
public boolean isFull() {
if (rear == maxSize - 1) {
System.out.println("队列满,rear==MaxSize是数组满");
return true;
}
else
return false;
}
/**
* 入队
*
* @param element 元素
*/
public void add(T element) {
if (isFull())
throw new FullQueueException("队列已满,无法添加");
elementData[rear++] = element;
}
/**
* 队列师傅为空
*
* @return 是否为空
*/
public boolean isEmpty() {
return front == rear;
}
/**
* 出队
*
* @return 出队元素
*/
public T pop() {
if (isEmpty())
throw new EmptyQueueException("队列为空,无法出队");
T temp = elementData[front];
elementData[front++] = null;
return temp;
}
/**
* @return 队列最多可用大小,数组长度减1
*/
public int queueLength() {
return maxSize - 1;
}
/**
* 队列元素个数
*
* @return 元素个数
*/
public int elementsCount() {
return rear - front;
}
public T peekFirstElement() {
return elementData[front];
}
@Override
public String toString() {
if (isEmpty())
throw new EmptyQueueException("队列为空,浅浅的抛一下异常");
StringBuilder s = new StringBuilder();
s.append("toString->");
for (int i = front; i <= rear - 1; i++)
if (i != rear - 1)
s.append(elementData[i]).append("->");
else
s.append(elementData[i]).append("]");
return s.toString();
}
}
/**
* 自定义队列满异常
*/
class FullQueueException extends RuntimeException {
public FullQueueException() {
}
public FullQueueException(String msg) {
super(msg);
}
}
/**
* 自定义队列空异常
*/
class EmptyQueueException extends RuntimeException {
public EmptyQueueException() {
}
public EmptyQueueException(String msg) {
super(msg);
}
}
测试类
package com.xm;
public class Demo1 {
public static void main(String[] args) throws IllegalAccessException {
ArrayQueue<String> queue = new ArrayQueue(4);
queue.add("肖猛");
queue.add("肖扬");
queue.add("肖涵");
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.queueLength());
System.out.println(queue.toString());
ArrayQueue<Integer> queue1=new ArrayQueue<>(4);
queue1.add(1);
queue1.add(2);
queue1.add(3);
int sum=queue1.pop()+queue1.pop()+queue1.pop();
System.out.println(sum);
}
}
简单循环队列数组实现(可扩容)
package com.xm;
/**
* 循环队列数组形式
*
* @param <T> 泛型
*/
public class CircleArrayQueue<T> {
public CircleArrayQueue(int maxSize) throws IllegalAccessException {
if (maxSize < 0)
throw new IllegalAccessException("队列长度为负数");
this.maxSize = maxSize;
this.elementData = (T[]) new Object[maxSize];
this.front = 0;
this.rear = 0;
}
/**
* 队列信息数组
*/
private T[] elementData;
/**
* 队头元素位置
*/
private int front;
/**
* 指向最后一个元素的下一个位置
*/
private int rear;
/**
* 数组长度,可以稍微扩容一下
*/
private int maxSize;
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 浅浅的阔个容
*
* @param element
*/
public void add(T element) {
if (isFull()) {
maxSize *= 2;
T[] extData = (T[]) new Object[maxSize];
for (int i = 0; i < elementData.length; i++)
extData[i] = elementData[i];
elementData = extData;
elementData[rear] = element;
}
else
elementData[rear] = element;
rear = (rear + 1) % maxSize;
}
/**
* 判断队列是否为空
*
* @return 是否为空
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 弹出队列
*
* @return 弹出队列的元素
*/
public T get() {
if (isEmpty())
throw new EmptyQueueException("队列为空,无法出队");
T temp = elementData[front];
elementData[front] = null;
front = (front + 1) % maxSize;
return temp;
}
/**
* @return 队列最多可用大小,数组长度减1
*/
public int queueLength() {
return maxSize - 1;
}
/**
* 队列元素个数
*
* @return 元素个数
*/
public int elementsCount() {
return (rear - front + maxSize) % maxSize;
}
/**
* emmm.......
*
* @return emmm....
*/
public T peekFirstElement() {
return elementData[front];
}
@Override
public String toString() {
if (isEmpty())
throw new EmptyQueueException("队列为空,浅浅的抛一下异常");
StringBuilder s = new StringBuilder();
s.append("toString->[");
for (int i = front; i < front + elementsCount(); i++)
if (i == front + elementsCount() - 1)
s.append(elementData[i % maxSize]).append("]");
else
s.append(elementData[i % maxSize]).append("->");
return s.toString();
}
}
测试类
package com.xm;
public class Demo2 {
public static void main(String[] args) throws IllegalAccessException {
CircleArrayQueue<String> circleArrayQueue = new CircleArrayQueue<>(1);
circleArrayQueue.add("1");
circleArrayQueue.add("2");
circleArrayQueue.add("3");
circleArrayQueue.add("4");
circleArrayQueue.add("5");
System.out.println(circleArrayQueue.get());
System.out.println(circleArrayQueue.get());
circleArrayQueue.add("6");
System.out.println(circleArrayQueue);
CircleArrayQueue<Integer> circleArrayQueue1=new CircleArrayQueue<>(3);
circleArrayQueue1.add(1);
circleArrayQueue1.add(2);
System.out.println(circleArrayQueue1.get());
circleArrayQueue1.add(3);
System.out.println(circleArrayQueue1);
}
}
队列实现------链表
简单队列链表实现
package com.xm;
/**
* 单链表类
*/
public class SingleLinkedList {
/**
* 初始化
*/
public SingleLinkedList() {
this.first=new Node();
}
/**
* 起始节点
*/
public Node first;
/**
* 链表大小
*/
public int size = 0;
/**
* getLastNode
*/
public Node getLastNode() {
Node lastNode = this.first;
while (lastNode.next != null)
lastNode = lastNode.next;
// for(lastNode=this.first;lastNode.next!=null;lastNode=lastNode.next);
return lastNode;
}
/**
* add方法
*/
public void add(String item) {
size++;
Node lastNode = getLastNode();
lastNode.next = new Node(item);
}
/**
* 判断链表是否为空
*/
public boolean isEmpty() {
return this.first.next == null;
}
/**
* 出队
*/
public String get() {
if (isEmpty())
throw new EmptySingleLinkedList("链表为空");
size--;
Node secondNode;
secondNode = this.first.next;
this.first.next = first.next.next;
return secondNode.item;
}
/**
* 获取链表有效节点个数方式1
*/
public int getSize1(){
return size;
}
/**
* 获取链表有效节点个数方式2
*/
public int getSize2()
{
int cnt=0;
for(Node cntNode=first.next;cntNode!=null;cntNode=cntNode.next)
{
cnt++;
}
return cnt;
}
/**
* 获取链表中倒数第n个节点的值
*/
public String getLastIndexOf(int lastIndex)
{
if(isEmpty())
throw new EmptySingleLinkedList("链表为空");
if(lastIndex<=0||lastIndex>getSize2())
throw new IndexOutOfBoundsException("元素位置非法");
int index=getSize2()-lastIndex+1;
Node ansNode=this.first;
for(int i=0;i<index;i++)
ansNode=ansNode.next;
return ansNode.item;
}
/**
* 节点内容反转方式1
*/
public void Reverse1()
{
//数组反转实现
if(getSize1()>1)
{
Node nextNode;
Node reverseFirst=new Node();
Node currentNode=this.first.next;
while(currentNode!=null)
{
nextNode=currentNode.next;
currentNode.next=reverseFirst.next;
reverseFirst.next=currentNode;
currentNode=nextNode;
}
this.first.next=reverseFirst.next;
}
}
/**
* 打印链表元素(StringBuilder遍历,e.......)
*/
public void print() {
if(isEmpty())
throw new EmptySingleLinkedList("链表为空");
Node cntNode = this.first;
while (cntNode.next != null) {
System.out.println(cntNode.next.item);
cntNode = cntNode.next;
}
}
/**
* 打印链表元素递归
*/
public void print2() {
if(isEmpty())
throw new EmptySingleLinkedList("链表为空");
Node cntNode = this.first;
while (cntNode.next != null) {
System.out.println(cntNode.next.item);
cntNode = cntNode.next;
}
}
/**
* 节点类
* 类似静态成员方法,不能想象成静态成员变量
*/
public static class Node {
public Node() {
}
public Node(String item) {
this.item = item;
}
public Node(String item, Node next) {
this.item = item;
this.next = next;
}
/**
* 节点存储值
*/
private String item;
/**
*后置节点
*/
private Node next;
}
}
class EmptySingleLinkedList extends RuntimeException {
public EmptySingleLinkedList() {
}
public EmptySingleLinkedList(String msg) {
super(msg);
}
}
测试类
package com.xm;
import org.junit.Test;
import java.util.LinkedList;
public class Demo1 {
@Test
public void test() {
//查看底层源码实现
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("what1");
linkedList.add("what2");
linkedList.add("what3");
linkedList.remove(0);
System.out.println(linkedList);
System.out.println("---------------");
SingleLinkedList list = new SingleLinkedList();
list.add("宫本武藏");
list.add("阿通");
list.add("橘子");
list.Reverse1();
System.out.println("-----------");
list.print();
System.out.println("-----------");
System.out.println(list.getLastIndexOf(3));//注意:这里的参数是代表倒数第几个,从1开始,不是冲0开始
System.out.println("------------------");
System.out.println("得到链表有效节点个数:");
System.out.println(list.getSize1());
System.out.println(list.getSize2());
System.out.println("出队列节点的内容:");
System.out.println(list.get());
System.out.println(list.get());
System.out.println(list.get());
System.out.println("判断链表是否为空:");
System.out.println(list.isEmpty());
System.out.println("打印链表:");
list.print();
}
}