diff --git a/docs/datastructure/queue.md b/docs/datastructure/queue.md new file mode 100644 index 0000000..76ddea7 --- /dev/null +++ b/docs/datastructure/queue.md @@ -0,0 +1,364 @@ +# Queue +A queue is a kind of linear table. It only allows delete operations at the front of the table and insert operations at the rear of the table. This package includes ArrayQueue, LinkedQueue, CircularQueue, and PriorityQueue. + +
+ +## Source + +- [https://github.com/duke-git/lancet/blob/main/datastructure/queue/arrayqueue.go](https://github.com/duke-git/lancet/blob/main/datastructure/queue/arrayqueue.go) +- [https://github.com/duke-git/lancet/blob/main/datastructure/queue/linkedqueue.go](https://github.com/duke-git/lancet/blob/main/datastructure/queue/linkedqueue.go) +- [https://github.com/duke-git/lancet/blob/main/datastructure/queue/circularqueue.go](https://github.com/duke-git/lancet/blob/main/datastructure/queue/circularqueue.go) +- [https://github.com/duke-git/lancet/blob/main/datastructure/queue/priorityqueue.go](https://github.com/duke-git/lancet/blob/main/datastructure/queue/priorityqueue.go) + +
+ +## Usage +```go +import ( + stack "github.com/duke-git/lancet/v2/datastructure/stack" +) +``` + +
+ +## Index + +### 1. ArrayQueue +- [NewArrayQueue](#NewArrayQueue) +- [Enqueue](#ArrayQueue_Enqueue) +- [Dequeue](#ArrayQueue_Dequeue) +- [Front](#ArrayQueue_Front) +- [Back](#ArrayQueue_Back) +- [Front](#ArrayQueue_Size) +- [IsEmpty](#ArrayQueue_IsEmpty) +- [Clear](#ArrayQueue_Clear) +- [Contain](#ArrayQueue_Contain) + + + +### 2. LinkedStack + + + +
+ +## Documentation + +### 1. ArrayQueue +Common queue implemented by slice. + +### NewArrayQueue +

Return a ArrayQueue pointer with specific capacity

+ +Signature: + +```go +func NewArrayQueue[T any](capacity int) *ArrayQueue[T] + +type ArrayQueue[T any] struct { + items []T + head int + tail int + capacity int + size int +} +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + fmt.Println(q.Data()) // [] +} +``` + + + +### Data +

Get all queue data

+ +Signature: + +```go +func (q *ArrayQueue[T]) Data() []T +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + fmt.Println(q.Data()) // [] +} +``` + + + + +### Enqueue +

Put element into queue, if queue is full, return false

+ +Signature: + +```go +func (q *ArrayQueue[T]) Enqueue(item T) bool +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.Data()) // 1,2,3 +} +``` + + + + +### Dequeue +

Remove head element of queue and return it

+ +Signature: + +```go +func (q *ArrayQueue[T]) Dequeue() (T, bool) +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.Dequeue()) // 1 + fmt.Println(q.Data()) // 2,3 +} +``` + + + + +### Front +

Just get head element of queue

+ +Signature: + +```go +func (q *ArrayQueue[T]) Front() T +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.Front()) // 1 + fmt.Println(q.Data()) // 1,2,3 +} +``` + + + + +### Back +

Just get tail element of queue

+ +Signature: + +```go +func (q *ArrayQueue[T]) Back() T +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.Back()) // 3 + fmt.Println(q.Data()) // 1,2,3 +} +``` + + + +### Size +

Get the number of elements in queue

+ +Signature: + +```go +func (q *ArrayQueue[T]) Size() int +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.Size()) // 3 +} +``` + + + +### IsEmpty +

Check if queue is empty or not

+ +Signature: + +```go +func (q *ArrayQueue[T]) IsEmpty() bool +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + fmt.Println(q.IsEmpty()) // true + + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.IsEmpty()) // false +} +``` + + + + +### Clear +

Clean all data in queue

+ +Signature: + +```go +func (q *ArrayQueue[T]) Clear() +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + q.Clear() + + fmt.Println(q.IsEmpty()) // true +} +``` + + + +### Contain +

checks if the value is in queue or not

+ +Signature: + +```go +func (q *ArrayQueue[T]) Contain(value T) bool +``` +Example: + +```go +package main + +import ( + "fmt" + queue "github.com/duke-git/lancet/v2/datastructure/queue" +) + +func main() { + q := queue.NewArrayQueue[int](5) + q.Enqueue(1) + q.Enqueue(2) + q.Enqueue(3) + + fmt.Println(q.Contain(1)) // true + fmt.Println(q.Contain(4)) // false +} +``` + + + + + +