linkedlist

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

LinkedList Package

This package provides three different linked list implementations in Go: Linear (Singly), Double (Doubly), and Circular. All implementations are designed to be thread-safe and support generic types.

Features

Core Structures
  • Linear (Singly) Linked List
  • Double (Doubly) Linked List
  • Circular Linked List
Generic Type Support
  • Support for all data types ([T any])
  • Custom comparison functions
    • less function: For sorting operations
    • equals function: For equality checks
Common Operations
  • AddToStart: Add element to the beginning
  • AddToSequentially: Add element in sorted order
  • AddToAfter: Add element after a specific value
  • AddToEnd: Add element to the end
  • Delete: Remove element
  • Search: Find element
  • List: Get all elements
  • Print: Display elements
Thread Safety
  • Safe read/write operations with RWMutex
  • Concurrent access support for all structures
  • Deadlock prevention mechanisms

Usage Examples

Using with Integer Type
// Linear List example
intList := linkedlist.NewLinear[int](10)
intList.AddToEnd(20)
intList.AddToEnd(30)

// Comparison functions
intLess := func(a, b int) bool { return a < b }
intEquals := func(a, b int) bool { return a == b }

// Sequential adding and searching
intList.AddToSequentially(15, intLess)
exists := intList.Search(20, intEquals)
Using with String Type
// Double List example
strList := linkedlist.NewDouble[string]("Hello")
strList.AddToEnd("World")

// Comparison functions
strLess := func(a, b string) bool { return strings.Compare(a, b) < 0 }
strEquals := func(a, b string) bool { return a == b }

// Sequential adding
strList.AddToSequentially("Go", strLess)

// Forward and backward listing
strList.Print(false)  // Forward direction
strList.Print(true)   // Backward direction
Using with Custom Struct
// Custom struct definition
type Person struct {
    Name string
    Age  int
}

// Circular List example
personList := linkedlist.NewCircular[Person](Person{Name: "John", Age: 25})

// Comparison functions
personLess := func(a, b Person) bool { return a.Age < b.Age }
personEquals := func(a, b Person) bool { 
    return a.Name == b.Name && a.Age == b.Age 
}

// Adding and removing elements
personList.AddToEnd(Person{Name: "Jane", Age: 30})
personList.AddToSequentially(Person{Name: "Bob", Age: 28}, personLess)
personList.Delete(Person{Name: "John", Age: 25}, personEquals)

Implementation Details

Data Structures
  • Linear: Single next pointer
  • Double: Both next and prev pointers
  • Circular: Last element connected to first with next pointer
Time Complexities
Linear and Double List
  • AddToStart: O(1)
  • AddToEnd: O(n)
  • AddToSequentially: O(n)
  • AddToAfter: O(n)
  • Delete: O(n)
  • Search: O(n)
  • List: O(n)
Circular List
  • AddToStart: O(1)
  • AddToEnd: O(n)
  • AddToSequentially: O(n)
  • AddToAfter: O(n)
  • Delete: O(n)
  • List: O(n)
Thread Safety Details
  • RLock for read operations
  • Lock for write operations
  • Automatic unlock with defer
  • Safe design for concurrent access
Memory Management
  • Dynamic node creation and deletion
  • Pointer management
  • Circular reference prevention
  • Memory leak prevention mechanisms

Testing

The package comes with comprehensive test coverage. 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

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 Circular

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

Circular represents a generic circular linked list

func NewCircular added in v1.2.0

func NewCircular[T any](data T) *Circular[T]

NewCircular creates a new generic circular linked list node

func (*Circular[T]) AddToAfter added in v1.2.0

func (node *Circular[T]) AddToAfter(data T, which T, equals func(T, T) bool)

AddToAfter adds data after the specified value

func (*Circular[T]) AddToEnd added in v1.2.0

func (node *Circular[T]) AddToEnd(data T)

AddToEnd adds data at the end of the list

func (*Circular[T]) AddToSequentially added in v1.2.0

func (node *Circular[T]) AddToSequentially(data T, less func(T, T) bool)

AddToSequentially adds data in sorted order

func (*Circular[T]) AddToStart added in v1.2.0

func (node *Circular[T]) AddToStart(data T)

AddToStart adds data at the beginning of the list

func (*Circular[T]) Delete added in v1.2.0

func (node *Circular[T]) Delete(data T, equals func(T, T) bool) error

Delete removes data from the list

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

func (node *Circular[T]) List() []T

List returns a slice of list data

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

func (node *Circular[T]) Print()

Print displays list data

type Double

type Double[T any] struct {
	X    T
	Next *Double[T]
	Prev *Double[T]
	// contains filtered or unexported fields
}

Double represents a generic double linked list

func NewDouble added in v1.2.0

func NewDouble[T any](data T) *Double[T]

NewDouble creates a new generic double linked list node

func (*Double[T]) AddToAfter added in v1.2.0

func (node *Double[T]) AddToAfter(data T, which T, equals func(T, T) bool)

AddToAfter adds data after the specified value

func (*Double[T]) AddToEnd added in v1.2.0

func (node *Double[T]) AddToEnd(data T)

AddToEnd adds data at the end of the list

func (*Double[T]) AddToSequentially added in v1.2.0

func (node *Double[T]) AddToSequentially(data T, less func(T, T) bool)

AddToSequentially adds data in sorted order

func (*Double[T]) AddToStart added in v1.2.0

func (node *Double[T]) AddToStart(data T)

AddToStart adds data at the beginning of the list

func (*Double[T]) Delete added in v1.2.0

func (node *Double[T]) Delete(data T, equals func(T, T) bool) error

Delete removes data from the list

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

func (node *Double[T]) List(reverse bool) []T

List returns a slice of list data

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

func (node *Double[T]) Print(reverse bool)

Print displays list data

type Linear

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

Linear represents a generic linear linked list

func NewLinear added in v1.2.0

func NewLinear[T any](data T) *Linear[T]

NewLinear creates a new generic linear linked list node

func (*Linear[T]) AddToAfter added in v1.2.0

func (node *Linear[T]) AddToAfter(data T, which T, equals func(T, T) bool) error

AddToAfter adds data after the specified value

func (*Linear[T]) AddToEnd added in v1.2.0

func (node *Linear[T]) AddToEnd(data T)

AddToEnd adds data at the end of the list

func (*Linear[T]) AddToSequentially added in v1.2.0

func (node *Linear[T]) AddToSequentially(data T, less func(T, T) bool)

AddToSequentially adds data in sorted order

func (*Linear[T]) AddToStart added in v1.2.0

func (node *Linear[T]) AddToStart(data T)

AddToStart adds data at the beginning of the list

func (*Linear[T]) Delete added in v1.2.0

func (node *Linear[T]) Delete(data T, equals func(T, T) bool) error

Delete removes data from the list

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

func (node *Linear[T]) List() []T

List returns a slice of list data

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

func (node *Linear[T]) Print()

Print displays list data

func (*Linear[T]) Search added in v1.2.0

func (node *Linear[T]) Search(data T, equals func(T, T) bool) bool

Search looks for data in the list

Jump to

Keyboard shortcuts

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