Skip to content

array queue, circular buffer, priority queue implemented using golang generics

License

Notifications You must be signed in to change notification settings

doraemonkeys/queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

arrayQueue、circularBuffer、priorityQueue

Go Reference Go Report Card

Features

  • The data structure implemented using generics
  • Higher performance
  • arrayQueue is scalable, automatic expanding like slice

Quick Start

priorityQueue

go get -u github.com/doraemonkeys/queue/priorityQueue
package main

import (
	"fmt"

	pq "github.com/doraemonkeys/queue/priorityQueue"
)

func main() {
	q := pq.New(func(a, b int) bool {
		return a < b
	})
	q.Push(1)
	q.Push(5)
	q.Push(3)

	q2 := q.ToTopK(q.Len())
	q2.Push(7)
	for !q2.IsEmpty() {
		fmt.Println(q2.Pop())
	}
	// Output:
	// 3
	// 5
	// 7
}

arrayQueue

go get -u github.com/doraemonkeys/queue/arrayQueue
package main

import (
	"fmt"

	aq "github.com/doraemonkeys/queue/arrayQueue"
)

func main() {
	que := aq.New[int]()
	que.Push(1)
	que.Push(2)
	que.Push(3)
	que.Pop()
	que.Push(99)
	fmt.Println(que.Front())
	fmt.Println(que.Back())
	it := que.Iterator()
	for it.Next() {
		fmt.Println(it.Value())
	}
	// Output:
	// 2
	// 99
	// 2
	// 3
	// 99
}

circularBuffer

go get -u github.com/doraemonkeys/queue/circularBuffer
package main

import (
	"fmt"

	cb "github.com/doraemonkeys/queue/circularBuffer"
)

func main() {
	c := cb.New[int](3)
	c.PushBack(1)
	c.PushBack(2)
	c.PushBack(3)
	// 1 2 3
	c.PushBack(4)
	// 2 3 4
	c.PushFront(5)
	// 5 2 3
	it := c.Iterator()
	for it.Next() {
		fmt.Println(it.Value())
	}
	// Output:
	// 5
	// 2
	// 3
}

About

array queue, circular buffer, priority queue implemented using golang generics

Topics

Resources

License

Stars

Watchers

Forks

Languages