0 Replies - 318 Views - Last Post: 11 April 2009 - 04:39 PM

#1 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

[LUA]Lua: A Very Basic Linked List

Posted 11 April 2009 - 04:39 PM

Description: Implement using tables, can hold any data type in Lua (except nil of course). Shows how to use a table to emulate a linked list. This version is more akin to a stack linked list given the LIFO output print.
--[[
-- KYA
-- 4/11/09
-- A Basic Linked List gathered from input
--]]

--variable initialized
list = nil
--get 10 inputs [from the user], add to list
print("Enter 10 items (one per line):")
for i = 1, 10 do
	input = io.read()
	list = {next = list, value = input}
end

--Example Usage
--some blank space
print("")
--print out list
l = list			-->assign list to 'l'
--list is printed out in reverse since this list
--was implemented as a 'stack'
print("List printed out backwards:")
while l do
	print (l.value)
	l = l.next
end



Is This A Good Question/Topic? 0
  • +

Page 1 of 1