-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
40 lines (34 loc) · 863 Bytes
/
stack.go
File metadata and controls
40 lines (34 loc) · 863 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
package main
// Stack represents a stack data structure.
type Stack struct {
items []interface{}
}
// Push adds a new element to the top of the stack.
func (s *Stack) Push(item interface{}) {
s.items = append(s.items, item)
}
// Pop removes and returns the top element from the stack.
func (s *Stack) Pop() interface{} {
if len(s.items) == 0 {
return nil
}
topIndex := len(s.items) - 1
topItem := s.items[topIndex]
s.items = s.items[:topIndex]
return topItem
}
// Peek returns the top element from the stack without removing it.
func (s *Stack) Peek() interface{} {
if len(s.items) == 0 {
return nil
}
return s.items[len(s.items)-1]
}
// Size returns the number of elements in the stack.
func (s *Stack) Size() int {
return len(s.items)
}
// IsEmpty checks if the stack is empty.
func (s *Stack) IsEmpty() bool {
return len(s.items) == 0
}