自己实现数据结构系列四---Queue
一.代码部分
1.定义接口:
public interface Queue<E> { void enqueue(E e); E dequeue(); E getFront(); int getSize(); boolean isEmpty(); }
2.基于数组的实现
public class ArrayQueue<E> implements Queue<E> { private ArrayList<E> arrayList; public ArrayQueue(int capacity){ arrayList = new ArrayList<>(capacity); } public ArrayQueue(){ arrayList = new ArrayList<>(); } @Override public void enqueue(E e) { arrayList.addLast(e); } @Override public E dequeue() { return arrayList.removeFirst(); } @Override public E getFront() { return arrayList.get(0); } @Override public int getSize() { return arrayList.getSize(); } @Override public boolean isEmpty() { return arrayList.isEmpty(); } }
3.改进链表实现队列:
public class LinkedListQueue<E> implements Queue<E> { //节点,用来存放数据:数据+下一个元素的引用 private class Node{ private E e; private Node next; public Node(E e,Node next){ this.e = e; this.next = next; } public Node(E e){ this(e,null); } public Node(){ this(null,null); } public String toString(){ return e.toString(); } } private Node head;//头节点 private Node tail;//尾节点 private int size; public LinkedListQueue(){ head = null; tail = null; 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("empty"); 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("empty"); return head.e; } @Override public int getSize() { return size; } @Override public boolean isEmpty() { return size ==0; } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append("queue:front"); Node cur = head; while (cur != null){ res.append(cur + "->"); cur = cur.next; } res.append("null tail"); return res.toString(); } }
3.循环队列:
public class LoopQueue<E> { private E[] data; private int front;//队列头 private int tail;//队列尾 private int size; /** * 初始化容量的构造方法 * @param capacity */ 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; } public boolean isEmpty(){ return front == tail; } public int getSize(){ return size; } /** * 改变数组容量 * @param newCapacity */ private void resize(int newCapacity){ E[] newData = (E[]) new Object[newCapacity+1]; for (int i = 0; i < size; i++) { newData[i] = data[(i+front)%data.length]; } data = newData; front = 0; tail = size; } /** * 入队 * @param e */ public void enqueue(E e){ if ((tail+1)%data.length == front){ resize(getCapacity()*2); } data[tail] = e; tail = (tail+1)%data.length;//注意这里不是tail++ size++; } /** * 出队 * @return */ public E dequeue(){ if (isEmpty()){ throw new IllegalArgumentException("can't dequeue"); } E ret = data[front]; data[front] = null; front = (front+1)%data.length;//注意这里不是front++ size--; if(size == getCapacity()/4 && getCapacity()/2 != 0){ resize(getCapacity()/2); } return ret; } /** * 队列头 * @return */ public E getFront(){ if (isEmpty()){ throw new IllegalArgumentException("queue is empty"); } return data[front]; } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append(String.format("queue size =%d, capacity = %d\n"),size,getCapacity()); res.append("front ["); for (int i = front; i != tail; i = (i=1)%data.length) { res.append(data[i]); if((i+1)%data.length != tail){ res.append(","); } } res.append("] tail"); return res.toString(); } }
标签:
数据结构
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 编码之道,道心破碎。
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
· 知名开源项目Alist被收购!惹程序员众怒,开团炮轰甲方
· 突发,小红书开发者后门被破解?!
· 历时半年,我将一个大型asp.net的零代码快速开发平台转成了java
· [原创]《C#高级GDI+实战:从零开发一个流程图》第03章:画一个线,连接两个矩形!
· C# 将 CSV 转化为 Excel