• Lua初学习 9-14_03 数据结构 ---> 队列 (自己写的 跟书上差不多)


    1:创建一个双向队列

    list = { first =1,last = 0}

    function list:pushFirst(value) --从头放入一个 value
    local f = self.first-1 -- f=0
    self[f] = value --self[0] = value
    self.first =f -- self.f = 0
    --print(self.first,f)
    end

    function list:pushLast(value) --从尾放入一个 value
    local l= self.last+1
    self[l] = value
    self.last = l
    end

    function list:popFirst() --推出第一个value
    if(self.first > self.last) then
    print("警告 : 队列已经空了 ")
    return nil
    end
    --print("进来时候 first index : ",self.first)
    local v = self[self.first]
    --print("推出来的first:",self[self.first])
    self[self.first] = nil
    self.first = self.first + 1
    --print("list first index : ",self.first)
    return v
    end

    function list:popLast() --推出最后一个value
    if(self.first > self.last) then
    print("警告 : 队列已经空了 ")
    return nil
    end
    local v = self[self.last]
    self[self.last] = nil
    self.last =self.last-1
    return v
    end


    list:pushFirst(11)
    list:pushFirst(22)
    list:pushFirst(33)
    list:pushLast(44)
    list:pushLast(55)
    --从前放入3个 从后放入2个 33 22 11 44 55

    print(list:popLast()) --last 55
    list:pushLast(99) -- 33 22 11 44 99
    list:pushFirst("orz") --orz" 33 22 11 44 99
    print(list:popFirst()) --first orz

          ========接下来再推剩下的==========

    print(list:popFirst()) --剩余5   ->orz
    print(list:popFirst()) --剩余4       33
    print(list:popFirst()) --剩余3       22
    print(list:popFirst()) --剩余2       11
    print(list:popFirst()) --剩余1       44
    print(list:popFirst()) --剩余0       99
    print(list:popFirst()) --剩余0       空警告

  • 相关阅读:
    MongoDB Shell
    mongo 日记
    java 堆栈 静态
    面向对象(2)
    面向对象(1)
    mongo 学习笔记
    深入浅出学Spring Data JPA
    java记录
    mongodb 2.6 window 安装启动服务
    CF1012F Passports
  • 原文地址:https://www.cnblogs.com/cocotang/p/5871283.html
Copyright © 2020-2023  润新知