数组
package main
import (
"errors"
"fmt"
)
func main() {
stack1 := createQueue[int]()
err := stack1.push(1)
if err != nil {
return
}
stack1.push(2)
stack1.push(3)
stack1.push(4)
stack1.push(5)
popData1 := stack1.pop()
fmt.Printf("出队列元素%+v\n", *popData1)
popData2 := stack1.pop()
fmt.Printf("出队列元素%+v\n", *popData2)
stack1.push(6)
stack1.push(7)
stack1.push(8)
stack1.push(9)
stack1.pop()
err = stack1.push(10)
if err != nil {
fmt.Printf(err.Error() + "\n")
}
stack1.forEach()
}
type queue[T int | string | map[string]string] struct {
data [6]T
head int
tail int
}
func createQueue[T int | string | map[string]string]() *queue[T] {
return &queue[T]{
data: [6]T{},
}
}
func (s *queue[T]) push(item T) error {
if len(s.data)-1 == s.tail {
if s.head == 0 {
return errors.New("队列满!")
}
currentTail := s.tail - s.head
for i := 0; i < currentTail; i++ {
s.data[i] = s.data[i+s.head]
}
s.head = 0
s.tail = currentTail
}
s.data[s.tail] = item
s.tail++
return nil
}
func (s *queue[T]) pop() *T {
if s.head == s.tail {
return nil
}
res := &s.data[s.head]
s.head++
return res
}
func (s *queue[T]) forEach() {
fmt.Printf("遍历队列:\n")
for i := s.head; i < s.tail; i++ {
fmt.Printf("当前队列元素%+v\n", s.data[i])
}
}
切片
package main
import (
"fmt"
)
func main() {
stack1 := createQueue[int](5)
err := stack1.push(1)
if err != nil {
return
}
stack1.push(2)
fmt.Printf("队列容量%+v\n", cap(stack1.data))
stack1.push(3)
stack1.push(4)
stack1.push(5)
popData1 := stack1.pop()
fmt.Printf("出队列元素%+v\n", *popData1)
popData2 := stack1.pop()
fmt.Printf("出队列元素%+v\n", *popData2)
stack1.forEach()
stack1.push(6)
stack1.push(7)
stack1.push(8)
stack1.push(9)
fmt.Printf("队列容量%+v\n", cap(stack1.data))
popData3 := stack1.pop()
fmt.Printf("出队列元素%+v\n", *popData3)
err = stack1.push(10)
if err != nil {
fmt.Printf(err.Error() + "\n")
}
stack1.forEach()
}
type queue[T int | string | map[string]string] struct {
data []T
}
func createQueue[T int | string | map[string]string](len int) *queue[T] {
return &queue[T]{
data: make([]T, 0, len),
}
}
func (s *queue[T]) push(item T) error {
s.data = append(s.data, item)
return nil
}
func (s *queue[T]) pop() *T {
if len(s.data) == 0 {
return nil
}
res := &s.data[0]
s.data = s.data[1:]
return res
}
func (s *queue[T]) forEach() {
fmt.Printf("遍历队列:\n")
for _, datum := range s.data {
fmt.Printf("当前队列元素%+v\n", datum)
}
}
链表
package main
import (
"errors"
"fmt"
)
func main() {
linkedObj := getLinked[int](5)
err := linkedObj.headPush(6)
if err != nil {
fmt.Printf(err.Error())
}
err = linkedObj.headPush(5)
if err != nil {
fmt.Printf(err.Error())
}
err = linkedObj.headPush(4)
if err != nil {
fmt.Printf(err.Error())
}
err = linkedObj.headPush(3)
if err != nil {
fmt.Printf(err.Error())
}
err = linkedObj.headPush(2)
if err != nil {
fmt.Printf(err.Error())
}
err = linkedObj.headPush(1)
if err != nil {
fmt.Printf(err.Error())
}
err = linkedObj.headPush(0)
if err != nil {
fmt.Printf(err.Error())
}
item := linkedObj.tailPop()
fmt.Printf("弹出节点: %+v\n", *item)
item = linkedObj.tailPop()
fmt.Printf("弹出节点: %+v\n", *item)
linkedObj.headForeach()
err = linkedObj.headPush(-1)
if err != nil {
fmt.Printf(err.Error())
}
linkedObj.tailForeach()
}
type linked[T int | string | map[string]string] struct {
head *node[T]
length int
limit int
}
type node[T int | string | map[string]string] struct {
data *T
next *node[T]
prev *node[T]
}
func getLinked[T int | string | map[string]string](limit int) *linked[T] {
return &linked[T]{
head: nil,
length: 0,
limit: limit,
}
}
func createNode[T int | string | map[string]string](data T) *node[T] {
return &node[T]{
data: &data,
next: nil,
prev: nil,
}
}
func (l *linked[T]) headPush(data T) error {
if l.length >= l.limit {
return errors.New("当前队满\n")
}
newNode := createNode(data)
if l.head == nil {
l.head = newNode
l.length++
newNode.next = newNode
newNode.prev = newNode
return nil
}
currentNode := l.head
headNodePos := l.head
l.head = newNode
newNode.next = currentNode
currentNode.prev = newNode
for {
if currentNode.next == headNodePos {
break
}
currentNode = currentNode.next
}
if l.length >= l.limit {
currentNode.prev.next = newNode
newNode.prev = currentNode.prev
} else {
currentNode.next = newNode
newNode.prev = currentNode
l.length++
}
return nil
}
func (l *linked[T]) tailPop() *T {
if l.head == nil {
return nil
}
currentNode := l.head
headNodePos := l.head
for {
if currentNode.next == headNodePos {
break
}
currentNode = currentNode.next
}
currentNode.prev.next = headNodePos
headNodePos.prev = currentNode.prev
l.length--
return currentNode.data
}
func (l *linked[T]) headForeach() {
headNode := l.head
headNodPos := headNode
fmt.Printf("从头结点遍历:\n")
for {
fmt.Printf("当前节点: %+v\n", *headNode.data)
if headNode.next == headNodPos {
break
}
headNode = headNode.next
}
}
func (l *linked[T]) tailForeach() {
endNode := l.head
endNodePos := endNode
fmt.Printf("从尾结点遍历:\n")
for {
fmt.Printf("当前节点: %+v\n", *endNode.prev.data)
if endNode.prev == endNodePos {
break
}
endNode = endNode.prev
}
}
链表加锁实现线程安全
package main
import (
"errors"
"fmt"
"sync"
)
func main() {
linkedObj := getLinked[int](5)
syncGroup := sync.WaitGroup{}
syncGroup.Add(1050)
for i := 0; i < 1000; i++ {
i := i
go func() {
err := linkedObj.headPush(i)
if err != nil {
fmt.Printf(err.Error())
}
syncGroup.Done()
}()
}
for i := 0; i < 50; i++ {
go func() {
data := linkedObj.tailPop()
if data != nil {
fmt.Println(*data)
}
syncGroup.Done()
}()
}
syncGroup.Wait()
linkedObj.headForeach()
}
type linked[T int | string | map[string]string] struct {
head *node[T]
length int
limit int
headLock sync.Mutex
tailLock sync.Mutex
}
type node[T int | string | map[string]string] struct {
data *T
next *node[T]
prev *node[T]
}
func getLinked[T int | string | map[string]string](limit int) *linked[T] {
return &linked[T]{
head: nil,
length: 0,
limit: limit,
headLock: sync.Mutex{},
tailLock: sync.Mutex{},
}
}
func createNode[T int | string | map[string]string](data T) *node[T] {
return &node[T]{
data: &data,
next: nil,
prev: nil,
}
}
func (l *linked[T]) headPush(data T) error {
l.headLock.Lock()
defer l.headLock.Unlock()
if l.length >= l.limit {
return errors.New("当前队满\n")
}
newNode := createNode(data)
if l.head == nil {
l.head = newNode
l.length++
newNode.next = newNode
newNode.prev = newNode
return nil
}
currentNode := l.head
headNodePos := l.head
l.head = newNode
newNode.next = currentNode
currentNode.prev = newNode
for {
if currentNode.next == headNodePos {
break
}
currentNode = currentNode.next
}
if l.length >= l.limit {
currentNode.prev.next = newNode
newNode.prev = currentNode.prev
} else {
currentNode.next = newNode
newNode.prev = currentNode
l.length++
}
return nil
}
func (l *linked[T]) tailPop() *T {
l.tailLock.Lock()
defer l.tailLock.Unlock()
if l.head == nil {
return nil
}
currentNode := l.head
headNodePos := l.head
for {
if currentNode.next == headNodePos {
break
}
currentNode = currentNode.next
}
if currentNode == headNodePos {
l.head = nil
} else {
currentNode.prev.next = headNodePos
headNodePos.prev = currentNode.prev
}
l.length--
return currentNode.data
}
func (l *linked[T]) headForeach() {
if l.head == nil {
fmt.Printf("队空:\n")
return
}
headNode := l.head
headNodPos := headNode
fmt.Printf("从头结点遍历:\n")
for {
fmt.Printf("当前节点: %+v\n", *headNode.data)
if headNode.next == headNodPos {
break
}
headNode = headNode.next
}
}
func (l *linked[T]) tailForeach() {
if l.head == nil {
fmt.Printf("队空:\n")
return
}
endNode := l.head
endNodePos := endNode
fmt.Printf("从尾结点遍历:\n")
for {
fmt.Printf("当前节点: %+v\n", *endNode.prev.data)
if endNode.prev == endNodePos {
break
}
endNode = endNode.prev
}
}
cas 实现 无锁队列