lua写的一个顺序表

local list = {}
function list:InitList(length)
    local small_list = {}
    small_list.size = length
    small_list.length = length
    self.small_list = {}
     self.small_list.number = {}
    for i = 1 , length do
        self.small_list.number[i] = 0
    end
  
end


function list:DestoryList()
  self.small_list.number = {}
  self.small_list.number = nil
end


function list:ClearList()
    for i = 1 , #self.small_list.number do
        self.small_list.number[i] = 0
    end
    self.small_list.number = {}
end


function list:ListEmpty()
   if not next(self.small_list.number) then
      return true
   end
   return false
end


function list:ListLength()
   local num = 0
   for i = 1, #self.small_list.number do 
      num = num + 1 
  end
  return num
end


function list:GetElem(i)
    return self.small_list.number[i]
end


function list:LocateElem(num , two_number)
    for k ,v in pairs  (self.small_list.number) do
        if num == v then
            two_number = k 
            return two_number
        end
    end
    return -1
end


function list:PriorElem(num , two_number)
    if num < 1 or num >self.small_list.length then
        return false
    elseif self.small_list.length == 0 then 
        return false
    end
    two_number = self.small_list.number[num - 1]
   return true and  two_number 
end


function list:NextElem(num , two_number)
    if num < 0 or num >= self.small_list.length then
        return false
    elseif  0 == self.small_list.length then
        return false
    end
    two_number = self.small_list.number[num + 1]
    return true and two_number 
end
    
function list:ListInsert(num , two_number)
     self.small_list.length = self.small_list.length + 1
    if self.small_list.size == self.small_list.length then
        return false
    elseif num < 1 or num > self.small_list.length then
        return false
    end
    for i = self.small_list.length - 1 , num, - 1 do 
        self.small_list.number[i + 1] = self.small_list.number[i]
    end
     self.small_list.number[num] = two_number
    return true 
end


function list:ListDelete(num , two_number)
    if 0 == self.small_list.length then
        return false
    elseif  num < 1  or num > self.small_list.length then
        return false
    end
    two_number = self.small_list.number[num]
    for i = num + 1 , self.small_list.length do 
        self.small_list.number[i - 1] = self.small_list.number[i]
    end
    self.small_list.length = self.small_list.length - 1
    return true and two_number
end


function list:ListTraverse()
    for k , v in pairs (self.small_list.number) do
        print(v , "打印线性表中的元素" ) 
    end
  
end


function list:main()
    
    list:InitList(10)
    list:ListInsert(1,2)
    list:ListInsert(2,5)
    list:ListInsert(3,6)
    list:ListInsert(4,7)
    list:ListInsert(5,8)
    list:ListTraverse()
    local a 
 
    list:ListTraverse()
end 
list:main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Lua 实现 A* 算法的示例代码: ```lua -- 定义节点结构体 local Node = { x = 0, -- 节点的 x 坐标 y = 0, -- 节点的 y 坐标 g = 0, -- 从起点到当前节点的实际代价 h = 0, -- 从当前节点到目标节点的估计代价 f = 0, -- f = g + h parent = nil, -- 父节点 } -- 计算曼哈顿距离 local function manhattanDistance(node, target) return math.abs(node.x - target.x) + math.abs(node.y - target.y) end -- A* 算法 local function aStar(start, target, map) -- 初始化 open 和 close 列表 local open = {} local close = {} -- 将起点加入 open 列表 table.insert(open, start) while #open > 0 do -- 选取 f 值最小的节点 local current = open[1] for i = 2, #open do if open[i].f < current.f then current = open[i] end end -- 如果当前节点为目标节点,则返回路径 if current.x == target.x and current.y == target.y then local path = {} while current.parent ~= nil do table.insert(path, 1, current) current = current.parent end table.insert(path, 1, start) return path end -- 将当前节点从 open 列表中移除,并加入 close 列表 for i = #open, 1, -1 do if open[i].x == current.x and open[i].y == current.y then table.remove(open, i) break end end table.insert(close, current) -- 遍历当前节点的邻居节点 local neighbors = {} if map[current.x - 1] and map[current.x - 1][current.y] then table.insert(neighbors, map[current.x - 1][current.y]) end if map[current.x + 1] and map[current.x + 1][current.y] then table.insert(neighbors, map[current.x + 1][current.y]) end if map[current.x][current.y - 1] then table.insert(neighbors, map[current.x][current.y - 1]) end if map[current.x][current.y + 1] then table.insert(neighbors, map[current.x][current.y + 1]) end for _, neighbor in ipairs(neighbors) do -- 如果邻居节点已经在 close 列表中,则忽略 local isInClose = false for i = 1, #close do if close[i].x == neighbor.x and close[i].y == neighbor.y then isInClose = true break end end if isInClose then goto continue end -- 如果邻居节点不可通行,则忽略 if not neighbor.walkable then goto continue end -- 计算邻居节点的实际代价 local g = current.g + 1 -- 如果邻居节点不在 open 列表中,则添加到 open 列表 local isInOpen = false for i = 1, #open do if open[i].x == neighbor.x and open[i].y == neighbor.y then isInOpen = true break end end if not isInOpen then neighbor.parent = current neighbor.g = g neighbor.h = manhattanDistance(neighbor, target) neighbor.f = g + neighbor.h table.insert(open, neighbor) else -- 如果邻居节点已经在 open 列表中,则更新 f 值和父节点 if g < neighbor.g then neighbor.parent = current neighbor.g = g neighbor.f = g + neighbor.h end end ::continue:: end end -- 没有找到路径,返回空表 return {} end ``` 在使用该算法时,需要先定义地图和起点、目标点的位置。地图可以用一个二维数组来表示,每个元素表示一个节点,包含节点的 x、y 坐标和是否可通行等信息。下面是一个简单的示例: ```lua local map = { { {x=1, y=1, walkable=true}, {x=1, y=2, walkable=true}, {x=1, y=3, walkable=true} }, { {x=2, y=1, walkable=true}, {x=2, y=2, walkable=false}, {x=2, y=3, walkable=true} }, { {x=3, y=1, walkable=true}, {x=3, y=2, walkable=true}, {x=3, y=3, walkable=true} }, } local start = map[1][1] local target = map[3][3] local path = aStar(start, target, map) ``` 该示例中,起点为地图左上角节点,目标点为地图右下角节点。节点的 `walkable` 属性表示是否可通行,其中 `false` 表示不可通行。算法返回的结果是一个包含路径节点的数组,按顺序表示从起点到目标点的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值