-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex17.1.lua
47 lines (40 loc) · 963 Bytes
/
ex17.1.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local function listNew ()
return {first = 0, last = -1}
end
local function pushFirst (list, value)
local first = list.first - 1
list.first = first
list[first] = value
end
local function pushLast (list, value)
local last = list.last + 1
list.last = last
list[last] = value
end
local function popFirst (list)
local first = list.first
if first > list.last then error("list is empty") end
local value = list[first]
list[first] = nil
-- to allow garbage collection
list.first = first + 1
return value
end
local function popLast (list)
local last = list.last
if list.first > last then error("list is empty") end
local value = list[last]
list[last] = nil
-- to allow garbage collection
list.last = last - 1
return value
end
local M =
{
listNew = listNew,
pushFirst = pushFirst,
pushLast = pushLast,
popFirst = popFirst,
popLast = popLast,
}
return M