https://github.com/hslam/sortedlist
Package sortedlist implements sorted list.
https://github.com/hslam/sortedlist
Last synced: 8 months ago
JSON representation
Package sortedlist implements sorted list.
- Host: GitHub
- URL: https://github.com/hslam/sortedlist
- Owner: hslam
- License: mit
- Created: 2019-11-06T14:53:53.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-26T14:54:38.000Z (about 5 years ago)
- Last Synced: 2025-06-23T06:46:45.475Z (12 months ago)
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sortedlist
[](https://pkg.go.dev/github.com/hslam/sortedlist)
[](https://github.com/hslam/sortedlist/actions)
[](https://codecov.io/gh/hslam/sortedlist)
[](https://goreportcard.com/report/github.com/hslam/sortedlist)
[](https://github.com/hslam/sortedlist/blob/master/LICENSE)
Package sortedlist implements a sorted list.
## Get started
### Install
```
go get github.com/hslam/sortedlist
```
### Import
```
import "github.com/hslam/sortedlist"
```
### Usage
#### Example
```go
package main
import (
"fmt"
"github.com/hslam/sortedlist"
)
func main() {
s := sortedlist.New(sortedlist.LessInt)
fmt.Printf("ASC\t\t%t\n", s.Ascend())
s.Insert(1, 1)
s.Insert(3, 2)
s.Insert(3, 3)
s.Insert(5, 4)
s.Insert(6, 4)
s.Insert(2, 5)
s.Insert(2, 6)
s.Insert(4, 7)
printList(s)
fmt.Printf("Contain\t\tScore:2 Value:5\t%t\n", s.Contain(2, 5))
fmt.Printf("Remove\t\tScore:2 Value:5\t%t\n", s.Remove(2, 5))
fmt.Printf("ContainScore\tScore:3\t%t\n", s.ContainScore(3))
fmt.Printf("RemoveScore\tScore:3\t%t\n", s.RemoveScore(3))
fmt.Printf("ContainValue\tValue:4\t%t\n", s.ContainValue(4))
fmt.Printf("RemoveValue\tValue:4\t%t\n", s.RemoveValue(4))
printList(s)
top := s.Top()
fmt.Printf("Top\t\tScore:%d\tValue:%d\n", top.Score(), top.Value())
bottom := s.Bottom()
fmt.Printf("Bottom\t\tScore:%d\tValue:%d\n", bottom.Score(), bottom.Value())
printList(s)
}
func printList(s *sortedlist.SortedList) {
fmt.Println("\n===========Traverse===========")
cur := s.Front()
for cur != nil && cur != s.Rear() {
fmt.Printf("Traverse\tScore:%d\tValue:%d\n", cur.Score(), cur.Value())
cur = cur.Next()
}
fmt.Println("=============End==============\n")
}
```
### Output
```
ASC true
===========Traverse===========
Traverse Score:1 Value:1
Traverse Score:2 Value:5
Traverse Score:2 Value:6
Traverse Score:3 Value:2
Traverse Score:3 Value:3
Traverse Score:4 Value:7
Traverse Score:5 Value:4
Traverse Score:6 Value:4
=============End==============
Contain Score:2 Value:5 true
Remove Score:2 Value:5 true
ContainScore Score:3 true
RemoveScore Score:3 true
ContainValue Value:4 true
RemoveValue Value:4 true
===========Traverse===========
Traverse Score:1 Value:1
Traverse Score:2 Value:6
Traverse Score:4 Value:7
=============End==============
Top Score:1 Value:1
Bottom Score:4 Value:7
===========Traverse===========
Traverse Score:2 Value:6
=============End==============
```
### License
This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)
### Author
sortedlist was written by Meng Huang.