zoukankan      html  css  js  c++  java
  • 栈和队列

    栈,后进先出

    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)
    }
    
  • 相关阅读:
    js 数据类型的转换
    js数组学习方法汇总
    跳转页面的方法总结
    今天用js做拉一个时钟
    今天用js做拉一个时钟
    js中字符的比较
    1005 继续(3n+1)猜想 (25分)
    1002 写出这个数
    日期差值
    1040 有几个PAT (25分)
  • 原文地址:https://www.cnblogs.com/weiweng/p/12486369.html
Copyright © 2011-2022 走看看