Suphi's Hybrid Linked List Module
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 herePermission 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