linkedlist

package
v0.0.0-...-846829c Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoubleLinkedList

type DoubleLinkedList struct {
	// contains filtered or unexported fields
}

DoubleLinkedList is a Double Linked List structure

It's a linear data structure, which means that it has a sequence of nodes. With a "Doubly linked list" each node has a pointer to the previous and next node.

Because the Double Linked List hold a reference to the next and previous node, The first and last node are immediately accessible.

This is not a thread-safe implementation. Because of the function calls that you make it's better to create your own thread-safe implementation(if needed). This is just a simple implementation to show how it works and because the common use cases dont need a thread-safe implementation of this data structure.

func NewDoubleLinkedList

func NewDoubleLinkedList(useNodePool bool) *DoubleLinkedList

NewDoubleLinkedList creates a new DoubleLinkedList. It will initialize the struct with the default parameters.

useNodePool is the only argument, which is a indicator if nodePool should be used to create new nodes and if they should be released into the nodePool.

func (*DoubleLinkedList) First

func (d *DoubleLinkedList) First() *Node

First is a little helper function to get the first node in the sequence. It returns the first node in the sequence, this is not the case when the sequence is empty. Then it will return nil.

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122
^
|

Return this node.

func (*DoubleLinkedList) InsertAtFront

func (d *DoubleLinkedList) InsertAtFront(value any) *Node

InsertAtFront adds a new node to the front of the sequence. It returns the new node that was added.

A example: All nodes have a number Value.

  8 <-> 152 <-> 9828 <-> 5 <-> 12344122

	insert new node with value of 69

New sequence:

69 <-> 8 <-> 152 <-> 9828 <-> 5 <-> 12344122

func (*DoubleLinkedList) InsertAtLast

func (d *DoubleLinkedList) InsertAtLast(value any) *Node

InsertAtLast adds a new node to the back of the sequence. It returns the new node that was added.

A example: All nodes have a number Value.

  8 <-> 152 <-> 9828 <-> 5 <-> 12344122

	insert new node with value of 6941

New sequence:

8 <-> 152 <-> 9828 <-> 5 <-> 12344122 <-> 6941

func (*DoubleLinkedList) InsertValueAfterNode

func (d *DoubleLinkedList) InsertValueAfterNode(value any, targetNode *Node) *Node

InsertValueAfterNode will insert a given value before a given node. This will let the user insert a new node at a given position in the sequence It will return the pointer to the node, if the given node is in the list, otherwise it will return a nil.

A example: All nodes have a number Value.

 8 <-> 152 <-> 9828 <-> 5 <-> 12344122
               ^^^^
                ||
         Target node

Insert a new node with the value of 314 after this node

New sequence:

8 <-> 152 <-> 9828 <-> 314 <-> 5 <-> 12344122

func (*DoubleLinkedList) InsertValueBeforeNode

func (d *DoubleLinkedList) InsertValueBeforeNode(value any, targetNode *Node) *Node

InsertValueBeforeNode will insert a given value before a given node. This will let the user insert a new node at a given position in the sequence It will return the pointer to the node, if the given node is in the list, otherwise it will return a nil.

A example: All nodes have a number Value.

 8 <-> 152 <-> 9828 <-> 5 <-> 12344122
               ^^^^
                ||
         Target node

Insert a new node with the value of 314 before this node

New sequence:

8 <-> 152 <-> 314 <-> 9828 <-> 5 <-> 12344122

func (*DoubleLinkedList) Last

func (d *DoubleLinkedList) Last() *Node

Last is a little helper function to get the last node in the sequence. It returns the last node in the sequence, this is not the case when the sequence is empty. Then it will return nil.

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122
                             ^^^^^^^^
                                ||
                         Return this node

func (*DoubleLinkedList) MoveNodeToBack

func (d *DoubleLinkedList) MoveNodeToBack(targetNode *Node)

MoveNodeToBack will move a given node to the back of the list. This won't change the size of the list, just re-locate an existing node in the list to a the back of the list. It will return nothing, not on success or on failure(node has to be in the list).

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122
              ^^^^
               ||
        Target node

New sequence:

8 <-> 152 <-> 5 <-> 12344122 <-> 9828

func (*DoubleLinkedList) MoveNodeToFront

func (d *DoubleLinkedList) MoveNodeToFront(targetNode *Node)

MoveNodeToFront will move a given node to the front of the list. This won't change the size of the list, just re-locate an existing node in the list to a the front of the list. It will return nothing, not on success or on failure(node has to be in the list)

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122
              ^^^^
               ||
        Target node

New sequence:

9828 <-> 8 <-> 152 <-> 5 <-> 12344122

func (*DoubleLinkedList) Remove

func (d *DoubleLinkedList) Remove(node *Node)

Remove removes a node from the list. It won't return a error if the node is not from the given list.

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122
              ^^^^
               ||
        Remove this node

New sequence:

8 <-> 152 <-> 5 <-> 12344122

func (*DoubleLinkedList) ReverseOrderOfList

func (d *DoubleLinkedList) ReverseOrderOfList()

ReverseOrderOfList is a common task in linked list structures. It will reverse the order of nodes in the list, such that the first node in the list will eventually become the last node in the list. The size of the list won't change, it will simply change the order of nodes.

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122

            Reverse this list.

New sequence:

12344122 <-> 5 <-> 9828 <-> 152 <-> 8

func (*DoubleLinkedList) Size

func (d *DoubleLinkedList) Size() uint

Size is a little helper function to get the size of the sequence. It returns the amount of nodes that are in the sequence.

func (*DoubleLinkedList) ToSlice

func (d *DoubleLinkedList) ToSlice() []any

ToSlice can be considered a helper function to convert the current list to a slice. The returned slices will only contain the values of the nodes, the order of the slice is the same as the list. The first item in the slice is the value of the first node in the list.

A example: All nodes have a number Value.

8 <-> 152 <-> 9828 <-> 5 <-> 12344122

         Convert to a slice

Returns: []interface{}{8, 152, 9828, 5, 12344122}.

type Node

type Node struct {
	// Each node has a value, this value can be any type.
	// Which is why we use interface{}.
	// The value is a public field, so it can be accessed.
	Value any
	// contains filtered or unexported fields
}

Node is the struct type for the individual nodes that will be in the Double linked list.

It holds the value of the node, which can be anything. It holds the pointer of the next and previous node. It holds the pointer to the list which the node belongs to.

Node doesn't has any current helper function implemented.

Jump to

Keyboard shortcuts

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