-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathStack.go
54 lines (44 loc) · 777 Bytes
/
Stack.go
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
package StackLinkedList
type Node struct {
data int
next *Node
}
type Stack struct {
top *Node
}
func (list *Stack) Push(i int) {
data := &Node{data: i}
if list.top != nil {
data.next = list.top
}
list.top = data
}
func (list *Stack) Pop() (int, bool) {
if list.top == nil {
return 0, false
}
i := list.top.data
list.top = list.top.next
return i, true
}
func (list *Stack) Peek() (int, bool) {
if list.top == nil {
return 0, false
}
return list.top.data, true
}
func (list *Stack) Get() []int {
var items []int
current := list.top
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}
func (list *Stack) IsEmpty() bool {
return list.top == nil
}
func (list *Stack) Empty() {
list.top = nil
}