github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/queues/linkedlistqueue/linkedlistqueue.go (about)

     1  // Package linkedlistqueue implements a queue backed by a singly-linked list.
     2  //
     3  // Structure is not thread safe.
     4  //
     5  // Reference: https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
     6  package linkedlistqueue
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/songzhibin97/go-baseutils/structure/lists/singlylinkedlist"
    13  	"github.com/songzhibin97/go-baseutils/structure/queues"
    14  )
    15  
    16  // Assert Queue implementation
    17  var _ queues.Queue[any] = (*Queue[any])(nil)
    18  
    19  // Queue holds elements in a singly-linked-list
    20  type Queue[E any] struct {
    21  	list *singlylinkedlist.List[E]
    22  }
    23  
    24  // New instantiates a new empty queue
    25  func New[E any]() *Queue[E] {
    26  	return &Queue[E]{list: &singlylinkedlist.List[E]{}}
    27  }
    28  
    29  // Enqueue adds a value to the end of the queue
    30  func (queue *Queue[E]) Enqueue(value E) {
    31  	queue.list.Add(value)
    32  }
    33  
    34  // Dequeue removes first element of the queue and returns it, or nil if queue is empty.
    35  // Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
    36  func (queue *Queue[E]) Dequeue() (value E, ok bool) {
    37  	value, ok = queue.list.Get(0)
    38  	if ok {
    39  		queue.list.Remove(0)
    40  	}
    41  	return
    42  }
    43  
    44  // Peek returns first element of the queue without removing it, or nil if queue is empty.
    45  // Second return parameter is true, unless the queue was empty and there was nothing to peek.
    46  func (queue *Queue[E]) Peek() (value E, ok bool) {
    47  	return queue.list.Get(0)
    48  }
    49  
    50  // Empty returns true if queue does not contain any elements.
    51  func (queue *Queue[E]) Empty() bool {
    52  	return queue.list.Empty()
    53  }
    54  
    55  // Size returns number of elements within the queue.
    56  func (queue *Queue[E]) Size() int {
    57  	return queue.list.Size()
    58  }
    59  
    60  // Clear removes all elements from the queue.
    61  func (queue *Queue[E]) Clear() {
    62  	queue.list.Clear()
    63  }
    64  
    65  // Values returns all elements in the queue (FIFO order).
    66  func (queue *Queue[E]) Values() []E {
    67  	return queue.list.Values()
    68  }
    69  
    70  // String returns a string representation of container
    71  func (queue *Queue[E]) String() string {
    72  	b := strings.Builder{}
    73  	b.WriteString("LinkedListQueue\n")
    74  	for index, value := range queue.Values() {
    75  		b.WriteString(fmt.Sprintf("(index:%d value:%v) ", index, value))
    76  	}
    77  	return b.String()
    78  }
    79  
    80  // Check that the index is within bounds of the list
    81  func (queue *Queue[E]) withinRange(index int) bool {
    82  	return index >= 0 && index < queue.list.Size()
    83  }
    84  
    85  // UnmarshalJSON @implements json.Unmarshaler
    86  func (queue *Queue[E]) UnmarshalJSON(bytes []byte) error {
    87  	return queue.list.UnmarshalJSON(bytes)
    88  }
    89  
    90  // MarshalJSON @implements json.Marshaler
    91  func (queue *Queue[E]) MarshalJSON() ([]byte, error) {
    92  	return queue.list.MarshalJSON()
    93  }