首页 > 其他 > 详细

栈和队列

时间:2020-03-13 14:52:32      阅读:61      评论:0      收藏:0      [点我收藏+]

栈,后进先出

import "sync"

type Value int

type Stack struct {
    d    []Value
    lock sync.RWMutex
}

func NewStack() *Stack{
    s := &Stack{}
    s.d = []Value{}
    return s
}

func (s *Stack) Push(t Value){
    s.lock.Lock()
    s.d = append(s.d,t)
    s.lock.Unlock()
}

func (s *Stack) Pop() Value{
    s.lock.Lock()
    ret := s.d[len(s.d)-1]
    s.d = s.d[:len(s.d)-1]
    s.lock.Unlock()
    return ret
}

func (s *Stack) IsEmpty() bool{
    return len(s.d) == 0
}

func (s *Stack) Size() int{
    return len(s.d)
}

队列

队列,先进先出

import "sync"

type Value int

type Queue struct {
    d    []Value
    lock sync.RWMutex
}

func NewQueue() *Queue {
    q := &Queue{}
    q.d = []Value{}
    return q
}

func (q *Queue) Push(t Value) {
    q.lock.Lock()
    q.d = append(q.d, t)
    q.lock.Unlock()
}

func (q *Queue) Pop() Value {
    q.lock.Lock()
    ret := q.d[0]
    q.d = q.d[:len(q.d)-1]
    q.lock.Unlock()
    return ret
}

func (q *Queue) Front() Value {
    q.lock.Lock()
    ret := q.d[0]
    q.lock.Unlock()
    return ret
}

func (q *Queue) IsEmpty() bool {
    return len(q.d) == 0
}

func (q *Queue) Size() int {
    return len(q.d)
}

栈和队列

原文:https://www.cnblogs.com/weiweng/p/12486369.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!