arraystack

package
v0.0.0-...-bd0c551 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package arraystack implements a generic stack data structure using ArrayDeque as the underlying storage.

ArrayStack provides O(1) time complexity for push and pop operations, and efficient memory usage through the dynamic resizing capabilities of ArrayDeque. It supports all standard stack operations and provides iterators for element traversal.

Example:

// Create a new stack for integers
stack := arraystack.New[int]()

// Push elements onto the stack
stack.Push(5)
stack.Push(3)

// Check if the stack is empty
isEmpty := stack.IsEmpty() // false

// Get the number of elements
size := stack.Len() // 2

// Peek at the top element without removing it
top, found := stack.Peek() // 3, true

// Pop an element from the stack
element, found := stack.Pop() // 3, true

// Iterate through elements (LIFO order)
for val := range stack.Iter() {
	fmt.Println(val)
}

Package arraystack implements a generic stack data structure using ArrayDeque as the underlying storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayStack

type ArrayStack[T any] struct {
	// contains filtered or unexported fields
}

ArrayStack implements a stack data structure using ArrayDeque as the underlying storage T is the type of elements stored in the stack

Note: ArrayStack is not thread-safe. Additional synchronization mechanisms are required for concurrent access.

func New

func New[T any]() *ArrayStack[T]

New creates a new empty stack with the default initial capacity T is the type of elements to be stored in the stack

Returns:

A newly created ArrayStack instance

Time Complexity: O(1)

func (*ArrayStack[T]) Clear

func (s *ArrayStack[T]) Clear()

Clear removes all elements from the stack, leaving it empty

Time Complexity: O(n)

func (*ArrayStack[T]) Clone

func (s *ArrayStack[T]) Clone() *ArrayStack[T]

Clone creates a new stack that is a shallow copy of the original stack

Returns:

A new ArrayStack instance with the same elements as the original

Time Complexity: O(n)

func (*ArrayStack[T]) Extend

func (s *ArrayStack[T]) Extend(it iter.Seq[T])

Extend adds multiple elements to the top of the stack in the order they are provided

Parameters:

values: An [iter.Seq] of elements to push onto the stack

Time Complexity: O(n) where n is the number of elements to add, amortized

func (*ArrayStack[T]) IsEmpty

func (s *ArrayStack[T]) IsEmpty() bool

IsEmpty checks if the stack contains no elements

Returns:

true if the stack is empty, false otherwise

Time Complexity: O(1)

func (*ArrayStack[T]) Iter

func (s *ArrayStack[T]) Iter() iter.Seq[T]

Iter returns a sequential iter.Seq over the elements in LIFO (Last-In-First-Out) order

Returns:

An iterator that yields elements in LIFO order

Time Complexity: O(n) for full iteration

func (*ArrayStack[T]) IterMut

func (s *ArrayStack[T]) IterMut() iter.Seq[*T]

IterMut returns a mutable iter.Seq over the elements in LIFO (Last-In-First-Out) order

Returns:

An iterator that yields pointers to elements in LIFO order, allowing modification

Time Complexity: O(n) for full iteration

func (*ArrayStack[T]) Len

func (s *ArrayStack[T]) Len() int

Len returns the number of elements in the stack

Returns:

The number of elements in the stack

Time Complexity: O(1)

func (*ArrayStack[T]) Peek

func (s *ArrayStack[T]) Peek() (T, bool)

Peek returns the element at the top of the stack without removing it

Returns:

The top element and true if the stack is not empty
The zero value of type T and false if the stack is empty

Time Complexity: O(1)

func (*ArrayStack[T]) Pop

func (s *ArrayStack[T]) Pop() (T, bool)

Pop removes and returns the element at the top of the stack

Returns:

The removed element and true if the stack is not empty
The zero value of type T and false if the stack is empty

Time Complexity: O(1)

func (*ArrayStack[T]) Push

func (s *ArrayStack[T]) Push(value T)

Push adds an element to the top of the stack

Parameters:

value: The element value to push onto the stack

Time Complexity: O(1) amortized

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL