all repos

scratch @ 17b595b13a6b7913b0e606ccb699e368f104968c

⭐ me doing recreational ~~drugs~~ programming
3 files changed, 94 insertions(+), 0 deletions(-)
algos: куеуе
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-03-28 17:16:22 +0200
Authored at: 2026-03-28 16:50:17 +0200
Change ID: xvxorwkkousnpkotrqkzytuxsulsynkx
Parent: efb5ed1
A algos/go.mod
···
        
        1
        +module algos

      
        
        2
        +

      
        
        3
        +go 1.26.1

      
A algos/queue.go
···
        
        1
        +package algos

      
        
        2
        +

      
        
        3
        +type QNode[T any] struct {

      
        
        4
        +	v    T

      
        
        5
        +	next *QNode[T]

      
        
        6
        +}

      
        
        7
        +

      
        
        8
        +type Queue[T any] struct {

      
        
        9
        +	Len  int

      
        
        10
        +	head *QNode[T]

      
        
        11
        +	tail *QNode[T]

      
        
        12
        +}

      
        
        13
        +

      
        
        14
        +func (q *Queue[T]) Push(item T) {

      
        
        15
        +	node := &QNode[T]{v: item}

      
        
        16
        +	if q.Len == 0 {

      
        
        17
        +		q.head = node

      
        
        18
        +		q.tail = node

      
        
        19
        +	} else {

      
        
        20
        +		q.tail.next = node

      
        
        21
        +		q.tail = node

      
        
        22
        +	}

      
        
        23
        +	q.Len++

      
        
        24
        +}

      
        
        25
        +

      
        
        26
        +func (q *Queue[T]) Peek() T {

      
        
        27
        +	return q.head.v

      
        
        28
        +}

      
        
        29
        +

      
        
        30
        +func (q *Queue[T]) Pop() (T, bool) {

      
        
        31
        +	if q.Len == 0 {

      
        
        32
        +		var t T

      
        
        33
        +		return t, false

      
        
        34
        +	}

      
        
        35
        +

      
        
        36
        +	res := q.head.v

      
        
        37
        +	q.head = q.head.next

      
        
        38
        +	if q.head == nil {

      
        
        39
        +		q.tail = nil

      
        
        40
        +	}

      
        
        41
        +	q.Len--

      
        
        42
        +	return res, true

      
        
        43
        +}

      
A algos/queue_test.go
···
        
        1
        +package algos

      
        
        2
        +

      
        
        3
        +import "testing"

      
        
        4
        +

      
        
        5
        +func TestQueue(t *testing.T) {

      
        
        6
        +	q := Queue[int]{}

      
        
        7
        +

      
        
        8
        +	q.Push(5)

      
        
        9
        +	q.Push(7)

      
        
        10
        +	q.Push(9)

      
        
        11
        +

      
        
        12
        +	v, ok := q.Pop()

      
        
        13
        +	is(t, ok, true)

      
        
        14
        +	is(t, v, 5)

      
        
        15
        +

      
        
        16
        +	is(t, q.Len, 2)

      
        
        17
        +

      
        
        18
        +	q.Push(11)

      
        
        19
        +

      
        
        20
        +	v, ok = q.Pop()

      
        
        21
        +	is(t, ok, true)

      
        
        22
        +	is(t, v, 7)

      
        
        23
        +

      
        
        24
        +	v, ok = q.Pop()

      
        
        25
        +	is(t, ok, true)

      
        
        26
        +	is(t, v, 9)

      
        
        27
        +

      
        
        28
        +	is(t, q.Peek(), 11)

      
        
        29
        +

      
        
        30
        +	v, ok = q.Pop()

      
        
        31
        +	is(t, ok, true)

      
        
        32
        +	is(t, v, 11)

      
        
        33
        +

      
        
        34
        +	// everything works after removing all elements

      
        
        35
        +	q.Push(69420)

      
        
        36
        +	is(t, q.Peek(), 69420)

      
        
        37
        +

      
        
        38
        +	v, ok = q.Pop()

      
        
        39
        +	is(t, ok, true)

      
        
        40
        +	is(t, v, 69420)

      
        
        41
        +}

      
        
        42
        +

      
        
        43
        +func is[T comparable](tb testing.TB, a, b T) {

      
        
        44
        +	tb.Helper()

      
        
        45
        +	if a != b {

      
        
        46
        +		tb.Fatalf("%+v and %+v are not equal\n", a, b)

      
        
        47
        +	}

      
        
        48
        +}