algorithm-go

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub today2098/algorithm-go

:heavy_check_mark: algorithm/stack.go

Depends on

Required by

Verified with

Code

//go:build go1.18

package algorithm

import "errors"

var ErrStackEmpty = errors.New("Stack: stack is empty")

type Stack[T any] struct {
	Data []T
}

func NewStack[T any]() *Stack[T] {
	return &Stack[T]{Data: []T{}}
}

func (s *Stack[T]) Empty() bool {
	return s.Size() == 0
}

func (s *Stack[T]) Size() int {
	return len(s.Data)
}

func (s *Stack[T]) Top() T {
	if s.Empty() {
		panic(ErrStackEmpty)
	}
	return s.Data[len(s.Data)-1]
}

func (s *Stack[T]) Push(x T) {
	s.Data = append(s.Data, x)
}

func (s *Stack[T]) PushRange(v []T) {
	for i := 0; i < len(v); i++ {
		s.Data = append(s.Data, v[i])
	}
}

func (s *Stack[T]) Pop() T {
	if s.Empty() {
		panic(ErrStackEmpty)
	}
	res := s.Data[len(s.Data)-1]
	s.Data = s.Data[:len(s.Data)-1]
	return res
}
Traceback (most recent call last):
  File "/home/runner/.local/lib/python3.10/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
  File "/home/runner/.local/lib/python3.10/site-packages/onlinejudge_verify/languages/user_defined.py", line 68, in bundle
    raise RuntimeError('bundler is not specified: {}'.format(str(path)))
RuntimeError: bundler is not specified: algorithm/stack.go
Back to top page