lua单链表实现
List = {} --创建一个节点 function List.new(val) return {pnext = nil, value = val} end --往一个节点后添加一个节点 function List.addNode(nodeParent, nodeChild) nodeChild.pnext = nodeParent.pnext nodeParent.pnext = nodeChild return nodeChild end --输出链表 function List.print(list) while list do print(list.value) list = list.pnext end end --实现尾部插入 pHead = List.new(1) local node = pHead for i=2, 10 do node = List.addNode(node, List.new(i)) end List.print(pHead) --实现头部插入 pHead = List.new(10) for i=1, 9 do List.addNode(pHead, List.new(i)) end List.print(pHead)