Debian Codegraph @

modules/graph/stack.go

raw symbols SCIP
1 package graph
2
3 import "sync"
4
5 type entry struct {
6 data interface{}
7 next *entry
8 }
9
10 type Stack struct {
11 lock *sync.Mutex
12 head *entry
13 Size int
14 }
15
16 func (stk *Stack) Push(data interface{}) {
17 stk.lock.Lock()
18
19 element := new(entry)
20 element.data = data
21 temp := stk.head
22 element.next = temp
23 stk.head = element
24 stk.Size++
25
26 stk.lock.Unlock()
27 }
28
29 func (stk *Stack) Pop() interface{} {
30 if stk.head == nil {
31 return nil
32 }
33 stk.lock.Lock()
34 r := stk.head.data
35 stk.head = stk.head.next
36 stk.Size--
37
38 stk.lock.Unlock()
39
40 return r
41 }
42
43 func NewStack() *Stack {
44 stk := new(Stack)
45 stk.lock = &sync.Mutex{}
46 return stk
47 }