标签:style blog io ar color os sp on div
基于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] = element 13 end 14 15 function Stack:pop() 16 local size = self:size() 17 if self:isEmpty() then 18 printError("Error: Stack is empty!") 19 return 20 end 21 return table.remove(self.stack_table,size) 22 end 23 24 function Stack:top() 25 local size = self:size() 26 if self:isEmpty() then 27 printError("Error: Stack is empty!") 28 return 29 end 30 return self.stack_table[size] 31 end 32 33 function Stack:isEmpty() 34 local size = self:size() 35 if size == 0 then 36 return true 37 end 38 return false 39 end 40 41 function Stack:size() 42 return table.nums(self.stack_table) or 0 43 end 44 45 function Stack:clear() 46 -- body 47 self.stack_table = nil 48 self.stack_table = {} 49 end 50 51 function Stack:printElement() 52 local size = self:size() 53 54 if self:isEmpty() then 55 printError("Error: Stack is empty!") 56 return 57 end 58 59 local str = "{"..self.stack_table[size] 60 size = size - 1 61 while size > 0 do 62 str = str..", "..self.stack_table[size] 63 size = size - 1 64 end 65 str = str.."}" 66 print(str) 67 end 68 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_ = 0 10 self.head = -1 11 self.rear = -1 12 end 13 14 function Queue:enQueue(element) 15 if self.size_ == 0 then 16 self.head = 1 17 self.rear = 1 18 self.size_ = 1 19 self.queue[self.rear] = element 20 else 21 local temp = (self.rear + 1) % self.capacity 22 if temp == self.head then 23 printError("Error: capacity is full.") 24 return 25 else 26 self.rear = temp 27 end 28 29 self.queue[self.rear] = element 30 self.size_ = self.size_ + 1 31 end 32 33 end 34 35 function Queue:deQueue() 36 if self:isEmpty() then 37 printError("Error: The Queue is empty.") 38 return 39 end 40 self.size_ = self.size_ - 1 41 self.head = (self.head + 1) % self.capacity 42 local value = self.queue[self.head] 43 return value 44 end 45 46 function Queue:clear() 47 self.queue = nil 48 self.queue = {} 49 self.size_ = 0 50 self.head = -1 51 self.rear = -1 52 end 53 54 function Queue:isEmpty() 55 if self:size() == 0 then 56 return true 57 end 58 return false 59 end 60 61 function Queue:size() 62 return self.size_ 63 end 64 65 function Queue:printElement() 66 local h = self.head 67 local r = self.rear 68 local str = nil 69 local first_flag = true 70 while h ~= r do 71 if first_flag == true then 72 str = "{"..self.queue[h] 73 h = (h + 1) % self.capacity 74 first_flag = false 75 else 76 str = str..","..self.queue[h] 77 h = (h + 1) % self.capacity 78 end 79 end 80 str = str..","..self.queue[r].."}" 81 print(str) 82 end 83 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 end 26 27 function MainScene:onEnter() 28 end 29 30 function MainScene:onExit() 31 end 32 33 return MainScene
over~
标签:style blog io ar color os sp on div
原文地址:http://www.cnblogs.com/vokie/p/4110003.html