連結串列LinkedList
阿新 • • 發佈:2021-11-12
1 LinkedList = LinkedList or {} 2 3 LinkedList.Node = LinkedList.Node or {} 4 local Node = LinkedList.Node 5 6 7 function Node.new(v2) 8 local inst = { 9 _cname = "lang.LinkedList.Node", 10 _list = nil, 11 _n = nil, 12 _p = nil, 13 } 1415 local v = v2 16 17 function inst:value() 18 return v 19 end 20 21 function inst:setValue(v2) 22 v = v2 23 end 24 25 function inst:next() 26 return self._n 27 end 28 29 function inst:prev() 30 returnself._p 31 end 32 33 function inst:list() 34 return self._list 35 end 36 37 return inst 38 end 39 40 41 42 43 function LinkedList.new() 44 local inst = { 45 _cname = "lang.LinkedList" 46 } 47 48 local first = nil 49 locallast = nil 50 local len = 0 51 52 function inst:count() 53 return len 54 end 55 56 function inst:clear() 57 first = nil 58 last = nil 59 len = 0 60 end 61 62 function inst:contains(v) 63 if 0 == len then return false end 64 if "lang.LinkedList.Node" == v._cname then 65 assert(self == v._list, "remove: not my node") 66 local n_cur = first 67 while nil ~= n_cur do 68 if n_cur == v then 69 return true 70 end 71 n_cur = n_cur:next() 72 end 73 else 74 local n_cur = first 75 while nil ~= n_cur do 76 if n_cur:value() == v then 77 return true 78 end 79 n_cur = n_cur:next() 80 end 81 end 82 83 return false 84 end 85 86 function inst:copyTo(dst) 87 end 88 89 local function removeNode(self, n_cur) 90 if n_cur == first then 91 self:removeFirst() 92 elseif n_cur == last then 93 self:removeLast() 94 else 95 local n_left = n_cur._p 96 local n_right = n_cur._n 97 98 n_cur._p = nil 99 n_cur._n = nil 100 n_cur._list = nil 101 len = len - 1 102 103 n_left._n = n_right 104 n_right._p = n_left 105 end 106 end 107 108 function inst:remove(v) 109 if 0 == len then return false end 110 if "lang.LinkedList.Node" == v._cname then 111 assert(self == v._list, "remove: not my node") 112 local n_cur = first 113 while nil ~= n_cur do 114 if n_cur == v then 115 removeNode(self, n_cur) 116 return true 117 end 118 n_cur = n_cur:next() 119 end 120 else 121 local n_cur = first 122 while nil ~= n_cur do 123 if n_cur:value() == v then 124 removeNode(self, n_cur) 125 return true 126 end 127 n_cur = n_cur:next() 128 end 129 end 130 131 return false 132 end 133 134 function inst:first() 135 if 0 == len then return nil end 136 return first:value() 137 end 138 139 function inst:last() 140 if 0 == len then return nil end 141 return last:value() 142 end 143 144 function inst:addAfter(node, v) 145 assert("lang.LinkedList.Node" == node._cname, "addAfter: invalid node type") 146 assert(self == node._list, "addAfter: not my node") 147 local n_cur = nil 148 if "lang.LinkedList.Node" == v._cname then 149 assert(nil == v._list, "addAfter: already add to some LinkedList") 150 n_cur = v 151 else 152 n_cur = Node.new(v) 153 end 154 n_cur._list = self 155 156 local n_left = node 157 if last == n_left then 158 n_left._n = n_cur 159 --最右 160 n_cur._p = n_left 161 n_cur._n = nil 162 last = n_cur 163 else 164 local n_right = n_left._n 165 166 n_left._n = n_cur 167 --中 168 n_cur._p = n_left 169 n_cur._n = n_right 170 171 n_right._p = n_cur 172 end 173 len = len + 1 174 end 175 176 function inst:addBefore(node, v) 177 assert("lang.LinkedList.Node" == node._cname, "addBefore: invalid node type") 178 assert(self == node._list, "addBefore: not my node") 179 180 local n_cur = nil 181 if "lang.LinkedList.Node" == v._cname then 182 assert(nil == v._list, "addBefore: already add to some LinkedList") 183 n_cur = v 184 else 185 n_cur = Node.new(v) 186 end 187 n_cur._list = self 188 189 local n_right = node 190 if first == n_right then 191 --最左 192 n_cur._p = nil 193 n_cur._n = n_right 194 first = n_cur 195 196 n_right._p = n_cur 197 else 198 local n_left = n_right._p 199 200 n_left._n = n_cur 201 --中 202 n_cur._p = n_left 203 n_cur._n = n_right 204 205 n_right._p = n_cur 206 end 207 len = len + 1 208 end 209 210 function inst:addFirst(v) 211 local n_cur = nil 212 if "lang.LinkedList.Node" == v._cname then 213 assert(nil == v._list, "addFirst: already add to some LinkedList") 214 n_cur = v 215 else 216 n_cur = Node.new(v) 217 end 218 n_cur._list = self 219 if 0 == len then 220 first = n_cur 221 last = n_cur 222 else 223 local n_right = first 224 --最左 225 n_cur._p = nil 226 n_cur._n = n_right 227 first = n_cur 228 229 n_right._p = n_cur 230 end 231 len = len + 1 232 end 233 234 function inst:addLast(v) 235 local n_cur = nil 236 if "lang.LinkedList.Node" == v._cname then 237 assert(nil == v._list, "addLast: already add to some LinkedList") 238 n_cur = v 239 else 240 n_cur = Node.new(v) 241 end 242 n_cur._list = self 243 if 0 == len then 244 first = n_cur 245 last = n_cur 246 else 247 local n_left = last 248 249 n_left._n = n_cur 250 --最右 251 n_cur._p = n_left 252 n_cur._n = nil 253 last = n_cur 254 end 255 len = len + 1 256 end 257 258 function inst:removeFirst() 259 if 0 == len then return false end 260 local n_cur = first 261 local n_right = first._n 262 263 n_cur._p = nil 264 n_cur._n = nil 265 n_cur._list = nil 266 len = len - 1 267 268 if 0 == len then 269 first = nil 270 last = nil 271 else 272 n_right._p = nil 273 first = n_right 274 end 275 return true 276 end 277 278 function inst:removeLast() 279 if 0 == len then return false end 280 local n_left = last._p 281 local n_cur = last 282 283 n_cur._p = nil 284 n_cur._n = nil 285 n_cur._list = nil 286 len = len - 1 287 288 if 0 == len then 289 first = nil 290 last = nil 291 else 292 n_left._n = nil 293 last = n_left 294 end 295 return true 296 end 297 298 function inst:find(v) 299 if 0 == len then return nil end 300 local n_cur = first 301 while n_cur ~= nil do 302 if n_cur:value() == v then 303 return n_cur 304 end 305 n_cur = n_cur:next() 306 end 307 return nil 308 end 309 310 function inst:findLast(v) 311 if 0 == len then return nil end 312 local n_cur = last 313 while n_cur ~= nil do 314 if n_cur:value() == v then 315 return n_cur 316 end 317 n_cur = n_cur:prev() 318 end 319 return nil 320 end 321 322 function inst:__tostring() 323 if 0 == len then return "" end 324 325 local strTb = {} 326 local n_cur = first 327 while n_cur ~= nil do 328 local v = n_cur:value() 329 table.insert(strTb, (nil == v) and "nil" or tostring(v)) 330 n_cur = n_cur:next() 331 end 332 return table.concat(strTb, ",") 333 end 334 335 function inst:__len() 336 return len 337 end 338 339 function inst:__index(k) 340 error("LinkedList: invalid member: " .. k) 341 end 342 343 function inst:__newindex(k, v) 344 error("LinkedList: new member error: " .. k) 345 end 346 347 setmetatable(inst, inst) 348 return inst 349 end 350 351 function LinkedList.__call() 352 return LinkedList.new() 353 end 354 setmetatable(LinkedList, LinkedList)
測試程式碼:
1 local function TestAdd() 2 local l = LinkedList() 3 l:addLast("one") 4 assert(1 == l:count(), "add error") 5 assert(l:contains("one"), "add error") 6 7 l:addLast("two") 8 assert(2 == l:count(), "add error") 9 assert(l:contains("one"), "add error") 10 assert(l:contains("two"), "add error") 11 12 tostring(l) 13 14 l:addFirst("zero") 15 assert(3 == l:count(), "add error") 16 assert(l:contains("zero"), "add error") 17 assert(l:contains("one"), "add error") 18 assert(l:contains("two"), "add error") 19 20 l:addLast("three") 21 assert(4 == l:count(), "add error") 22 assert(l:contains("zero"), "add error") 23 assert(l:contains("one"), "add error") 24 assert(l:contains("two"), "add error") 25 assert(l:contains("three"), "add error") 26 end 27 28 local function TestRemove() 29 local l = LinkedList() 30 l:addLast("one") 31 l:remove("one") 32 assert(0 == l:count(), "remove error") 33 assert(false == l:contains("one"), "remove error") 34 assert(false == l:removeFirst(), "remove error") 35 assert(false == l:removeLast(), "remove error") 36 37 l:addFirst("one") 38 l:addFirst("zero") 39 assert(l:removeFirst(), "remove error") 40 assert("one" == l:first(), "remove error") 41 assert("one" == l:last(), "remove error") 42 assert(l:removeLast(), "remove error") 43 assert(false == l:removeFirst(), "remove error") 44 assert(false == l:removeLast(), "remove error") 45 46 l:addFirst("one") 47 l:addLast("two") 48 l:addLast("three") 49 l:addLast("four") 50 l:remove("two") 51 assert("one" == l:first(), "remove error") 52 assert("four" == l:last(), "remove error") 53 assert(3 == l:count(), "remove error") 54 end 55 56 local function TestFind() 57 local l = LinkedList() 58 59 l:addFirst("one") 60 l:addLast("two") 61 l:addLast("three") 62 l:addLast("four") 63 64 assert(nil ~= l:find("one"), "find error") 65 assert(nil == l:find("five"), "find error") 66 end 67 68 TestAdd() 69 TestRemove() 70 TestFind()