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 ¶
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
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
Click to show internal directories.
Click to hide internal directories.