队列是一个有序列表,可以用数组或链表来实现,遵循先入先出的原则

  1. /**
  2. * 队列
  3. */
  4. public interface Queue {
  5. /**
  6. * 判断队列是否为空
  7. */
  8. boolean isEmpty();
  9. /**
  10. * 判断队列是否已满
  11. */
  12. boolean isFull();
  13. /**
  14. * 添加数据
  15. */
  16. void add(int o);
  17. /**
  18. * 取出数据
  19. */
  20. int get();
  21. /**
  22. * 队列元素个数
  23. */
  24. int size();
  25. /**
  26. * 显示队列数据
  27. */
  28. void show();
  29. /**
  30. * 显示队列头数据
  31. */
  32. int showHead();


数组实现队列

  1. /**
  2. * 数组实现队列
  3. */
  4. public class ArrayQueue implements Queue {
  5. /**
  6. * 队列容量(空余一个空间)
  7. */
  8. private int maxSize;
  9. /**
  10. * 队列头指针:指向队列的第一个元素
  11. */
  12. private int head;
  13. /**
  14. * 队列尾指针:指向队列的最后一个元素的后一个位置
  15. */
  16. private int tail;
  17. /**
  18. * 存放数据
  19. */
  20. private int[] arr;
  21. public ArrayQueue(int size) {
  22. maxSize = size + 1;
  23. arr = new int[maxSize];
  24. head = 0;
  25. tail = 0;
  26. }
  27. /**
  28. * 判断队列是否为空
  29. * @return
  30. */
  31. @Override
  32. public boolean isEmpty() {
  33. return head == tail;
  34. }
  35. /**
  36. * 判断队列是否已满
  37. * @return
  38. */
  39. @Override
  40. public boolean isFull() {
  41. return (tail + 1) % maxSize == head;
  42. }
  43. /**
  44. * 添加数据
  45. * @param o
  46. */
  47. @Override
  48. public void add(int o) {
  49. if (isFull()) {
  50. System.out.println("添加失败,队列已满!");
  51. return;
  52. }
  53. arr[tail] = o;
  54. tail = (tail + 1) % maxSize;
  55. }
  56. /**
  57. * 取出数据
  58. * @return
  59. */
  60. @Override
  61. public int get() {
  62. if (isEmpty()) {
  63. throw new RuntimeException("取出失败,队列已空!");
  64. }
  65. int getValue = arr[head];
  66. head = (head + 1) % maxSize;
  67. return getValue;
  68. }
  69. /**
  70. * 队列元素个数
  71. * @return
  72. */
  73. @Override
  74. public int size() {
  75. return (tail - head + maxSize) % maxSize;
  76. }
  77. /**
  78. * 显示队列数据
  79. */
  80. @Override
  81. public void show() {
  82. for (int i = head; i< head + size(); i++) {
  83. System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
  84. }
  85. }
  86. /**
  87. * 显示队列头数据
  88. * @return
  89. */
  90. @Override
  91. public int showHead() {
  92. if (isEmpty()) {
  93. throw new RuntimeException("队列头为空!");
  94. }
  95. return arr[head];
  96. }
  97. public static void main(String[] args) {
  98. ArrayQueue queue = new ArrayQueue(3);
  99. Scanner scanner = new Scanner(System.in);
  100. boolean loop = true;
  101. while (loop) {
  102. System.out.print("【数组实现队列】显示队列(s)、添加数据(a)、取出数据(g)、队列头数据(h)、队列元素个数(l)、退出(e):");
  103. char key = scanner.next().charAt(0);
  104. switch (key) {
  105. case 's': queue.show(); break;
  106. case 'a':
  107. System.out.print("请输入一个数:");
  108. int value = scanner.nextInt();
  109. queue.add(value);
  110. break;
  111. case 'g':
  112. try {
  113. System.out.println("取出的数据是:" + queue.get());
  114. } catch (Exception e) {
  115. System.out.println(e.getMessage());
  116. }
  117. break;
  118. case 'h':
  119. try {
  120. System.out.println("头数据是:" + queue.showHead());
  121. } catch (Exception e) {
  122. System.out.println(e.getMessage());
  123. }
  124. break;
  125. case 'l': System.out.println("队列元素个数是:" + queue.size()); break;
  126. case 'e':
  127. scanner.close();
  128. loop = false;
  129. break;
  130. default: break;
  131. }
  132. }
  133. System.out.println("程序退出。。。");
  134. }
  135. }


链表实现队列

  1. /**
  2. * 链表实现队列
  3. */
  4. public class LinkedListQueue implements Queue {
  5. /**
  6. * 队列单个节点
  7. */
  8. private class Node {
  9. private int data;
  10. private Node next;
  11. public Node(int data) {
  12. this.data = data;
  13. }
  14. @Override
  15. public String toString() {
  16. return "Node{" + "data=" + data + '}';
  17. }
  18. }
  19. /**
  20. * 队列容量
  21. */
  22. private int maxSize;
  23. /**
  24. * 队列头指针:指向队列的第一个节点
  25. */
  26. private Node head;
  27. /**
  28. * 队列尾指针:指向队列的最后一个节点
  29. */
  30. private Node tail;
  31. /**
  32. * 队列元素个数
  33. */
  34. private int realSize;
  35. public LinkedListQueue(int size) {
  36. this.maxSize = size;
  37. this.realSize = 0;
  38. this.head = null;
  39. this.tail = null;
  40. }
  41. /**
  42. * 判断队列是否为空
  43. * @return
  44. */
  45. @Override
  46. public boolean isEmpty() {
  47. return realSize == 0;
  48. }
  49. /**
  50. * 判断队列是否已满
  51. * @return
  52. */
  53. @Override
  54. public boolean isFull() {
  55. return realSize == maxSize;
  56. }
  57. /**
  58. * 添加数据
  59. * @param o
  60. */
  61. @Override
  62. public void add(int o) {
  63. if (isFull()) {
  64. System.out.println("添加失败,队列已满!");
  65. return;
  66. }
  67. Node node = new Node(o);
  68. // 如果队列为空
  69. if (isEmpty()) {
  70. head = node;
  71. tail = node;
  72. } else {
  73. tail.next = node;
  74. tail = node;
  75. }
  76. realSize++;
  77. }
  78. /**
  79. * 取出数据
  80. * @return
  81. */
  82. @Override
  83. public int get() {
  84. if (isEmpty()) {
  85. throw new RuntimeException("取出失败,队列已空!");
  86. }
  87. int tmp = head.data;
  88. head = head.next;
  89. realSize--;
  90. // 队列为空时,队列尾指针置为null
  91. if (realSize == 0) {
  92. tail = null;
  93. }
  94. return tmp;
  95. }
  96. /**
  97. * 队列元素个数
  98. * @return
  99. */
  100. @Override
  101. public int size() {
  102. return realSize;
  103. }
  104. /**
  105. * 显示队列数据
  106. */
  107. @Override
  108. public void show() {
  109. if (head == null) {
  110. System.out.println("队列为空!");
  111. return;
  112. }
  113. for(Node temp = head; temp != null; temp = temp.next) {
  114. System.out.println(temp);
  115. }
  116. }
  117. /**
  118. * 显示队列头数据
  119. * @return
  120. */
  121. @Override
  122. public int showHead() {
  123. if (isEmpty()) {
  124. throw new RuntimeException("队列头为空!");
  125. }
  126. return head.data;
  127. }
  128. public static void main(String[] args) {
  129. LinkedListQueue queue = new LinkedListQueue(3);
  130. Scanner scanner = new Scanner(System.in);
  131. boolean loop = true;
  132. while (loop) {
  133. System.out.print("【链表实现队列】显示队列(s)、添加数据(a)、取出数据(g)、队列头数据(h)、队列元素个数(l)、退出(e):");
  134. char key = scanner.next().charAt(0);
  135. switch (key) {
  136. case 's': queue.show(); break;
  137. case 'a':
  138. System.out.print("请输入一个数:");
  139. int value = scanner.nextInt();
  140. queue.add(value);
  141. break;
  142. case 'h':
  143. try {
  144. System.out.println("头数据是:" + queue.showHead());
  145. } catch (Exception e) {
  146. System.out.println(e.getMessage());
  147. }
  148. break;
  149. case 'g':
  150. try {
  151. System.out.println("取出的数据是:" + queue.get());
  152. } catch (Exception e) {
  153. System.out.println(e.getMessage());
  154. }
  155. break;
  156. case 'l': System.out.println("队列元素个数是:" + queue.size()); break;
  157. case 'e':
  158. scanner.close();
  159. loop = false;
  160. break;
  161. default: break;
  162. }
  163. }
  164. System.out.println("程序退出。。。");
  165. }
  166. }