QueueHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ropin.Inspection.Common.Helper
  7. {
  8. public class QueueHelper
  9. {
  10. object[] data; //数据元素
  11. int maxsize; //最大容量
  12. int front; //指向队头
  13. int rear; //指向队尾
  14. //初始化队列
  15. public QueueHelper(int size)
  16. {
  17. this.maxsize = size;
  18. data = new object[size];
  19. front = rear = -1;
  20. }
  21. //最大容量属性
  22. public int MaxSize
  23. {
  24. get
  25. {
  26. return this.maxsize;
  27. }
  28. set
  29. {
  30. this.maxsize = value;
  31. }
  32. }
  33. //队尾属性
  34. public int Rear
  35. {
  36. get
  37. {
  38. return this.rear;
  39. }
  40. }
  41. //队头属性
  42. public int Front
  43. {
  44. get
  45. {
  46. return this.front;
  47. }
  48. }
  49. //数据属性
  50. public object this[int index]
  51. {
  52. get
  53. {
  54. return data[index];
  55. }
  56. }
  57. //获得队列的长度
  58. public int GetQueueLength()
  59. {
  60. return rear - front;
  61. }
  62. //判断队列是否满
  63. public bool IsFull()
  64. {
  65. if (GetQueueLength() == maxsize)
  66. return true;
  67. else
  68. return false;
  69. }
  70. //判断队列是否为空
  71. public bool IsEmpty()
  72. {
  73. if (rear == front)
  74. return true;
  75. else
  76. return false;
  77. }
  78. //清空队列
  79. public void ClearQueue()
  80. {
  81. rear = front = -1;
  82. }
  83. //入队
  84. public void In(object e)
  85. {
  86. if (IsFull())
  87. {
  88. Console.WriteLine("队列已满!");
  89. return;
  90. }
  91. data[++rear] = e;
  92. }
  93. //出队
  94. public object Out()
  95. {
  96. if (IsEmpty())
  97. {
  98. Console.WriteLine("队列为空!");
  99. return null;
  100. }
  101. if (rear - front > 0)
  102. {
  103. object tmp = data[++front];
  104. return tmp;
  105. }
  106. else
  107. {
  108. Console.WriteLine("全出队了!");
  109. ClearQueue();
  110. return null;
  111. }
  112. }
  113. //获得队头元素
  114. public object GetHead()
  115. {
  116. if (IsEmpty())
  117. {
  118. Console.WriteLine("队列为空!");
  119. return null;
  120. }
  121. return data[front + 1];
  122. }
  123. }
  124. }