-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllist-stack-min.go
More file actions
60 lines (47 loc) · 758 Bytes
/
llist-stack-min.go
File metadata and controls
60 lines (47 loc) · 758 Bytes
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
package main
import "fmt"
type List struct {
head *Node
tail *Node
}
type Node struct {
value int
prev *Node
next *Node
}
var min int
func (l *List) StackInsertBack(value int) {
n := &Node{
value: value,
prev: l.tail,
}
if l.tail != nil {
l.tail.next = n
}
l.tail = n
if l.head == nil {
l.head = n
}
}
func (l *List) StackMin() {
min = l.head.value
for l.head != nil {
fmt.Printf("%v\n", l.head.value)
if l.head.value < min {
min = l.head.value
}
l.head = l.head.next
}
fmt.Println("Min in the Stack:", min)
}
func main() {
l := List{}
l.StackInsertBack(2)
l.StackInsertBack(2)
l.StackInsertBack(1)
l.StackInsertBack(6)
l.StackInsertBack(3)
l.StackInsertBack(9)
l.StackInsertBack(5)
l.StackMin()
}