-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.go
More file actions
38 lines (31 loc) · 747 Bytes
/
stack.go
File metadata and controls
38 lines (31 loc) · 747 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
package stack
import (
"github.com/HotPotatoC/sture/linkedlist"
)
// Stack is a stack.
type Stack[T any] struct {
list *linkedlist.LinkedList[T]
cmp func(T, T) int
}
// NewStack returns a new stack.
func NewStack[T any](cmp func(T, T) int) *Stack[T] {
return &Stack[T]{
list: linkedlist.NewLinkedList(cmp),
}
}
// Add adds a new node to the top of the stack.
func (s *Stack[T]) Add(value T) {
s.list.Append(value)
}
// Pop removes the top node from the stack.
func (s *Stack[T]) Pop() {
s.list.Pop()
}
// Peek returns the value of the top node in the stack.
func (s *Stack[T]) Peek() T {
return s.list.Tail().Value()
}
// IsEmpty returns true if the stack is empty.
func (s *Stack[T]) IsEmpty() bool {
return s.list.IsEmpty()
}