• LeetCode——622.设计循环队列


    1. package j12;
    2. import java.util.*;
    3. class A {
    4. int x[] = new int[1000];
    5. void addx(int location, int value) {
    6. x[location] = value;
    7. }
    8. }
    9. class MyCircularQueue {
    10. A a = new A();
    11. int length = 0;
    12. int user = 0;
    13. int rear_location = 0;
    14. int front_location = 0;
    15. public MyCircularQueue(int k) {
    16. length = k;
    17. }
    18. public boolean enQueue(int value) {
    19. if (user < length) {
    20. a.x[rear_location] = value;
    21. // a.addx(rear_location,value);
    22. rear_location++;
    23. user++;
    24. return true;
    25. }
    26. else {
    27. return false;
    28. }
    29. }
    30. public boolean deQueue() {
    31. if (length == 0||rear_location==front_location) {
    32. return false;
    33. } else {
    34. user--;
    35. front_location++;
    36. return true;
    37. }
    38. }
    39. public int Front() {
    40. if (length == 0||rear_location==front_location) {
    41. return -1;
    42. } else {
    43. return a.x[front_location];
    44. }
    45. }
    46. public int Rear() {
    47. if (length == 0||rear_location==front_location) {
    48. return -1;
    49. } else {
    50. int temp=rear_location;
    51. temp--;
    52. return a.x[temp];
    53. }
    54. }
    55. public boolean isEmpty() {
    56. if (user == 0) {
    57. return true;
    58. } else {
    59. return false;
    60. }
    61. }
    62. public boolean isFull() {
    63. if(user==length){
    64. return true;
    65. }
    66. else {
    67. return false;
    68. }
    69. }
    70. }
    71. /**
    72. * Your MyCircularQueue object will be instantiated and called as such:
    73. * MyCircularQueue obj = new MyCircularQueue(k);
    74. * boolean param_1 = obj.enQueue(value);
    75. * boolean param_2 = obj.deQueue();
    76. * int param_3 = obj.Front();
    77. * int param_4 = obj.Rear();
    78. * boolean param_5 = obj.isEmpty();
    79. * boolean param_6 = obj.isFull();
    80. */
    81. public class j13 {
    82. public static void main(String args[]) {
    83. MyCircularQueue circularQueue = new MyCircularQueue(6);
    84. System.out.println(circularQueue.enQueue(6));
    85. System.out.println(circularQueue.Rear());
    86. System.out.println(circularQueue.Rear());
    87. System.out.println(circularQueue.deQueue());
    88. System.out.println(circularQueue.enQueue(5));
    89. System.out.println(circularQueue.Rear());
    90. System.out.println(circularQueue.deQueue());
    91. System.out.println(circularQueue.Front());
    92. System.out.println(circularQueue.deQueue());
    93. System.out.println(circularQueue.deQueue());
    94. System.out.println(circularQueue.deQueue());
    95. // MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
    96. // System.out.println(circularQueue.enQueue(1)); // 返回 true
    97. // System.out.println(circularQueue.enQueue(2)); // 返回 true
    98. // System.out.println(circularQueue.enQueue(3)); // 返回 true
    99. // System.out.println(circularQueue.enQueue(4)); // 返回 false,队列已满
    100. // System.out.println(circularQueue.Rear()); // 返回 3
    101. // System.out.println(circularQueue.isFull()); // 返回 true
    102. // System.out.println(circularQueue.deQueue()); // 返回 true
    103. // System.out.println(circularQueue.enQueue(4)); // 返回 true
    104. // System.out.println(circularQueue.Rear()); // 返回 4
    105. }
    106. }

  • 相关阅读:
    使用 Flutter 和 Firebase 制作!计数器应用程序
    同构字符串(简单)
    虚拟机centos设置网络模式(桥接|NAT)
    【电压质量】提高隔离电源系统的电压质量(Simulink实现)
    C++ 11 新容器和新算法
    2023CSPJ 旅游巴士 —— dijkstra
    昇思25天学习打卡营第21天|RNN实现情感分类
    状态机-状态规划(309. 买卖股票的最佳时机含冷冻期)
    基于不同监督强度分类的语义分割综述:A Breif Survey on Semantic Segmentation with Deep Learning
    css样式在弹性盒元素在必要的时候拆行
  • 原文地址:https://blog.csdn.net/m0_51928176/article/details/126130731