Suphi's Hybrid Linked List Module

Suphi's Hybrid Linked List Module

Download | Discord Server

Features

Edit While Iterating          "Its safe to insert and remove links while iterating"
Linked List + Dictionary      "The power of a linked list and a dictionary in one"

Support My Work

If you liked my work and want to donate to me you can do so here


SourceCode

You can get the sourcecode to this module here

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.


Documentation

CONSTRUCTOR

new()
"Creates a new hybrid linked list"

LIST PROPERTIES

Links  table  {}
"Dictionary of links with the values as keys"
Length  number  0
"The length of the list"

LIST METHODS

InsertFront(value: any)  link
"Adds any value to the front of the list"
InsertAfter(value: any)  link
"The same as InsertFront"
InsertBack(value: any)  link
"Adds any value to the back of the list"
InsertBefore(value: any)  link
"The same as InsertBack"
GetNext(link: link?)  link?
"Gets the next link in the list if its the last link returns nil, if no link is passed into the method returns the first link"
GetPrevious(link: link?)  link?
"Gets the previous link in the list if its the first link returns nil, if no link is passed into the method returns the last link"
IterateForward(link: link?)  function list link?
"Returns a iterator that can be used in a for loop if a link is passed into this function it will start interating from that link"
IterateBackward(link: link?)  function list link?
"Returns a iterator that can be used in a for loop if a link is passed into this function it will start interating from that link"
Remove(value: any)  link?
"Removes link with a specific value in the list and returns that link, returns nil if value was not found"
RemoveFirst()  any link?
"Removes the first link in the list and returns that links value plus link, returns nil if there are no links in the list"
RemoveLast()  any link?
"Removes the last link in the list and returns that link value plus link, returns nil if there are no links in the list"

LINK METHODS

InsertAfter(value: any)  link
"Adds any value after the link"
InsertBefore(value: any)  link
"Adds any value before the link"
GetNext()  link?
"Gets the next link after this link if this link is the last link then nil is returned"
GetPrevious()  link?
"Gets the previous link before this link if this link is the first link then nil is returned"
Remove()  nil
"Removes this link from the list"

Example

-- Require the ModuleScript
local hybridLinkedList = require(118822092315237)

-- Make a new hybrid linked list
local list = hybridLinkedList.new()

-- Insert 4 values
local link1 = list:InsertBack("A")
local link2 = list:InsertBack("B")
local link3 = list:InsertBack("C")
local link4 = list:InsertBack("D")

-- Insert another value after link2
link2:InsertAfter("Hello")

-- Insert another value after value C
list.Links.C:InsertAfter("World")

-- Remove value B
list:Remove("B") -- or list.Links.B:Remove()

-- Iterate forward and print values
for link, value in list:IterateForward() do print(value) end

-- Remove Link 4
link4:Remove()

-- get the first links Value
print("First Value:   ", list:GetNext().Value)

-- get the second links value
print("Second Value:  ", list:GetNext():GetNext().Value)

-- print the lists length
print("Length:        ", list.Length)

LINKED LIST VS HYBRID LINKED LIST

local link1 = linkedList:InsertBack({Value = "A"})
local link2 = linkedList:InsertBack({Value = "B"})
local link3 = linkedList:InsertBack({Value = "C"})

link2:InsertAfter({Value = "Insert"})
link2:Remove()

for link in linkedList:IterateForward() do print(link.Value) end
local link1 = hybridLinkedList:InsertBack("A")
local link2 = hybridLinkedList:InsertBack("B")
local link3 = hybridLinkedList:InsertBack("C")

hybridLinkedList.Links.B:InsertAfter("Insert") -- same as link2:InsertAfter("Insert")
hybridLinkedList:Remove("B") -- same as hybridLinkedList.Links.B:Remove() and link2:Remove()

for link, value in hybridLinkedList:IterateForward() do print(value) end

Other Projects

Infinite Terrain
Suphi’s DataStore Module
Global Framework
Infinite Scripter
Mesh Editor
Toggle Block Comment
Toggle Decomposition Geometry
Tag Explorer
Suphi’s Linked List Module
Suphi’s Hybrid Linked List Module
Suphi’s RemoteFunction Module

11 Likes

I have no clue what this does, but it’s made by Suphi, so 10/10

2 Likes

I’m not sure how it works but if Suphi made it it’s definitely great

This video might help clear up how this work

This hybrid version works like a linked list but at the same time uses a dictionary this has some benefits in that it allows you to also index the links via there value

-- Make a list
local list = hybridLinkedList.new()

-- Insert values A, B, C, D into the list
list:InsertBack("A")
list:InsertBack("B")
list:InsertBack("C")
list:InsertBack("D")

-- Use the dictionary part of the list to get the link that has the C value
-- without the need to loop through the linked list to find it
local linkC = list.Links.C

one thing you might wonder is why not just use a dictionary on its own
and if you can do that then that’s what you should do but in some cases you might want to keep the order of your dictionary’s or want to insert new items while iterating and that’s what this hybridLinkedList allows you to do

1 Like

I dont know what the use cases are, but as it was made by Suphi, it 100% useful.

This is great, I will definitely use this when I get around to redoing my spectate system. This will make switching backwards and forwards so much easier!

Thank you!

1 Like