Ai
7 Star 53 Fork 26

ryanduan/wsPool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CircularLinkedList.go 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
ryanduan 提交于 2020-11-18 14:45 +08:00 . 修复bug 和并发性能提升
package queue
//环形队列
import (
"container/list"
"fmt"
"sync"
)
type CircularLinked struct {
data *list.List
}
var lock1 sync.Mutex
var _ *CircularLinked
func (q *CircularLinked) Push(v interface{}) {
defer lock1.Unlock()
lock1.Lock()
q.data.PushFront(v)
}
func (q *CircularLinked) Pop() interface{} {
defer lock1.Unlock()
lock1.Lock()
iter := q.data.Back()
v := iter.Value
q.data.Remove(iter)
return v
}
//读取第一个元素后,把它移到队列尾
func (q *CircularLinked) Front() *list.Element {
defer lock1.Unlock()
lock1.Lock()
iter := q.data.Front()
q.data.Remove(iter)
if iter != nil {
//q.data.MoveToBack(iter)
q.data.PushBack(iter.Value.(string))
}
return iter
}
//移除元素
func (q *CircularLinked) Remove(iter *list.Element) {
defer lock1.Unlock()
lock1.Lock()
if iter != nil {
q.data.Remove(iter)
}
}
func (q *CircularLinked) Len() int {
return q.data.Len()
}
func (q *CircularLinked) Dump() {
for iter := q.data.Back(); iter != nil; iter = iter.Prev() {
fmt.Println("item:", iter.Value)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rczweb/wsPool.git
git@gitee.com:rczweb/wsPool.git
rczweb
wsPool
wsPool
v1.4.5

搜索帮助