sll

package
v0.0.0-...-d12f04e Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 1 Imported by: 0

README

Singly Linked List (sll)

Contents

Functions
InitList
func InitList(payload interface{}) *Sll

Initialize a singly linked list, accepts a payload of any type for the head node, returns a pointer to the list.

Methods
Append
func (list *Sll) Append(payload interface{}) *Node

Append a paylod in the list as a node, returns a pointer to the newly created node

Time complexity: O(1)



Prepend
func (list *Sll) Prepend(payload interface{}) *Node

Prepend a payload in the list as a node (i.e. change the head node), returns a pointer to the newly created node.

Time complexity: O(1)



RemoveByIndex
func (list *Sll) RemoveByIndex(index int) (interface{}, bool)

Remove a node at a spcified index, returns the payload of the node removed and an ok value.

0 <= index <= n

Where n is the length of the list.

Average Time complexity: O(index)



RemoveByNode
func (list *Sll) RemoveByNode(nodeToBeRemoved *Node) (interface{}, bool)

Remove a node by its reference, returns the payload of the node removed and an ok value.

Average time complexity: O(n)

Where n is the length of the list.



ReplaceByIndex
func (list *Sll) ReplaceByIndex(index int, payload interface{}) (interface{}, *Node, bool)

Replace a node at the specified with a new node created with the supplied payload. Returns the payload of the old node, a pointer to the new node and an ok value.

0 <= index <= n

Where n is the length of the list.

Average time complexity: O(index)



ReplaceByNode
func (list *Sll) ReplaceByNode(nodeToBeReplaced *Node, payload interface{}) (interface{}, *Node, bool)

Replace a node specified with a pointer with a new node created with the supplied payload. Returns the payload of the old node, a pointer to the new node and an ok value

Average time complexity: O(n)

Where n is the length of the list.



InsertAt
func (list *Sll) InsertAt(index int, payload interface{}) (*Node, bool)

Insert a new node created with the supplied payload at a specific index. Any exisiting node at that index become the next node of the newly created node. Returns a pointer to the newly created node and an ok value.

0 <= index <= n

Average time complexity: O(index)

Where n is the length of the list.



InsertAfter
func (list *Sll) InsertAfter(referenceNode *Node, payload interface{}) (*Node, bool)

Insert a new node created with the supplied payload after a specific node. Returns a pointer to newly inserted node and an ok value.

Time complexity: O(1)



InsertBefore
func (list *Sll) InsertBefore(referenceNode *Node, payload interface{}) (*Node, bool)

Insert a new node created with the supplied payload before a specific node. Returns a pointer to newly inserted node and an ok value.

Average time complexity: O(n)

Where n is the length of the list.



Get
func (list *Sll) Get(index int) (*Node, bool)

Get the node reference (pointer to the node) at a specific index. Returns a pointer to the node and an ok value.

Average time complexity: O(n)

Where n is the length of the list.



IndexOf
func (list *Sll) IndexOf(payload interface{}) (int, bool)

Get the index of a node, returns the index and an ok value. Note that the comparison is done by using == operator

Average time complexity: O(n)

Where n is the length of the list.



Print
func (list *Sll) Print()

Print the entire content of the list using fmt. Maybe used for debugging.

Average time complexity: O(n)

Where n is the length of the list.



Length
func (list *Sll) Length() int

Returns the length (no. of nodes) of the list.

Average time complexity: O(1)



ToSlice
func (list *Sll) ToSlice() []interface{}

Returns the payloads of all the nodes in a slice, order being same of the nodes.

Average time complexity: O(n)

Where n is the length of the list.

Clear
func (list *Sll) Clear()

Clears the list (removes all node)

Average time complexity: O(1)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Payload interface{}
	// contains filtered or unexported fields
}

Node : A struct describing the node, has Payload and a pointer to the next node in the list

type Sll

type Sll struct {
	Head     *Node
	LastNode *Node
	// contains filtered or unexported fields
}

Sll : A singly linked list which hols the elements

func InitList

func InitList(payload interface{}) *Sll

InitList : Instantiate a new linked list

func (*Sll) Append

func (list *Sll) Append(payload interface{}) *Node

Append : Append a node to the end of the list

func (*Sll) Clear

func (list *Sll) Clear()

Clear : Clears the list

func (*Sll) Get

func (list *Sll) Get(index int) (*Node, bool)

Get : Get a node from specified index

func (*Sll) IndexOf

func (list *Sll) IndexOf(payload interface{}) (int, bool)

IndexOf : Finds first occurance given payload in the list, if found returns its index else -1

func (*Sll) InsertAfter

func (list *Sll) InsertAfter(referenceNode *Node, payload interface{}) (*Node, bool)

InsertAfter : Insert a node after a given node. Returns pointer to newly inserted node, ok

func (*Sll) InsertAt

func (list *Sll) InsertAt(index int, payload interface{}) (*Node, bool)

InsertAt : Insert a node at at particular index, the current node at that index gets shifted. 0 <= index < length. Returns pointer to newly inserted node, ok

func (*Sll) InsertBefore

func (list *Sll) InsertBefore(referenceNode *Node, payload interface{}) (*Node, bool)

InsertBefore : Insert a node before a given node. Returns pointer to newly inserted node, ok

func (*Sll) Length

func (list *Sll) Length() int

Length : Get the current length of the list

func (*Sll) Prepend

func (list *Sll) Prepend(payload interface{}) *Node

Prepend : Prepend a node to the begining of the list

func (*Sll) Print

func (list *Sll) Print()

Print : Print the list in using fmt.Println canbe used for debugging

func (*Sll) RemoveByIndex

func (list *Sll) RemoveByIndex(index int) (interface{}, bool)

RemoveByIndex : Remove node from specified index

func (*Sll) RemoveByNode

func (list *Sll) RemoveByNode(nodeToBeRemoved *Node) (interface{}, bool)

RemoveByNode : Remove a node from the list based on its reference, returns node's payload, ok

func (*Sll) ReplaceByIndex

func (list *Sll) ReplaceByIndex(index int, payload interface{}) (interface{}, *Node, bool)

ReplaceByIndex : Replace a node with a new node created with a given payload dpending upon the index. Returns the old node payload, newly created node, ok

func (*Sll) ReplaceByNode

func (list *Sll) ReplaceByNode(nodeToBeReplaced *Node, payload interface{}) (interface{}, *Node, bool)

ReplaceByNode : Replace a node with a new node created with a given payload dpending upon a node reference. Returns the old node payload, newly created node, ok

func (*Sll) ToSlice

func (list *Sll) ToSlice() []interface{}

ToSlice : Returns the elements present in the list as an array, orders are same as of the list

Jump to

Keyboard shortcuts

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