Wrda
Spell Reviewer
- Joined
- Nov 18, 2012
- Messages
- 1,923
With the addition of Lua and its rising there's quite a few systems that need to be remade in such scripting language.
Linked list is one of the few subjects I lack knowledge on, might I say, pretty noobish, however I want to get somewhat deeper into it.
The code above can be found on this link https://developpaper.com/lua-implementation-of-linked-list/
I modified it a bit for my own purposes, but it seems to work (didn't test everything).
From what I understand, this is a doubly linked list, I'm not sure if it's optimized or if it's the best to use in some cases where linked lists are necessary.
There's also another one, a circular doubly linked list: LinkedList.lua
Any thoughts, comments about this use and usefulness as a later worthy submission?
Linked list is one of the few subjects I lack knowledge on, might I say, pretty noobish, however I want to get somewhat deeper into it.
Lua:
do
list = {}
function list.create()
local list = {}
list.first = nil
list.last = nil
list.count = 0
return list
end
---Insert an element after the last node
function list.push(list, value)
local item = {}
item.value = value
--Current empty queue
if list.first == nil then
list.first = item
list.last = item
list.count = 1
else
--Non empty queue, add to end
list.last.next = item
item.pre = list.last
list.last = item
list.count = list.count + 1
end
end
---Insert an element to the beginning
function list.insert(list, value)
local item = {}
item.value = value
if list.first == nil then
list.first = item
list.last = item
list.count = list.count + 1
else
item.next = list.first
list.first.pre = item
list.first = item
list.count = list.count + 1
end
end
function list.removeFirst(list)
local val = nil
--Empty stack
if list.count == 0 then
--Only one element
elseif list.count == 1 then
val = list.first.value
list.first = nil
list.last = nil
list.count = 0
--Multiple elements, first with value
elseif list.first ~= nil then
--Value
val = list.first.value
--Head to second
list.first = list.first.next
--Nonempty
if list.first ~= nil then
list.first.pre = nil
list.count = list.count - 1
else
list.count = 0
end
end
return val
end
---Delete last element
function list.removeLast(list)
local item = list.last
if list.last ~= nil then
--If there is only one element in itself
if list.last.pre == nil then
list.first = nil
list.last = nil
list.count = 0
else
--More than one element
list.last = list.last.pre
list.last.next = nil
list.count = list.count - 1
end
end
if item ~= nil then
return item.value
end
end
--looping through the whole list
function list.foreach(list, func)
local temp = list.first
local continue = true
while temp ~= nil and continue do
continue = func(temp.value)
if not continue then
break
end
temp = temp.next
end
end
end
I modified it a bit for my own purposes, but it seems to work (didn't test everything).
From what I understand, this is a doubly linked list, I'm not sure if it's optimized or if it's the best to use in some cases where linked lists are necessary.
There's also another one, a circular doubly linked list: LinkedList.lua
Any thoughts, comments about this use and usefulness as a later worthy submission?