-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex14.2.lua
More file actions
33 lines (29 loc) · 756 Bytes
/
ex14.2.lua
File metadata and controls
33 lines (29 loc) · 756 Bytes
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
function ListNew() return {first = 0, last = 0} end
function PushFirst(list, value)
local first = list.first - 1
list.first = first
list[first] = value
end
function PushLast(list, value)
local last = list.last + 1
list.last = last
list[last] = value
end
function PopFirst(list)
local first = list.first
if first == 0 then error("list is empty") end
local value = list[first]
list[first] = nil
-- to allow garbage collection
list.first = first + 1
return value
end
function PopLast(list)
local last = list.last
if list.first == 0 then error("list is empty") end
local value = list[last]
list[last] = nil
-- to allow garbage collection
list.last = last - 1
return value
end