-
-
Notifications
You must be signed in to change notification settings - Fork 414
/
Copy pathstack.go
63 lines (53 loc) · 1.61 KB
/
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
55
56
57
58
59
60
61
62
63
package main
/*
An example of a stack being implemented in Go,
*/
import "fmt"
func main() {
// Initalize the stack type
stack := Stack{}
stack.Show() // Stack: []
stack.Push(42) // Adds to the stack, current stack = [42]
stack.Push(23) // Adds to the stack, current stack = [42 23]
stack.Push(5) // Adds to the stack, current stack = [42 23 5]
fmt.Println(stack.Size()) // 3
stack.Show() // Stack: [42 23 5]
fmt.Println(stack.Pop()) // 5
stack.Show() // Stack: [42 23]
fmt.Println(stack.Pop()) // 23
fmt.Println(stack.Size()) // 1
stack.Show() // Stack: [42]
fmt.Println(stack.Pop()) // 42
stack.Show() // Stack: []
fmt.Println(stack.Pop()) // panic: Attempt to pop an empty Stack!
}
// Struct to hold the stack items
type Stack struct {
Items []int
}
// Show prints out the current stack
func (stack *Stack) Show() {
fmt.Printf("Stack: %+v\n", stack.Items)
}
// Push adds an item to the current stack
func (stack *Stack) Push(item int) {
stack.Items = append(stack.Items, item)
}
// Pop removes the last item from the stack
// fails if the stack is empty
func (stack *Stack) Pop() int {
if !stack.IsEmpty() {
poppedItem := stack.Items[len(stack.Items)-1]
stack.Items = stack.Items[:len(stack.Items)-1]
return poppedItem
}
panic("Attempt to pop an empty Stack!")
}
// Size returns the length of the current stack as int
func (stack *Stack) Size() int {
return len(stack.Items)
}
// IsEmpty returns where the stack is empty or not as boolean
func (stack *Stack) IsEmpty() bool {
return len(stack.Items) == 0
}