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)

 

相关文章:

  • 2021-10-10
  • 2021-04-17
  • 2020-10-06
  • 2021-11-29
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2021-07-22
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
相关资源
相似解决方案