list

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2021 License: MIT Imports: 2 Imported by: 0

README

Linked List Data Structure

Some good code optimizations for linked lists are to have head AND tail pointers. This will allow you to add items to the end of the list in O(1) constant time rather than O(n) linear time. Another best practice to help traverse the list is to add a previous pointer to each node of the linked list.

I started with this code source and expanded on it to test and document the code using Go's best practices. Also while writing tests, I found a bug in the code that it was not properly deleting the final element of a list.

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

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 New added in v0.4.2

func New() LinkedList

New generates and returns a new empty LinkedList.

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.

Jump to

Keyboard shortcuts

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