-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_queue.go
51 lines (42 loc) · 1.23 KB
/
linked_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package queue
import (
"github.com/golang-ds/linkedlist/singly"
)
type LinkedQueue[T any] struct {
data singly.LinkedList[T]
}
// New constructs and returns an empty linked-queue.
// time-complexity: O(1)
func New[T any]() LinkedQueue[T] {
return LinkedQueue[T]{}
}
// Enqueue adds an element to the end of the queue.
// time-complexity: O(1)
func (q *LinkedQueue[T]) Enqueue(data T) {
q.data.AddLast(data)
}
// Dequeue removes and returns the front element of the queue. It returns false if the queue was empty.
// time-complexity: O(1)
func (q *LinkedQueue[T]) Dequeue() (val T, ok bool) {
return q.data.RemoveFirst()
}
// First returns the front element of the queue. It returns false if the queue was empty.
// time-complexity: O(1)
func (q *LinkedQueue[T]) First() (val T, ok bool) {
return q.data.First()
}
// Size returns the number of the elements in the queue.
// time-complexity: O(1)
func (q *LinkedQueue[T]) Size() int {
return q.data.Size
}
// IsEmpty returns true if the queue is empty.
// time-complexity: O(1)
func (q *LinkedQueue[T]) IsEmpty() bool {
return q.data.IsEmpty()
}
// String returns the string representation of the queue.
// time-complexity: O(n)
func (q *LinkedQueue[T]) String() string {
return q.data.String()
}