-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d611316
Showing
16 changed files
with
1,156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 HotPotatoC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Sture | ||
|
||
A collection of data structures based on Go 1.18+ Generics. | ||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/HotPotatoC/sture | ||
``` | ||
|
||
## Usage | ||
|
||
Creating a priority queue using: | ||
|
||
```go | ||
import "github.com/HotPotatoC/sture/queue" | ||
|
||
func main() { | ||
pq := queue.NewPriorityQueue[string]() | ||
|
||
pq.Enqueue("Adam", 1) | ||
pq.Enqueue("John", 3) | ||
pq.Enqueue("Bob", 2) | ||
|
||
top := pq.Peek() | ||
fmt.Println(top) // John | ||
} | ||
``` | ||
|
||
## Support | ||
|
||
If this project is helpful to you, please consider supporting me by donating or just give this project a 🌟 | ||
|
||
<a href="https://www.buymeacoffee.com/hotpotato" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/HotPotatoC/sture/queue" | ||
) | ||
|
||
func main() { | ||
pq := queue.NewPriorityQueue[string]() | ||
|
||
pq.Enqueue("Adam", 1) | ||
pq.Enqueue("John", 3) | ||
pq.Enqueue("Bob", 2) | ||
|
||
top := pq.Peek() | ||
fmt.Println(top) // John | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/HotPotatoC/sture | ||
|
||
go 1.18 | ||
|
||
require golang.org/x/exp v0.0.0-20220318154914-8dddf5d87bd8 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/exp v0.0.0-20220318154914-8dddf5d87bd8 h1:s/+U+w0teGzcoH2mdIlFQ6KfVKGaYpgyGdUefZrn9TU= | ||
golang.org/x/exp v0.0.0-20220318154914-8dddf5d87bd8/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
package linkedlist | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
// CircularLinkedList is a circular linked list. | ||
type CircularLinkedList[V constraints.Ordered] struct { | ||
head *Node[V] | ||
tail *Node[V] | ||
nSize uint | ||
} | ||
|
||
// NewCircularLinkedList returns a new circular linked list. | ||
func NewCircularLinkedList[V constraints.Ordered]() *CircularLinkedList[V] { | ||
return &CircularLinkedList[V]{} | ||
} | ||
|
||
// Append adds a new node to the end of the list. | ||
func (ll *CircularLinkedList[V]) Append(value V) { | ||
newNode := NewNode(value) | ||
ll.nSize++ | ||
|
||
if ll.head == nil { | ||
ll.head = newNode | ||
ll.tail = newNode | ||
return | ||
} | ||
|
||
newNode.prev = ll.tail | ||
newNode.next = ll.head | ||
ll.tail.next = newNode | ||
ll.tail = ll.tail.next | ||
ll.head.prev = ll.tail | ||
} | ||
|
||
// PushHead adds a new node to the head of the list. | ||
func (ll *CircularLinkedList[V]) PushHead(value V) { | ||
newNode := NewNode(value) | ||
ll.nSize++ | ||
|
||
if ll.head == nil { | ||
ll.head = newNode | ||
ll.tail = newNode | ||
return | ||
} | ||
|
||
newNode.next = ll.head | ||
ll.head.prev = newNode | ||
ll.head = newNode | ||
ll.tail.next = ll.head | ||
} | ||
|
||
// InsertAt adds a new node to the given position | ||
func (ll *CircularLinkedList[V]) InsertAt(pos int, value V) error { | ||
newNode := NewNode(value) | ||
|
||
if pos < 1 || uint(pos) > ll.nSize { | ||
return errors.New("invalid position") | ||
} | ||
|
||
currPos := 1 | ||
currNode := ll.head | ||
|
||
if pos == 1 { | ||
ll.PushHead(value) | ||
return nil | ||
} | ||
|
||
for currPos < pos { | ||
currNode = currNode.next | ||
currPos++ | ||
} | ||
|
||
newNode.prev = currNode.prev | ||
newNode.next = currNode | ||
currNode.prev.next = newNode | ||
currNode.prev = newNode | ||
|
||
ll.nSize++ | ||
|
||
return nil | ||
} | ||
|
||
// PushMid adds a new node to the middle of the list. | ||
func (ll *CircularLinkedList[V]) PushMid(value V) { | ||
newNode := NewNode(value) | ||
ll.nSize++ | ||
|
||
if ll.head == nil { | ||
ll.head = newNode | ||
ll.tail = newNode | ||
return | ||
} | ||
|
||
if ll.head.value > value { | ||
ll.PushHead(value) | ||
return | ||
} | ||
|
||
if ll.tail.value < value { | ||
ll.Append(value) | ||
return | ||
} | ||
|
||
curr := ll.head | ||
|
||
for curr.value < value { | ||
curr = curr.next | ||
} | ||
|
||
newNode.prev = curr.prev | ||
newNode.next = curr | ||
curr.prev.next = newNode | ||
curr.prev = newNode | ||
} | ||
|
||
// Pop removes the last node from the list. | ||
func (ll *CircularLinkedList[V]) Pop() { | ||
if ll.head == nil { | ||
return | ||
} | ||
|
||
ll.nSize-- | ||
|
||
if ll.head == ll.tail { | ||
ll.head = nil | ||
ll.tail = nil | ||
return | ||
} | ||
|
||
prev := ll.tail.prev | ||
prev.next = ll.head | ||
ll.tail = prev | ||
ll.head.prev = ll.tail | ||
} | ||
|
||
// PopHead removes the first node from the list. | ||
func (ll *CircularLinkedList[V]) PopHead() { | ||
if ll.head == nil { | ||
return | ||
} | ||
|
||
ll.nSize-- | ||
|
||
if ll.head == ll.tail { | ||
ll.head = nil | ||
ll.tail = nil | ||
return | ||
} | ||
|
||
next := ll.head.next | ||
next.prev = ll.tail | ||
ll.head = next | ||
ll.tail.next = ll.head | ||
} | ||
|
||
// Find returns the node with the given value. | ||
func (ll *CircularLinkedList[V]) Find(value V) *Node[V] { | ||
curr := ll.head | ||
|
||
for curr != nil { | ||
if curr.value == value { | ||
return curr | ||
} | ||
|
||
curr = curr.next | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// String returns a string representation of the list. | ||
func (ll *CircularLinkedList[V]) String() string { | ||
var s string | ||
|
||
curr := ll.head | ||
|
||
for curr != nil { | ||
s += fmt.Sprintf("[%v]", curr.value) | ||
if curr.next != ll.head { | ||
s += `-` | ||
} | ||
|
||
if curr.next == ll.head { | ||
return s | ||
} | ||
|
||
curr = curr.next | ||
} | ||
|
||
return s | ||
} | ||
|
||
// Head returns the head node of the list. | ||
func (ll *CircularLinkedList[V]) Head() *Node[V] { | ||
return ll.head | ||
} | ||
|
||
// Tail returns the tail node of the list. | ||
func (ll *CircularLinkedList[V]) Tail() *Node[V] { | ||
return ll.tail | ||
} | ||
|
||
// Size returns the size of the list. | ||
func (ll *CircularLinkedList[V]) Size() uint { | ||
return ll.nSize | ||
} | ||
|
||
// IsCircular returns true if the list is circular. | ||
func (ll *CircularLinkedList[V]) IsCircular() bool { | ||
return ll.head != nil && ll.head == ll.tail.next | ||
} | ||
|
||
// IsEmpty returns true if the list is empty. | ||
func (ll *CircularLinkedList[V]) IsEmpty() bool { | ||
return ll.head == nil | ||
} |
Oops, something went wrong.