stack

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2025 License: MIT Imports: 2 Imported by: 0

README

Stack Package

This package provides two different stack implementations in Go: Array-based and LinkedList-based. All implementations are designed to be thread-safe and support generic types.

Features

Core Structures
  • Array Stack (Dynamic array-based implementation with generic type support)
  • LinkedList Stack (Linked list-based implementation with generic type support)
Common Operations
  • Push: Add element to the stack
  • Pop: Remove element from the stack
  • IsEmpty: Check if stack is empty
  • List: Get all elements
  • Print: Display elements
Thread Safety
  • Safe read/write operations with RWMutex
  • Concurrent access support for all structures
  • Deadlock prevention mechanisms
Generic Type Support
  • Support for any comparable type
  • Type-safe operations
  • Custom type support with proper comparison functions

Usage Examples

Array Stack
// Create a new integer stack
intStack := NewArrayStack[int]()

// Add elements
intStack.Push(1)
intStack.Push(2)
intStack.Push(3)

// Remove element from top
intStack.Pop()

// Check if empty
isEmpty := intStack.IsEmpty()

// List elements
elements := intStack.List()
intStack.Print()

// Create a new string stack
strStack := NewArrayStack[string]()

// Add elements
strStack.Push("a")
strStack.Push("b")
strStack.Push("c")

// Remove element from top
strStack.Pop()

// List elements
elements = strStack.List()
strStack.Print()

// Create a stack with custom type
type Person struct {
    Name string
    Age  int
}

personStack := NewArrayStack[Person]()
personStack.Push(Person{Name: "John", Age: 30})
personStack.Push(Person{Name: "Jane", Age: 25})
LinkedList Stack
// Create a new integer stack
intStack := NewLinkedListStack[int](0)

// Add elements
intStack.Push(1)
intStack.Push(2)
intStack.Push(3)

// Remove element from top
intStack.Pop()

// Check if empty
isEmpty := intStack.IsEmpty()

// List elements
elements := intStack.List()
intStack.Print()

// Create a new string stack
strStack := NewLinkedListStack[string]("")

// Add elements
strStack.Push("a")
strStack.Push("b")
strStack.Push("c")

// Remove element from top
strStack.Pop()

// List elements
elements = strStack.List()
strStack.Print()

// Create a stack with custom type
type Person struct {
    Name string
    Age  int
}

personStack := NewLinkedListStack[Person](Person{Name: "", Age: 0})
personStack.Push(Person{Name: "John", Age: 30})
personStack.Push(Person{Name: "Jane", Age: 25})

Implementation Details

Data Structures
Array Stack
  • Generic type support with comparable constraint
  • Dynamic array-based implementation
  • Auto-resizing capability (grows and shrinks)
  • Efficient memory management
  • Index tracking for top element
LinkedList Stack
  • Generic type support with comparable constraint
  • Node-based implementation
  • Dynamic memory allocation
  • LIFO (Last-In-First-Out) structure
  • No size limitations
Time Complexities
Array Stack
  • Push: O(1) amortized, O(n) worst case when resizing
  • Pop: O(1) amortized, O(n) worst case when shrinking
  • IsEmpty: O(1)
  • List: O(n)
  • Space: O(n)
LinkedList Stack
  • Push: O(1)
  • Pop: O(1)
  • IsEmpty: O(1)
  • List: O(n)
  • Space: O(n)
Memory Management
Array Stack
  • Dynamic array resizing (doubles when full)
  • Array shrinking (halves when 1/4 full)
  • Efficient memory utilization
  • Automatic capacity management
LinkedList Stack
  • Dynamic node allocation
  • No pre-allocated memory
  • Memory freed on pop
  • No explicit size limitations
Thread Safety Details
  • RLock for read operations (IsEmpty, List, Print)
  • Lock for write operations (Push, Pop)
  • Automatic unlock with defer
  • Safe design for concurrent access
Generic Type Constraints
  • Types must satisfy the comparable interface
  • Support for built-in types (int, string, etc.)
  • Support for custom types that implement comparable
  • Type safety at compile time

Testing

The package comes with comprehensive test coverage for various types. To run tests:

go test ./...

Contributing

Contributions are welcome! Please ensure that any new features or modifications come with:

  • Proper documentation
  • Thread safety considerations
  • Comprehensive test cases
  • Example usage
  • Performance analysis
  • Generic type support considerations

License

This package is distributed under the MIT license. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayStack

type ArrayStack[T any] struct {
	Arr     []T
	ArrSize int
	Index   int
	// contains filtered or unexported fields
}

ArrayStack represents a generic array-based stack

func NewArrayStack added in v1.2.0

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

NewArrayStack creates a new generic array-based stack

func (*ArrayStack[T]) IsEmpty added in v1.2.0

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

IsEmpty returns true if stack is empty

func (*ArrayStack[T]) List added in v1.2.0

func (arr *ArrayStack[T]) List() []T

List returns a slice of stack data

func (*ArrayStack[T]) Pop added in v1.2.0

func (arr *ArrayStack[T]) Pop()

Pop removes data from the stack

func (*ArrayStack[T]) Print added in v1.2.0

func (arr *ArrayStack[T]) Print()

Print displays stack data

func (*ArrayStack[T]) Push added in v1.2.0

func (arr *ArrayStack[T]) Push(data T)

Push adds data to the stack

type LinkedListStack

type LinkedListStack[T comparable] struct {
	X    T
	Next *LinkedListStack[T]
	// contains filtered or unexported fields
}

LinkedListStack represents a generic linked list-based stack

func NewLinkedListStack added in v1.2.0

func NewLinkedListStack[T comparable](data T) *LinkedListStack[T]

NewLinkedListStack creates a new generic linked list-based stack

func (*LinkedListStack[T]) IsEmpty added in v1.2.0

func (arr *LinkedListStack[T]) IsEmpty() bool

IsEmpty returns true if stack is empty

func (*LinkedListStack[T]) List added in v1.2.0

func (arr *LinkedListStack[T]) List() []T

List returns a slice of stack data

func (*LinkedListStack[T]) Pop added in v1.2.0

func (arr *LinkedListStack[T]) Pop()

Pop removes data from the beginning

func (*LinkedListStack[T]) Print added in v1.2.0

func (arr *LinkedListStack[T]) Print()

Print displays stack data

func (*LinkedListStack[T]) Push added in v1.2.0

func (arr *LinkedListStack[T]) Push(data T)

Push adds data at the beginning (LIFO)

Jump to

Keyboard shortcuts

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