隊列,顧名思義就是像排隊一樣,先來先到,先進先出。
type Elem int
type Node struct {
data Elem
next *Node
}
type QueueLink struct {
front? *Node // 對頭
tail? *Node // 隊尾
length int
}
func (q *QueueLink) InitQueue() {
q.front = new(Node)
q.tail = q.front
q.length = 0
}
func (q *QueueLink) EnQueue(e Elem) {
node := new(Node)
node.data = e
node.next = nil
q.tail.next = node
q.tail = node
q.length++
}
func (q *QueueLink) OutQueue(e *Elem) error {
if q.Empty() {
return errors.New("empty queue.")
}
node := q.front.next
*e = node.data
q.front.next = node.next
// 如果彈出的是隊尾元素那么隊列就空了,這個時候隊尾等于隊列
if q.tail == node {
q.tail = q.front
}
q.length--
return nil
}
func (q *QueueLink) Empty() bool {
return q.front == q.tail
}
func (q *QueueLink) Length() int {
return q.length
}
func (q *QueueLink) Destroy() {
q.front = nil
q.tail = nil
}