-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.lua
128 lines (113 loc) · 3.04 KB
/
linkedList.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
local LinkedList = {}
function LinkedList:new()
local instance = {}
setmetatable(instance, self)
self.__index = self
instance.head = nil
instance.currentNode = nil
instance.previousNode = nil
instance.length = 0
return instance
end
function LinkedList:begin()
if self.head then
self.previousNode = nil
self.currentNode = self.head
return self.head.value
end
end
function LinkedList:next()
if (self.currentNode and self.currentNode.nextNode) then
self.previousNode = self.currentNode
self.currentNode = self.currentNode.nextNode
return self.currentNode.value
end
return nil
end
function LinkedList:last()
if self:begin() then
while self:next() do end
return self.currentNode.value
end
return nil
end
function LinkedList:push(obj)
local node = {
value = obj,
nextNode = self.head,
previousNode = nil
}
self.head = node
self.length = self.length + 1
end
function LinkedList:popBack()
if self:last() then
local node = self.currentNode
if (self.previousNode) then
self.previousNode.nextNode = nil
end
self.length = self.length - 1
if self.length == 0 then
self.head = nil
self.currentNode = nil
self.previousNode = nil
end
return node.value
end
return nil
end
function LinkedList:removeHere()
local current = self.currentNode
if (not current) then return nil end
local nextNode = current.nextNode
local previous = self.previousNode
if nextNode then
if previous then --case: { o -> O -> o }
previous.nextNode = nextNode
else --case: { _ -> O -> o }
self.head = nextNode
end
self.currentNode = nextNode
current.nextNode = nil
else
if previous then --case: { o -> O -> _ }
self.currentNode = previous
current.nextNode = nil
self.previousNode = nil
else --case: { _ -> O -> _ }
self.head = nil
self.currentNode = nil
self.previousNode = nil
end
end
self.length = self.length - 1
return current.value
end
function LinkedList:find(value)
local currentValue = self:begin()
if (not currentValue) then return false end
if currentValue == value then return true end
currentValue = self:next()
while currentValue do
if currentValue == value then return true end
currentValue = self:next()
end
return false
end
function LinkedList:toArray()
if (not self:begin()) then return {} end
local arr = { self.currentNode.value }
while self:next() do
arr.push(self.currentNode.value)
end
return arr
end
function LinkedList:toString()
if (not self:begin()) then return "{}" end
local str = "{ "..self.currentNode.value
while self:next() do
str = str..", "..self.currentNode.value
end
str = str.." }"
return str
end