singly_linkedlist

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

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

A singly linked list iterator that traverse elements by chaining order.

func (*Iterator[T]) Get

func (i *Iterator[T]) Get() T

Return the value.

Example
list := New[int]()
list.PushFront(5)
list.PushFront(3)
iter := list.Begin()
fmt.Println(iter.Get())
Output:

3

func (*Iterator[T]) HasNext

func (i *Iterator[T]) HasNext() bool

Return true if can advance.

Example
list := New[int]()
list.PushFront(5)
list.PushFront(3)
iterator := list.Begin()
fmt.Println(iterator.HasNext())
iterator.Next()
fmt.Println(iterator.HasNext())
iterator.Next()
fmt.Println(iterator.HasNext())
Output:

true
true
false

func (*Iterator[T]) Next

func (i *Iterator[T]) Next()

Advance the iterator.

Example
list := New[int]()
list.PushFront(5)
list.PushFront(3)
iterator := list.Begin()
iterator.Next()
fmt.Println(iterator.HasNext())
Output:

true

func (*Iterator[T]) Set added in v0.1.1

func (i *Iterator[T]) Set(value T)

Set the value.

Example
list := New[int]()
list.PushFront(5)
list.PushFront(3)
iter := list.Begin()
iter.Set(10)
fmt.Println(iter.Get())
Output:

10

type SinglyLinkedList

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

A singly linked list that supports traversing forward only.

func New added in v0.2.0

func New[T any]() SinglyLinkedList[T]

func (*SinglyLinkedList[T]) Begin

func (s *SinglyLinkedList[T]) Begin() Iterator[T]

Return an iterator points to the first element.

func (*SinglyLinkedList[T]) Insert

func (s *SinglyLinkedList[T]) Insert(i Iterator[T], e T)

Insert e before i.

Example
list := New[int]()
iterator := list.Begin()
list.Insert(iterator, 10)
fmt.Println(list.Len())
Output:

1

func (*SinglyLinkedList[T]) Len

func (d *SinglyLinkedList[T]) Len() int

Return the number of element.

func (*SinglyLinkedList[T]) PopFront

func (s *SinglyLinkedList[T]) PopFront()

Remove the first element.

Example
list := New[int]()
list.PushFront(42)
list.PopFront()
fmt.Println(list.Len())
Output:

0

func (*SinglyLinkedList[T]) PushFront

func (s *SinglyLinkedList[T]) PushFront(e T)

Prepend e to the list.

Example
list := New[int]()
list.PushFront(42)
fmt.Println(list.Len())
Output:

1

func (*SinglyLinkedList[T]) Remove

func (s *SinglyLinkedList[T]) Remove(i Iterator[T])

Remove the element at i.

Example
list := New[int]()
iterator := list.Begin()
list.Insert(iterator, 10)
fmt.Println(list.Len())
list.Remove(iterator)
fmt.Println(list.Len())
Output:

1
0

Jump to

Keyboard shortcuts

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