-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks.go
48 lines (34 loc) · 984 Bytes
/
stacks.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
package main
// The main diference between a stack and a queue is the way that the data is removed
// -=-=-=-=-=-=-=- STACKS -=-=-=-=-=-=-=-=-=-
// Stacks have a Last in First Out data structure (LIFO)
// That the last one that you put in, would be the one that you take out first
// Like a book stack. We use PUSH => to insert items and POP => to remove items
import (
"fmt"
)
// Stack represents a stack that hold a slice
type Stack struct {
items []int
}
// Push items from Stack
func (s *Stack) Push(i int) {
s.items = append(s.items, i)
}
// Pop items from Stack (will remove a value at the end and return the removed value)
func (s *Stack) Pop() int {
l := len(s.items)-1
toRemove := s.items[l]
s.items = s.items[:l]
return toRemove
}
func main() {
myStack := Stack{}
fmt.Println(myStack)
myStack.Push(100)
myStack.Push(200)
myStack.Push(300)
fmt.Println(myStack)
myStack.Pop()
fmt.Println(myStack)
}