【LeetCode】循环队列

男娘i 2021-11-09 02:58 417阅读 0赞
Java code

通过神器通过神器通过神器通过神器通过神器通过神器

  1. class MyCircularQueue {
  2. private int front;
  3. private int rear;
  4. private int maxSize;
  5. private int size;
  6. private int[] arr;
  7. public MyCircularQueue(int k) {
  8. front = 0;
  9. rear = 0;
  10. size = 0;
  11. maxSize = k;
  12. arr = new int[maxSize];
  13. }
  14. public boolean enQueue(int value) {
  15. if(isFull()) {
  16. return false;
  17. }
  18. rear = (rear + 1) % maxSize;
  19. size++;
  20. arr[rear] = value;
  21. return true;
  22. }
  23. public boolean deQueue() {
  24. if(isEmpty()) {
  25. return false;
  26. }
  27. front = (front + 1) % maxSize;
  28. size--;
  29. return true;
  30. }
  31. public int Front() {
  32. if(isEmpty()) {
  33. return -1;
  34. }
  35. return arr[(front + 1) % maxSize];
  36. }
  37. public int Rear() {
  38. if(isEmpty()) {
  39. return -1;
  40. }
  41. return arr[rear];
  42. }
  43. public boolean isEmpty() {
  44. return size == 0;
  45. }
  46. public boolean isFull() {
  47. return size == maxSize;
  48. }
  49. }
提交记录

提交记录

发表评论

表情:
评论列表 (有 0 条评论,417人围观)

还没有评论,来说两句吧...

相关阅读

    相关 leetcode622设计循环队列

    使用数组完成对循环队列的设计 空闲单元法:进行队列是否为空,是否为满的情况; front : 指向队列中第一个元素 rear : 指向队列中最后一个元素的