Skip to content

hiennguyen-neih/go-linkedlist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-linkedlist

License Go Version Go Reference

Linked list library for Go programming language (golang).

Install

go get github.com/hiennguyen-neih/go-linkedlist

GoList (singly linked-list)

Import

import "github.com/hiennguyen-neih/go-linkedlist/golist"

Documentation

Go Reference

go doc -all github.com/hiennguyen-neih/go-linkedlist/golist

Usage

Declare variables

list1 := golist.GoList[int]{}
list2 := golist.New("a", "b", "c", "d", "e")
list3 := golist.FromSlice([]int{1, 2, 3, 4, 5})

For loop

list := golist.New("a", "b", "c", "d", "e")
for node := list.Head; node != nil; node = node.Next {
    // do somethings with node.Data
    fmt.Println(node.Data)
}

Example

list := golist.New("c", "d", "e")
list = golist.AppendHead(list, "a", "b")
list = golist.Join(list, "x")
list = golist.Map(list, func(s string) string {
    return strings.ToUpper(s)
})
fmt.Println(list) // ["A"->"X"->"B"->"X"->"C"->"X"->"D"->"X"->"E"]

GoList2 (doubly linked-list)

Import

import "github.com/hiennguyen-neih/go-linkedlist/golist2"

Documentation

Go Reference

go doc -all github.com/hiennguyen-neih/go-linkedlist/golist2

Usage

Declare variables

list1 := golist2.GoList2[int]{}
list2 := golist2.New("a", "b", "c", "d", "e")
list3 := golist2.FromSlice([]int{1, 2, 3, 4, 5})

For loop

list := golist2.New(1.0, 2.1, 3.2, 4.3, 5.4)
for node := list.Head; node != nil; node = node.Next {
    // do somethings with node.Data
    fmt.Println(node.Data)
}

Example

list2 := golist2.New(1, 2, 3)
list2 = golist2.Append(list2, 4, 5, 6)
list2 = golist2.FilterMap(list2, func(n int) (bool, int) {
    return n % 2 == 0, n * 2
})
list2 = golist2.Reverse(list2)
fmt.Println(list2)  // [12<->8<->4]