队列:先进先出的数据结构
C#版本:
1 namespace Queue 2 { 3 interface IQueue<T> 4 { 5 /// <summary>入队操作</summary> 6 bool EnQueue(T _data); 7 /// <summary>出队操作</summary> 8 bool DeQueue(ref T _data); 9 /// <summary>取队头元素</summary> 10 bool GetFront(ref T _data); 11 /// <summary>获取队列长度</summary> 12 int QueueLength(); 13 /// <summary>是否为空</summary> 14 bool IsEmpty(); 15 /// <summary>清空队列</summary> 16 void Clear(); 17 /// <summary>是否满</summary> 18 bool IsFull(); 19 } 20 21 class QueueNode<T> 22 { 23 #region 成员属性 24 private T data; //数据 25 private QueueNode<T> next; //指针 26 #endregion 27 #region 公开属性 28 public T Data 29 { 30 get 31 { 32 return data; 33 } 34 set 35 { 36 data = value; 37 } 38 } 39 public QueueNode<T> Next; 40 #endregion 41 42 #region 构造函数 43 public QueueNode() 44 { 45 Data = default(T); 46 Next = null; 47 } 48 public QueueNode(T _data = default(T),QueueNode<T> _next = null) 49 { 50 Data = _data; 51 Next = _next; 52 } 53 #endregion 54 } 55 56 57 /// <summary>链式队列 </summary> 58 class LinkQueue<T> : IQueue<T> 59 { 60 #region 成员属性 61 private QueueNode<T> front; //队列头指示器 62 private QueueNode<T> rear; //队列尾指示器 63 private int nodeNum; //队列节点个数 64 #endregion 65 66 #region 访问函数 67 public LinkQueue() 68 { 69 front = null; 70 rear = null; 71 nodeNum = 0; 72 } 73 74 /// <summary>入队操作,数据保存在_data中</summary> 75 public bool EnQueue(T _data) 76 { 77 if (IsFull()) 78 return false; 79 80 QueueNode<T> q = new QueueNode<T>(_data); 81 if (IsEmpty()) 82 { 83 front = q; 84 rear = q; 85 } 86 else 87 { 88 rear.Next = q; 89 rear = q; 90 } 91 ++nodeNum; 92 93 return true; 94 } 95 96 /// <summary>出队操作,数据保存在_data中</summary> 97 public bool DeQueue(ref T _data) 98 { 99 if (IsEmpty()) 100 return false; 101 102 _data = front.Data; 103 front = front.Next; 104 105 if (front == null) 106 rear = null; 107 108 --nodeNum; 109 return true; 110 } 111 112 /// <summary>获取队头元素值操作,数据保存在_data中</summary> 113 public bool GetFront(ref T _data) 114 { 115 if (IsEmpty()) 116 return false; 117 _data = front.Data; 118 return true; 119 } 120 121 /// <summary>返回队列是否为空</summary> 122 public bool IsEmpty() 123 { 124 if (front == rear && 0 == nodeNum) 125 return true; 126 return false; 127 } 128 129 /// <summary>返回队列是否满</summary> 130 public bool IsFull() 131 { 132 return false; 133 } 134 135 /// <summary>返回队列长度</summary> 136 public int QueueLength() 137 { 138 return nodeNum; 139 } 140 141 /// <summary>清空队列</summary> 142 public void Clear() 143 { 144 front = rear = null; 145 nodeNum = 0; 146 } 147 148 #endregion 149 } 150 }