数组实现

  1. public class Queue<T> {
  2. // 前后两个指针
  3. private int front;
  4. private int rear;
  5. // 底层存储数组
  6. private T[] queue;
  7. public YKDArrayQueue(int size) {
  8. this.front = 0;
  9. this.rear = 0;
  10. // #1. 特别注意此处的数组泛型的写法
  11. this.queue = (T[]) new Object[size];
  12. }
  13. // 队列尾部添加元素
  14. public void add(T o) {
  15. this.queue[this.rear] = o;
  16. // 因此是循环队列,所以处理数组长度取余
  17. int newRear = (rear + 1) % this.queue.length;
  18. // 暂不考虑扩容情况
  19. if (newRear == this.front) {
  20. // 如果加入元素以后指针碰撞,则抛出越界提示
  21. throw new IndexOutOfBoundsException("队列已满");
  22. }
  23. this.rear = newRear;
  24. }
  25. // 删除队列头部元素
  26. public T remove() {
  27. if(this.size()==0){
  28. throw new IndexOutOfBoundsException("队列为空,不允许remove");
  29. }
  30. T data = this.queue[this.front];
  31. if (data!=null){
  32. this.queue[this.front] = null;
  33. this.front = (this.front+1)%this.queue.length;
  34. return data;
  35. }
  36. return null;
  37. }
  38. // 获取队列中索引位置元素
  39. public T get(int i) {
  40. if(i<0 || i>=this.size()){
  41. throw new IndexOutOfBoundsException("获取队列元素,越界");
  42. }
  43. int index = (i+this.front)%this.queue.length;
  44. return this.queue[index];
  45. }
  46. // 获取队列的长度
  47. public int size() {
  48. if (this.rear < this.front) {
  49. return this.rear + this.queue.length - this.front;
  50. }
  51. return this.rear - this.front;
  52. }
  53. public String toString() {
  54. StringBuffer sb = new StringBuffer();
  55. int i = this.front;
  56. while (i != this.rear) {
  57. sb.append(this.queue[i]);
  58. sb.append(" ");
  59. i++;
  60. }
  61. return sb.toString();
  62. }
  63. public static void main(String[] args) {
  64. Queue<Integer> queue = new Queue<>(20);
  65. queue.add(10);
  66. queue.add(5);
  67. queue.add(11);
  68. System.out.println(queue.get(0));
  69. System.out.println(queue.get(2));
  70. System.out.println(queue.remove());
  71. System.out.println(queue.remove());
  72. System.out.println(queue.toString());
  73. System.out.println(queue.size());
  74. }
  75. }

链表实现

Node

  1. public class Node<T> {
  2. private T content;
  3. private Node<T> next;
  4. public Node() {
  5. }
  6. public Node(T content) {
  7. this.content = content;
  8. }
  9. public Node(T content, Node<T> next) {
  10. this.content = content;
  11. this.next = next;
  12. }
  13. public T getContent() {
  14. return content;
  15. }
  16. public void setContent(T content) {
  17. this.content = content;
  18. }
  19. public Node<T> getNext() {
  20. return next;
  21. }
  22. public void setNext(Node<T> next) {
  23. this.next = next;
  24. }
  25. }
  1. package com.youkeda;
  2. public class LinkedQueue<T> {
  3. // 前后两个指针
  4. private Node<T> front;
  5. private Node<T> rear;
  6. private int size = 0;
  7. // 队列尾部添加元素
  8. public void add(T o) {
  9. Node<T> node = new Node<>(o);
  10. if (this.front == null) {
  11. this.front = node;
  12. } else {
  13. this.rear.setNext(node);
  14. }
  15. this.size++;
  16. this.rear = node;
  17. }
  18. // 删除队列头部元素
  19. public T remove() {
  20. if(this.front == null){
  21. throw new IndexOutOfBoundsException("队列为空,不能删除");
  22. }
  23. Node<T> temp = this.front;
  24. this.front = temp.getNext();
  25. temp.setNext(null);
  26. this.size--;
  27. return temp.getContent();
  28. }
  29. // 获取队列中索引位置元素
  30. public T get(int i) {
  31. if (i < 0 || i >= this.size()) {
  32. throw new IndexOutOfBoundsException("获取队列元素,越界");
  33. }
  34. Node<T> node = this.front;
  35. while(i > 0){
  36. node = node.getNext();
  37. i--;
  38. }
  39. return node.getContent();
  40. }
  41. // 获取队列的长度
  42. public int size() {
  43. return this.size;
  44. }
  45. public String toString() {
  46. StringBuffer sb = new StringBuffer();
  47. Node<T> node = this.front;
  48. while (node.getNext() != null) {
  49. sb.append(node.getContent());
  50. sb.append(" ");
  51. node = node.getNext();
  52. }
  53. return sb.toString();
  54. }
  55. public static void main(String[] args) {
  56. LinkedQueue<Integer> queue = new LinkedQueue<>();
  57. queue.add(10);
  58. queue.add(5);
  59. queue.add(11);
  60. System.out.println(queue.get(0));
  61. System.out.println(queue.get(2));
  62. System.out.println(queue.remove());
  63. System.out.println(queue.remove());
  64. System.out.println(queue.toString());
  65. System.out.println(queue.size());
  66. }
  67. }

队列常见算法——滑块窗口

image.png

获取长度最小的连续子数组