stack.go
1.7 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package utils
import "sync"
// Item interface to store any data type in stack
type Item interface{}
// Stack struct which contains a list of Items
type Stack struct {
items []Item
mutex sync.Mutex
}
// NewEmptyStack() returns a new instance of Stack with zero elements
func NewEmptyStack() *Stack {
return &Stack{
items: nil,
}
}
// NewStack() returns a new instance of Stack with list of specified elements
func NewStack(items []Item) *Stack {
return &Stack{
items: items,
}
}
// Push() adds new item to top of existing/empty stack
func (stack *Stack) Push(item Item) {
stack.mutex.Lock()
defer stack.mutex.Unlock()
stack.items = append(stack.items, item)
}
// Pop() removes most recent item(top) from stack
func (stack *Stack) Pop() Item {
stack.mutex.Lock()
defer stack.mutex.Unlock()
if len(stack.items) == 0 {
return nil
}
lastItem := stack.items[len(stack.items)-1]
stack.items = stack.items[:len(stack.items)-1]
return lastItem
}
// IsEmpty() returns whether the stack is empty or not (boolean result)
func (stack *Stack) IsEmpty() bool {
stack.mutex.Lock()
defer stack.mutex.Unlock()
return len(stack.items) == 0
}
// Top() returns the last inserted item in stack without removing it.
func (stack *Stack) Top() Item {
stack.mutex.Lock()
defer stack.mutex.Unlock()
if len(stack.items) == 0 {
return nil
}
return stack.items[len(stack.items)-1]
}
// Size() returns the number of items currently in the stack
func (stack *Stack) Size() int {
stack.mutex.Lock()
defer stack.mutex.Unlock()
return len(stack.items)
}
// Clear() removes all items from the stack
func (stack *Stack) Clear() {
stack.mutex.Lock()
defer stack.mutex.Unlock()
stack.items = nil
}