-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstack.go
57 lines (44 loc) · 1.13 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
package ch03
import "errors"
// Item structure implements an item of the Stack/Queue
// Current implementation is based on Linked List
type Item[T any] struct {
data T
next *Item[T]
}
// New returns new stack node value data as a top value
func New[T any](data T) *Item[T] {
item := new(Item[T])
item.data = data
return item
}
// Stack structure implementation based on Linked List
type Stack[T any] struct {
top *Item[T]
}
// Pop returns (and removes) the top item from the stack
func (stack *Stack[T]) Pop() (T, error) {
if stack.top == nil {
return *new(T), errors.New("empty Stack error")
}
top := stack.top
stack.top = top.next
return top.data, nil
}
// Push add an item to the stack
func (stack *Stack[T]) Push(data T) {
top := New(data)
top.next = stack.top
stack.top = top
}
// Peek returns the top item from the satck (without removing it)
func (stack *Stack[T]) Peek() (T, error) {
if stack.top == nil {
return *new(T), errors.New("empty Stack error")
}
return stack.top.data, nil
}
// IsEmpty returns true when stack is empty, false otherwise
func (stack *Stack[T]) IsEmpty() bool {
return stack.top == nil
}