3 回答
TA贡献1844条经验 获得超8个赞
这是使用链接数据结构的 LIFO 实现
package stack
import "sync"
type element struct {
data interface{}
next *element
}
type stack struct {
lock *sync.Mutex
head *element
Size int
}
func (stk *stack) Push(data interface{}) {
stk.lock.Lock()
element := new(element)
element.data = data
temp := stk.head
element.next = temp
stk.head = element
stk.Size++
stk.lock.Unlock()
}
func (stk *stack) Pop() interface{} {
if stk.head == nil {
return nil
}
stk.lock.Lock()
r := stk.head.data
stk.head = stk.head.next
stk.Size--
stk.lock.Unlock()
return r
}
func New() *stack {
stk := new(stack)
stk.lock = &sync.Mutex{}
return stk
}
- 3 回答
- 0 关注
- 161 浏览
添加回答
举报