Change queues and stacks

- Make chain element private
- Remove JSON Marshaller from stack and queue
- Remove access to top chain element
This commit is contained in:
Melody Becker 2025-04-24 15:52:43 +02:00
parent 9870d87d41
commit 09de0a19e1
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
3 changed files with 18 additions and 52 deletions

View file

@ -1,13 +1,12 @@
package containers
import (
"encoding/json"
"errors"
)
type Stack[T any] struct {
top *ChainElem[T]
bottom *ChainElem[T]
top *chainElem[T]
bottom *chainElem[T]
}
// isValid checks if the stack is valid.
@ -61,17 +60,6 @@ func (s *Stack[T]) Top() (*T, error) {
return s.top.Elem, nil
}
// MarshalJSON is used by json.Marshal to create a json representation.
func (s *Stack[T]) MarshalJSON() ([]byte, error) {
if !s.isValid() {
return nil, errors.New("queue invalid")
}
if s.IsEmpty() {
return nil, errors.New("queue empty")
}
return json.Marshal(s.top)
}
func BuildStack[T any]() *Stack[T] {
empty := emptyElem[T]()
return &Stack[T]{