# 数据结构-队列链式存储结构的实现（golang）

```//队列的链式存储结构的实现
```
```//队列链式存储结构体
```
```type LSQueue struct {
```
```    front *LSQNode //头指针
```
```    rear  *LSQNode //尾指针
```
```    count int      //队列的线性长度
```
```}
```
```
```
```//队列链式存储结构节点
```
```type LSQNode struct {
```
```    elem    ElemType //储存的数据
```
```    next    *LSQNode //指向下一个节点的指针
```
```    lsQueue *LSQueue //所属队列
```
```}
```
```
```
```//初始化队列
```
```func InitLSQueue() *LSQueue {
```
```    lsQueue := new(LSQueue)
```
```    node := new(LSQNode)
```
```    node.elem = nil
```
```    node.next = nil
```
```    node.lsQueue = lsQueue
```
```    lsQueue.front = node
```
```    lsQueue.rear = node
```
```    lsQueue.count = 0
```
```    return lsQueue
```
```}
```
```
```
```//插入元素
```
```func (lsQueue *LSQueue) InsertLSQueueElem(elem ElemType) *LSQNode {
```
```    node := new(LSQNode)
```
```    node.elem = elem
```
```    node.next = nil
```
```    node.lsQueue = lsQueue
```
```    lsQueue.rear.next = node
```
```    lsQueue.rear = node
```
```    lsQueue.count++
```
```    return node
```
```}
```
```
```
```//删除元素
```
```func (lsQueue *LSQueue) DeleteLSQueueElem() *LSQNode {
```
```    if lsQueue.EmptyQueue() {
```
```        return nil
```
```    }
```
```    node := lsQueue.front.next
```
```    lsQueue.front.next = node.next
```
```    lsQueue.count--
```
```
```
```    if node == lsQueue.rear {
```
```        lsQueue.front = lsQueue.rear
```
```    }
```
```    return node
```
```}
```
```
```
```//判断队列是否为空
```
```func (lsQueue *LSQueue) EmptyQueue() bool {
```
```    if lsQueue.front == lsQueue.rear {
```
```        return true
```
```    }
```
```    return false
```
```}
```
```
```
```//清空队列
```
```func (lsQueue *LSQueue) ClearLSQueue() {
```
```    if lsQueue.EmptyQueue() {
```
```        return
```
```    }
```
```    lsQueue.front = lsQueue.rear
```
```    lsQueue.count = 0
```
```}
```
```
```
```//获取队列线性长度
```
```func (lsQueue *LSQueue) LSQueueLength() int {
```
```    if lsQueue.EmptyQueue() {
```
```        return 0
```
```    }
```
```    return lsQueue.count
```
```}
```
```
```
```import (
```
```    "testing"
```
```)
```
```
```
```var lsQueue *LSQueue
```
```
```
```func init() {
```
```    lsQueue = InitLSQueue()
```
```}
```
```
```
```func TestInsertLSQueueElem(t *testing.T) {
```
```    _= lsQueue.InsertLSQueueElem("ONE")
```
```
```
```}
```
```
```
```func TestDeleteLSQueueElem(t *testing.T) {
```
```    _= lsQueue.DeleteLSQueueElem()
```
```}
```
```
```

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传
```//队列的链式存储结构的实现
```
```//队列链式存储结构体
```
```type LSQueue struct {
```
```    front *LSQNode //头指针
```
```    rear  *LSQNode //尾指针
```
```    count int      //队列的线性长度
```
```}
```
```
```
```//队列链式存储结构节点
```
```type LSQNode struct {
```
```    elem    ElemType //储存的数据
```
```    next    *LSQNode //指向下一个节点的指针
```
```    lsQueue *LSQueue //所属队列
```
```}
```
```
```
```//初始化队列
```
```func InitLSQueue() *LSQueue {
```
```    lsQueue := new(LSQueue)
```
```    node := new(LSQNode)
```
```    node.elem = nil
```
```    node.next = nil
```
```    node.lsQueue = lsQueue
```
```    lsQueue.front = node
```
```    lsQueue.rear = node
```
```    lsQueue.count = 0
```
```    return lsQueue
```
```}
```
```
```
```//插入元素
```
```func (lsQueue *LSQueue) InsertLSQueueElem(elem ElemType) *LSQNode {
```
```    node := new(LSQNode)
```
```    node.elem = elem
```
```    node.next = nil
```
```    node.lsQueue = lsQueue
```
```    lsQueue.rear.next = node
```
```    lsQueue.rear = node
```
```    lsQueue.count++
```
```    return node
```
```}
```
```
```
```//删除元素
```
```func (lsQueue *LSQueue) DeleteLSQueueElem() *LSQNode {
```
```    if lsQueue.EmptyQueue() {
```
```        return nil
```
```    }
```
```    node := lsQueue.front.next
```
```    lsQueue.front.next = node.next
```
```    lsQueue.count--
```
```
```
```    if node == lsQueue.rear {
```
```        lsQueue.front = lsQueue.rear
```
```    }
```
```    return node
```
```}
```
```
```
```//判断队列是否为空
```
```func (lsQueue *LSQueue) EmptyQueue() bool {
```
```    if lsQueue.front == lsQueue.rear {
```
```        return true
```
```    }
```
```    return false
```
```}
```
```
```
```//清空队列
```
```func (lsQueue *LSQueue) ClearLSQueue() {
```
```    if lsQueue.EmptyQueue() {
```
```        return
```
```    }
```
```    lsQueue.front = lsQueue.rear
```
```    lsQueue.count = 0
```
```}
```
```
```
```//获取队列线性长度
```
```func (lsQueue *LSQueue) LSQueueLength() int {
```
```    if lsQueue.EmptyQueue() {
```
```        return 0
```
```    }
```
```    return lsQueue.count
```
```}
```
```
```
```import (
```
```    "testing"
```
```)
```
```
```
```var lsQueue *LSQueue
```
```
```
```func init() {
```
```    lsQueue = InitLSQueue()
```
```}
```
```
```
```func TestInsertLSQueueElem(t *testing.T) {
```
```    _= lsQueue.InsertLSQueueElem("ONE")
```
```
```
```}
```
```
```
```func TestDeleteLSQueueElem(t *testing.T) {
```
```    _= lsQueue.DeleteLSQueueElem()
```
```}
```
```
```