Documentation ¶
Overview ¶
Package list is a simple implementation of the linked list data structure.
Example ¶
package main import ( "fmt" "github.com/eng618/go-eng/ds/list" ) func main() { list := list.New() list.PushBack(20) list.PushBack(30) list.PushBack(40) list.PushBack(50) list.PushBack(70) fmt.Println("Length =", list.Length()) list.Display() if err := list.Delete(2); err != nil { fmt.Println(err) } list.Reverse() fmt.Println("Length =", list.Length()) list.Display() front, _ := list.PeekFront() back, _ := list.PeekBack() fmt.Println("Front =", front) fmt.Println("Back =", back) }
Output: Length = 5 20 -> 30 -> 40 -> 50 -> 70 -> Length = 4 70 -> 50 -> 30 -> 20 -> Front = 70 Back = 20
Index ¶
- type LinkedList
- func (l *LinkedList) Delete(position int) error
- func (l *LinkedList) Display()
- func (l *LinkedList) Head() interface{}
- func (l *LinkedList) Length() int
- func (l *LinkedList) PeekBack() (interface{}, error)
- func (l *LinkedList) PeekFront() (interface{}, error)
- func (l *LinkedList) PushBack(v interface{})
- func (l *LinkedList) PushFront(data interface{})
- func (l *LinkedList) Remove(data interface{}) error
- func (l *LinkedList) Reverse()
- func (l *LinkedList) Tail() interface{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LinkedList ¶
type LinkedList struct {
// contains filtered or unexported fields
}
LinkedList is the structure of a linked list.
func NewSeeded ¶ added in v0.5.0
func NewSeeded(seed interface{}) LinkedList
NewSeeded creates and returns a seeded LinkedList.
func (*LinkedList) Delete ¶
func (l *LinkedList) Delete(position int) error
Delete removes the node with the provided position from a linkedlist.
func (*LinkedList) Display ¶
func (l *LinkedList) Display()
Display is a helper to print a visual representation of the linked list to the console.
func (*LinkedList) Head ¶ added in v0.5.0
func (l *LinkedList) Head() interface{}
Head returns the value of the current head.
func (*LinkedList) Length ¶ added in v0.5.0
func (l *LinkedList) Length() int
Length returns the Length of the provided LinkedList.
func (*LinkedList) PeekBack ¶ added in v0.4.2
func (l *LinkedList) PeekBack() (interface{}, error)
PeekBack retrieves, but does not delete the last node of a linked list.
func (*LinkedList) PeekFront ¶ added in v0.4.2
func (l *LinkedList) PeekFront() (interface{}, error)
PeekFront retrieves, but does not delete the first node of a linked list.
func (*LinkedList) PushBack ¶
func (l *LinkedList) PushBack(v interface{})
PushBack adds the supplied node to the end of a LinkedList.
func (*LinkedList) PushFront ¶
func (l *LinkedList) PushFront(data interface{})
PushFront adds the supplied node to the beginning of a LinkedList.
func (*LinkedList) Remove ¶ added in v0.4.2
func (l *LinkedList) Remove(data interface{}) error
Remove will removed the first occurrence of the provided data from the list, if present.
func (*LinkedList) Reverse ¶
func (l *LinkedList) Reverse()
Reverse takes the linked list and reverses all off the values within it.
func (*LinkedList) Tail ¶ added in v0.5.0
func (l *LinkedList) Tail() interface{}
Tail returns the value of the current tail.