ID:1694206
 
Decided to have a little fun and make a small command set for linked lists without using the /list datum. This isn't by any means something I would use, but it was fun to work on for ten minutes anyway. The command set uses recursive function calls, with the /listlink datum to store link data.

If you so happen to want to use this for whatever reason go ahead.

///////////
// DATUM //
///////////

listlink
var/value
var/link

//////////////
// COMMANDS //
//////////////

// Creates a new list link with value V.
proc/LinkedList(V)
var/listlink/l = new()
l.value = V
return l


// Adds a value to list link L. If L already has a value,
// a new link is made and attached to L, which contains V.
proc/LinkedList_Add(listlink/L, V)
if (!L.value)
L.value = V

else if (!L.link)
L.link = LinkedList(V)

else if (L.link)
LinkedList_Add(L.link, V)


// Searches the list starting at link L for value V.
// If V is found in a link, the link previously checked
// will then contain the link of the current linknode.
// M is optional, but required if starting at the middle
// of the list.
proc/LinkedList_Remove(listlink/L, V, listlink/M)
if (L)
if (L.value == V)
if (M)
M.link = L.link
else
return L.link

else if (L.link)
return LinkedList_Remove(L.link, V, L)

return L


// Debugs the list by printing out the values to the world.
// V stands for the index of the list not used by user).
proc/LinkedList_Print(listlink/L, V=1)
if (V==1)
world << "Printing list:"
if (L)
if (L.value)
world << "[V]: [L.value]"
if (L.link)
LinkedList_Print(L.link, V+1)
else
world << "List does not exist (anymore)."


////////////////////////////////////////////
// DEBUG (REMOVE IF USING THE CODE ABOVE) //
////////////////////////////////////////////

client/New()
..()
var/d = LinkedList() // Create a new list link
LinkedList_Add(d,10) // Add 10 to current link
LinkedList_Add(d,20) // Add 20 to link (makes new one)
LinkedList_Add(d,30) // Add 30 to link (makes new link from new link)

d = LinkedList_Remove(d,10) // Remove 10 from the list (returns L.link)
d = LinkedList_Remove(d,20) // Remove 20 from the list (returns L.link.link)
d = LinkedList_Remove(d,40) // 40 not found.

LinkedList_Print(d) // Prints 30.
For extra fun try doubly linked circular list :)

These do actually have some practical uses in games but like you said you just made this one for fun. Looks good.
You are a naughty programmer. Such magic should be forbidden.

Go on...
In response to Mr_Goober
Mr_Goober wrote:
You are a naughty programmer. Such magic should be forbidden.

Go on...

Not sure I get it.