# 数据结构-（队列顺序存储）循环队列的实现（golang）

```import (
```
```    "errors"
```
```)
```
```
```
```//循环队列结构体(顺序存储)
```
```type CirularQueue struct {
```
```    elem  []ElemType //储存的数据
```
```    front int        //头指针
```
```    rear  int        //尾指针
```
```}
```
```
```
```//初始化循环队列
```
```func InitCQueue() *CirularQueue {
```
```    cqueue := new(CirularQueue)
```
```    cqueue.elem = make([]ElemType, MAXSIZE)
```
```    cqueue.front = 0
```
```    cqueue.rear = 0
```
```    return cqueue
```
```}
```
```
```
```//清空循环队列
```
```func (cqueue *CirularQueue) ClearCQueue() {
```
```    cqueue.front = 0
```
```    cqueue.rear = 0
```
```    cqueue.elem = nil
```
```}
```
```
```
```//判断循环队列是否为空
```
```func (cqueue *CirularQueue) EmptyCQueue() bool {
```
```    if cqueue.front == cqueue.rear ||
```
```        cqueue.elem[cqueue.rear-1] == nil {
```
```        return true
```
```    }
```
```    return false
```
```}
```
```
```
```//获取循环队列的长度
```
```func (cqueue *CirularQueue) CQLength() int {
```
```    if cqueue.EmptyCQueue() {
```
```        return 0
```
```    }
```
```    return (cqueue.rear - cqueue.front + MAXSIZE) % MAXSIZE
```
```}
```
```
```
```//获取队列头结点
```
```func (cqueue *CirularQueue) GetCQueueHead() ElemType {
```
```    if cqueue.EmptyCQueue() {
```
```        return nil
```
```    }
```
```    return cqueue.elem[cqueue.front]
```
```}
```
```
```
```//插入新元素
```
```func (cqueue *CirularQueue) EnterCQueue(elem ElemType) (err error) {
```
```    if (cqueue.rear+1)%MAXSIZE == cqueue.front {
```
```        return errors.New("Cirular Queue full !")
```
```    }
```
```    cqueue.elem[cqueue.rear] = elem
```
```    cqueue.rear = (cqueue.rear + 1) % MAXSIZE
```
```    return err
```
```}
```
```
```
```//删除元素
```
```func (cqueue *CirularQueue) DeleteCQueue() ElemType {
```
```    if cqueue.EmptyCQueue() {
```
```        return nil
```
```    }
```
```    elem := cqueue.elem[cqueue.front]
```
```    cqueue.elem[cqueue.front] = nil
```
```    cqueue.front = (cqueue.front + 1) % MAXSIZE
```
```    return elem
```
```}
```
```
```

```import (
```
```
```
```    "testing"
```
```
```
```)
```
```
```
```
```
```
```
```var cqueue *CirularQueue
```
```
```
```
```
```
```
```func init() {
```
```
```
```    cqueue = InitCQueue()
```
```
```
```}
```
```
```
```
```
```
```
```func TestEnterCQueue(t *testing.T) {
```
```
```
```    err := cqueue.EnterCQueue("ONE")
```
```
```
```    if err != nil {
```
```
```
```        t.Error(err)
```
```
```
```    }
```
```
```
```}
```
```
```
```func TestGetCQueueHead(t *testing.T) {
```
```
```
```    _ = cqueue.GetCQueueHead()
```
```
```
```
```
```
```
```}
```
```
```
```func TestDeleteCQueue(t *testing.T) {
```
```
```
```    _ = cqueue.DeleteCQueue()
```
```
```
```}
```
```
```
```
```
```
```

1 回复  |  直到 2018-06-22 10:38:02

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传
```import (
```
```    "errors"
```
```)
```
```
```
```//循环队列结构体(顺序存储)
```
```type CirularQueue struct {
```
```    elem  []ElemType //储存的数据
```
```    front int        //头指针
```
```    rear  int        //尾指针
```
```}
```
```
```
```//初始化循环队列
```
```func InitCQueue() *CirularQueue {
```
```    cqueue := new(CirularQueue)
```
```    cqueue.elem = make([]ElemType, MAXSIZE)
```
```    cqueue.front = 0
```
```    cqueue.rear = 0
```
```    return cqueue
```
```}
```
```
```
```//清空循环队列
```
```func (cqueue *CirularQueue) ClearCQueue() {
```
```    cqueue.front = 0
```
```    cqueue.rear = 0
```
```    cqueue.elem = nil
```
```}
```
```
```
```//判断循环队列是否为空
```
```func (cqueue *CirularQueue) EmptyCQueue() bool {
```
```    if cqueue.front == cqueue.rear ||
```
```        cqueue.elem[cqueue.rear-1] == nil {
```
```        return true
```
```    }
```
```    return false
```
```}
```
```
```
```//获取循环队列的长度
```
```func (cqueue *CirularQueue) CQLength() int {
```
```    if cqueue.EmptyCQueue() {
```
```        return 0
```
```    }
```
```    return (cqueue.rear - cqueue.front + MAXSIZE) % MAXSIZE
```
```}
```
```
```
```//获取队列头结点
```
```func (cqueue *CirularQueue) GetCQueueHead() ElemType {
```
```    if cqueue.EmptyCQueue() {
```
```        return nil
```
```    }
```
```    return cqueue.elem[cqueue.front]
```
```}
```
```
```
```//插入新元素
```
```func (cqueue *CirularQueue) EnterCQueue(elem ElemType) (err error) {
```
```    if (cqueue.rear+1)%MAXSIZE == cqueue.front {
```
```        return errors.New("Cirular Queue full !")
```
```    }
```
```    cqueue.elem[cqueue.rear] = elem
```
```    cqueue.rear = (cqueue.rear + 1) % MAXSIZE
```
```    return err
```
```}
```
```
```
```//删除元素
```
```func (cqueue *CirularQueue) DeleteCQueue() ElemType {
```
```    if cqueue.EmptyCQueue() {
```
```        return nil
```
```    }
```
```    elem := cqueue.elem[cqueue.front]
```
```    cqueue.elem[cqueue.front] = nil
```
```    cqueue.front = (cqueue.front + 1) % MAXSIZE
```
```    return elem
```
```}
```
```
```

```import (
```
```
```
```    "testing"
```
```
```
```)
```
```
```
```
```
```
```
```var cqueue *CirularQueue
```
```
```
```
```
```
```
```func init() {
```
```
```
```    cqueue = InitCQueue()
```
```
```
```}
```
```
```
```
```
```
```
```func TestEnterCQueue(t *testing.T) {
```
```
```
```    err := cqueue.EnterCQueue("ONE")
```
```
```
```    if err != nil {
```
```
```
```        t.Error(err)
```
```
```
```    }
```
```
```
```}
```
```
```
```func TestGetCQueueHead(t *testing.T) {
```
```
```
```    _ = cqueue.GetCQueueHead()
```
```
```
```
```
```
```
```}
```
```
```
```func TestDeleteCQueue(t *testing.T) {
```
```
```
```    _ = cqueue.DeleteCQueue()
```
```
```
```}
```
```
```
```
```
```
```