首页 > 代码库 > Lua实现的栈、队列

Lua实现的栈、队列

基于quick cocos2d-x2.2.5的API,写的一个栈,队列。满足游戏开发中的需求。

避免重复造车轮~(简单测试。留着自己用。不确定是否还存在bug)

栈Stack:

 1 -- 2 -- Date: 2014-11-19 15:29:02 3 -- 4 local Stack = class("Stack") 5  6 function Stack:ctor() 7     self.stack_table = {} 8 end 9 10 function Stack:push(element)11     local size = self:size()12     self.stack_table[size + 1] = element13 end14 15 function Stack:pop()16     local size = self:size()17     if self:isEmpty() then18         printError("Error: Stack is empty!")19         return20     end21     return table.remove(self.stack_table,size)22 end23 24 function Stack:top()25     local size = self:size()26     if self:isEmpty() then27         printError("Error: Stack is empty!")28         return29     end30     return self.stack_table[size]31 end32 33 function Stack:isEmpty()34     local size = self:size()35     if size == 0 then36         return true37     end38     return false39 end40 41 function Stack:size()42     return table.nums(self.stack_table) or 043 end44 45 function Stack:clear()46     -- body47     self.stack_table = nil48     self.stack_table = {}49 end50 51 function Stack:printElement()52     local size = self:size()53 54     if self:isEmpty() then55         printError("Error: Stack is empty!")56         return57     end58 59     local str = "{"..self.stack_table[size]60     size = size - 161     while size > 0 do62         str = str..", "..self.stack_table[size]63         size = size - 164     end65     str = str.."}"66     print(str)67 end68 69 70 return Stack

队列Queue:

 1 -- 2 -- Date: 2014-11-19 16:51:19 3 -- 4 local Queue = class("Queue") 5  6 function Queue:ctor(capacity) 7     self.capacity = capacity 8     self.queue = {} 9     self.size_ = 010     self.head = -111     self.rear = -112 end13 14 function Queue:enQueue(element)15     if self.size_ == 0 then16         self.head = 117         self.rear = 118         self.size_ = 119         self.queue[self.rear] = element20     else21         local temp = (self.rear + 1) % self.capacity22         if temp == self.head then23             printError("Error: capacity is full.")24             return 25         else26             self.rear = temp27         end28 29         self.queue[self.rear] = element30         self.size_ = self.size_ + 131     end32 33 end34 35 function Queue:deQueue()36     if self:isEmpty() then37         printError("Error: The Queue is empty.")38         return39     end40     self.size_ = self.size_ - 141     self.head = (self.head + 1) % self.capacity42     local value =http://www.mamicode.com/ self.queue[self.head]43     return value44 end45 46 function Queue:clear()47     self.queue = nil48     self.queue = {}49     self.size_ = 050     self.head = -151     self.rear = -152 end53 54 function Queue:isEmpty()55     if self:size() == 0 then56         return true57     end58     return false59 end60 61 function Queue:size()62     return self.size_63 end64 65 function Queue:printElement()66     local h = self.head67     local r = self.rear68     local str = nil69     local first_flag = true70     while h ~= r do71         if first_flag == true then72             str = "{"..self.queue[h]73             h = (h + 1) % self.capacity74             first_flag = false75         else76             str = str..","..self.queue[h]77             h = (h + 1) % self.capacity78         end79     end80     str = str..","..self.queue[r].."}"81     print(str)82 end83 84 return Queue

 ======================================================

 1 --调用过程: 2 local MainScene = class("MainScene", function() 3     return display.newScene("MainScene") 4 end) 5  6 local CURRENT_MODULE_NAME = ... 7 local Queue = import("../datastruct/Queue", CURRENT_MODULE_NAME) 8  9 function MainScene:ctor()10     local queue = Queue.new(5)11     queue:enQueue("a")12     queue:deQueue()13     queue:enQueue("b")14     queue:enQueue("c")15     queue:enQueue("d")16     queue:deQueue()17     queue:deQueue()18     queue:enQueue("e")19     queue:enQueue("f")20     queue:enQueue("g")21     queue:deQueue()22     queue:enQueue("h")23     queue:enQueue("i")24     queue:printElement()25 end26 27 function MainScene:onEnter()28 end29 30 function MainScene:onExit()31 end32 33 return MainScene

over~

Lua实现的栈、队列