栈,后进先出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
}

队列

队列,先进先出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
}