import "linkedlist"
Package linkedlist implements a singly linked list.
type Element
Element holds data and a pointer to the next Element.
type Element struct {
// contains filtered or unexported fields
}
type List
List holds the 1st element of the list and the size of the list.
type List struct {
// contains filtered or unexported fields
}
Example (Array)
{
s1 := []int{0, 1, 2, 3, 4}
list := New(s1)
s2 := list.Array()
fmt.Printf("s1: %#v\n", s1)
fmt.Printf("s2: %#v\n", s2)
}
s1: []int{0, 1, 2, 3, 4}
s2: []int{0, 1, 2, 3, 4}
Example (New)
{
slice := []int{0, 1, 2, 3, 4}
list := New(slice)
fmt.Printf("slice: %#v\n", slice)
fmt.Printf("list.head.data: %v\n", list.head.data)
fmt.Printf("list.tail.data: %v\n", list.tail.data)
fmt.Printf("list.size: %d\n", list.size)
}
slice: []int{0, 1, 2, 3, 4}
list.head.data: 0
list.tail.data: 4
list.size: 5
Example (Next)
{
slice := []int{0, 1, 2, 3, 4}
list := New(slice)
fmt.Printf("slice: %#v\n", slice)
n := list.Next()
for n != nil {
d := n.data
fmt.Printf("next: %d\n", d)
n = list.Next()
}
}
slice: []int{0, 1, 2, 3, 4}
next: 0
next: 1
next: 2
next: 3
next: 4
Example (Pop)
{
slice := []int{0, 1, 2, 3, 4}
list := New(slice)
fmt.Printf("slice: %#v\n", slice)
fmt.Printf("before: %d\n", list.tail.data)
d, e := list.Pop()
fmt.Printf("popped: %d, %v\n", d, e)
fmt.Printf("after: %d\n", list.tail.data)
d, e = list.Pop()
fmt.Printf("popped: %d, %v\n", d, e)
fmt.Printf("after: %d\n", list.tail.data)
d, e = list.Pop()
fmt.Printf("popped: %d, %v\n", d, e)
fmt.Printf("after: %d\n", list.tail.data)
d, e = list.Pop()
fmt.Printf("popped: %d, %v\n", d, e)
fmt.Printf("after: %d\n", list.tail.data)
d, e = list.Pop()
fmt.Printf("popped: %d, %v\n", d, e)
}
slice: []int{0, 1, 2, 3, 4}
before: 4
popped: 4, <nil>
after: 3
popped: 3, <nil>
after: 2
popped: 2, <nil>
after: 1
popped: 1, <nil>
after: 0
popped: 0, <nil>
Example (Push)
{
l1 := List{
head: nil,
tail: nil,
curr: nil,
size: 0,
}
fmt.Printf("(%d) %v, %v\n", l1.Size(), l1.head, l1.tail)
l1.Push(0)
fmt.Printf("(%d) %d, %d\n", l1.Size(), l1.head.data, l1.tail.data)
l1.Push(1)
fmt.Printf("(%d) %d, %d\n", l1.Size(), l1.head.next.data, l1.tail.data)
l1.Push(2)
fmt.Printf("(%d) %d, %d\n", l1.Size(), l1.head.next.next.data, l1.tail.data)
l1.Push(3)
fmt.Printf("(%d) %d, %d\n", l1.Size(), l1.head.next.next.next.data, l1.tail.data)
l1.Push(4)
fmt.Printf("(%d) %d, %d\n", l1.Size(), l1.head.next.next.next.next.data, l1.tail.data)
}
(0) <nil>, <nil>
(1) 0, 0
(2) 1, 1
(3) 2, 2
(4) 3, 3
(5) 4, 4
Example (Reverse)
{
s1 := []int{0, 1, 2, 3, 4}
l1 := New(s1)
l2 := l1.Reverse()
s2 := l2.Array()
fmt.Printf("s1: %#v\n", s1)
fmt.Printf("s2: %#v\n", s2)
}
s1: []int{0, 1, 2, 3, 4}
s2: []int{4, 3, 2, 1, 0}
Example (Size)
{
slice := []int{0, 1, 2, 3, 4}
list := New(slice)
fmt.Printf("slice len: %d\n", len(slice))
fmt.Printf(" list len: %d\n", list.Size())
}
slice len: 5
list len: 5
func New
func New(slice []int) *List
New returns a new list that is populated using the passed slice/array.
func (*List) Array
func (l *List) Array() []int
Array returns the List as a slice.
func (*List) Next
func (l *List) Next() *Element
Next returns a pointer to the next item in the List.
func (*List) Pop
func (l *List) Pop() (int, error)
Pop returns an interger and an error code from the last element of the List while also removing it.
func (*List) Push
func (l *List) Push(data int)
Push add a new number to the end of the List.
func (*List) Reset
func (l *List) Reset()
Reset resets a List to it's zero value.
func (*List) Reverse
func (l *List) Reverse() *List
Reverse returns a new List in reversed order.
func (*List) Size
func (l *List) Size() int
Size returns the size of the list.
Generated by gomarkdoc