-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanstack.go
76 lines (66 loc) · 1.39 KB
/
cleanstack.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
64
65
66
67
68
69
70
71
72
73
74
75
76
package stack
import "fmt"
type Node[T any] struct {
value T
next *Node[T]
}
type CleanStack[T any] struct {
head *Node[T]
length int
}
func (stack *CleanStack[T]) Push(value T) {
node := &Node[T]{value: value}
stack.length += 1
if stack.head == nil {
stack.head = node
return
}
node.next = stack.head
stack.head = node
}
func (stack *CleanStack[T]) Pop() (value T) {
if stack.head == nil {
return value
}
stack.length -= 1
temp := stack.head
stack.head = stack.head.next
return temp.value
}
func (stack *CleanStack[T]) Peek() (value T) {
if stack.head == nil {
return value
}
return stack.head.value
}
func (stack *CleanStack[T]) Length() int {
return stack.length
}
func (stack *CleanStack[T]) String() string {
if stack.head == nil {
return "<empty stack>"
}
str := ""
temp := stack.head
for i := 0; i < stack.length; i++ {
str += fmt.Sprintf("%v\n", temp.value)
temp = temp.next
}
return str
}
func NewStack[T any](initialValues ...T) *CleanStack[T] {
stack := CleanStack[T]{length: 0}
for _, value := range initialValues {
stack.Push(value)
}
return &stack
}
func RunStackExample() {
exampleStack := NewStack[int](2, 3, 4, 5, 6)
stackLength := exampleStack.length
fmt.Println("Stack after pushing \n", exampleStack)
for i := 0; i < stackLength; i++ {
fmt.Println("Popped value", exampleStack.Pop())
}
fmt.Println("Stack after popping:", exampleStack)
}