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:
parent
9870d87d41
commit
09de0a19e1
3 changed files with 18 additions and 52 deletions
|
@ -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]{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue