|
1 | 1 | <?xml version="1.0" encoding="UTF-8" ?> |
2 | 2 | <class name="LinkedList" inherits="Reference" version="3.2"> |
3 | 3 | <brief_description> |
| 4 | + A doubly linked list data structure. |
4 | 5 | </brief_description> |
5 | 6 | <description> |
| 7 | + A data structure which consists of a set of sequentially linked elements called nodes. Uses [ListNode] as a basic building block. Each node contains a reference to the previous node, the next node, and the data associated with the node. Insertion and deletion operations are faster [code]O(1)[/code] compared to [Array], but performs worse for random access [code]O(n)[/code]. |
| 8 | + [ListNode]s are constructed by inserting values to the list, and are not meant to be instantiated directly: |
| 9 | + [codeblock] |
| 10 | + var list = LinkedList.new() |
| 11 | + var node = list.push_back("Goost") |
| 12 | + var same_node = list.find("Goost") |
| 13 | + [/codeblock] |
| 14 | + Traversing a list can be done using a [code]for[/code] loop: |
| 15 | + [codeblock] |
| 16 | + for node in list: |
| 17 | + print(node) |
| 18 | + [/codeblock] |
| 19 | + or by manually walking the list using the nodes themselves: |
| 20 | + [codeblock] |
| 21 | + # Forward! |
| 22 | + var node = list.front |
| 23 | + while node: |
| 24 | + print(node) |
| 25 | + node = node.next |
| 26 | + |
| 27 | + # Backward! |
| 28 | + var node = list.back |
| 29 | + while node: |
| 30 | + print(node) |
| 31 | + node = node.prev |
| 32 | + [/codeblock] |
| 33 | + Nodes can be passed around throughout the code, and values can be changed dynamically for nodes which are already inserted into the list. |
6 | 34 | </description> |
7 | 35 | <tutorials> |
8 | 36 | </tutorials> |
|
11 | 39 | <return type="void"> |
12 | 40 | </return> |
13 | 41 | <description> |
| 42 | + Erases all nodes from the list. |
14 | 43 | </description> |
15 | 44 | </method> |
16 | 45 | <method name="create_from"> |
|
19 | 48 | <argument index="0" name="value" type="Variant"> |
20 | 49 | </argument> |
21 | 50 | <description> |
| 51 | + Initializes the list from a [Variant] compatible type. Clears all nodes before copying. |
| 52 | + If [code]value[/code] is [code]null[/code], just clears the contents of the list. |
| 53 | + If [code]value[/code] is [Array], each element in the array is converted to a [ListNode]. Pool*Arrays are converted similarly to [Array]. |
| 54 | + If [code]value[/code] is [Dictionary], each key in the dictionary is converted to a [ListNode], and the values are encoded as [ListNode] meta variables using [method Object.set_meta]. Values can be retrieved later with [code]node.get_meta("value")[/code] for each node. |
| 55 | + Any other type is simply pushed back to the list. |
22 | 56 | </description> |
23 | 57 | </method> |
24 | 58 | <method name="empty" qualifiers="const"> |
25 | 59 | <return type="bool"> |
26 | 60 | </return> |
27 | 61 | <description> |
| 62 | + Returns [code]true[/code] if the list doesn't contain any nodes. |
28 | 63 | </description> |
29 | 64 | </method> |
30 | 65 | <method name="erase"> |
|
33 | 68 | <argument index="0" name="value" type="Variant"> |
34 | 69 | </argument> |
35 | 70 | <description> |
| 71 | + Erases (deletes) the first found node with a matching value in the list. |
36 | 72 | </description> |
37 | 73 | </method> |
38 | 74 | <method name="find"> |
|
41 | 77 | <argument index="0" name="value" type="Variant"> |
42 | 78 | </argument> |
43 | 79 | <description> |
| 80 | + Returns a node if a list contains a node with specified value, otherwise returns [code]null[/code]. |
44 | 81 | </description> |
45 | 82 | </method> |
46 | 83 | <method name="get_elements"> |
47 | 84 | <return type="Array"> |
48 | 85 | </return> |
49 | 86 | <description> |
| 87 | + An alias for [method get_nodes]. |
50 | 88 | </description> |
51 | 89 | </method> |
52 | 90 | <method name="get_nodes"> |
53 | 91 | <return type="Array"> |
54 | 92 | </return> |
55 | 93 | <description> |
| 94 | + Returns all nodes as an [Array], preserving front-to-back order. |
56 | 95 | </description> |
57 | 96 | </method> |
58 | 97 | <method name="insert_after"> |
|
63 | 102 | <argument index="1" name="value" type="Variant"> |
64 | 103 | </argument> |
65 | 104 | <description> |
| 105 | + Constructs a new [ListNode] and places it [i]after[/i] existing node in the list. If [code]node[/code] is [code]null[/code], then the value is pushed at the end of the list, making the behavior equivalent to [method push_back]. |
66 | 106 | </description> |
67 | 107 | </method> |
68 | 108 | <method name="insert_before"> |
|
73 | 113 | <argument index="1" name="value" type="Variant"> |
74 | 114 | </argument> |
75 | 115 | <description> |
| 116 | + Constructs a new [ListNode] and places it [i]before[/i] existing node in the list. If [code]node[/code] is [code]null[/code], then the value is pushed at the end of the list, making the behavior equivalent to [method push_back]. |
76 | 117 | </description> |
77 | 118 | </method> |
78 | 119 | <method name="invert"> |
79 | 120 | <return type="void"> |
80 | 121 | </return> |
81 | 122 | <description> |
| 123 | + Inverts the order of nodes in the list. |
82 | 124 | </description> |
83 | 125 | </method> |
84 | 126 | <method name="move_before"> |
|
89 | 131 | <argument index="1" name="before_node" type="ListNode"> |
90 | 132 | </argument> |
91 | 133 | <description> |
| 134 | + Moves a node [i]before[/i] the other one within the list. |
92 | 135 | </description> |
93 | 136 | </method> |
94 | 137 | <method name="move_to_back"> |
|
97 | 140 | <argument index="0" name="node" type="ListNode"> |
98 | 141 | </argument> |
99 | 142 | <description> |
| 143 | + Moves a node to the back of the list ([member back] node will point to [code]node[/code]). |
100 | 144 | </description> |
101 | 145 | </method> |
102 | 146 | <method name="move_to_front"> |
|
105 | 149 | <argument index="0" name="node" type="ListNode"> |
106 | 150 | </argument> |
107 | 151 | <description> |
| 152 | + Moves a node to the front of the list (the [member front] node will point to [code]node[/code]). |
108 | 153 | </description> |
109 | 154 | </method> |
110 | 155 | <method name="pop_back"> |
111 | 156 | <return type="void"> |
112 | 157 | </return> |
113 | 158 | <description> |
| 159 | + Erases the last node of the list. Make sure to preserve the [member ListNode.value] if you're interested in the data associated with the node: |
| 160 | + [codeblock] |
| 161 | + var value = list.back.value |
| 162 | + list.pop_back() |
| 163 | + [/codeblock] |
114 | 164 | </description> |
115 | 165 | </method> |
116 | 166 | <method name="pop_front"> |
117 | 167 | <return type="void"> |
118 | 168 | </return> |
119 | 169 | <description> |
| 170 | + Erases the first node of the list. Make sure to preserve the [member ListNode.value] if you're interested in the data associated with the node: |
| 171 | + [codeblock] |
| 172 | + var value = list.front.value |
| 173 | + list.pop_front() |
| 174 | + [/codeblock] |
120 | 175 | </description> |
121 | 176 | </method> |
122 | 177 | <method name="push_back"> |
|
125 | 180 | <argument index="0" name="value" type="Variant"> |
126 | 181 | </argument> |
127 | 182 | <description> |
| 183 | + Constructs a new [ListNode] and pushes it at the [i]end[/i] of the list. |
128 | 184 | </description> |
129 | 185 | </method> |
130 | 186 | <method name="push_front"> |
|
133 | 189 | <argument index="0" name="value" type="Variant"> |
134 | 190 | </argument> |
135 | 191 | <description> |
| 192 | + Constructs a new [ListNode] and pushes it at the [i]beginning[/i] of the list. |
136 | 193 | </description> |
137 | 194 | </method> |
138 | 195 | <method name="remove"> |
|
141 | 198 | <argument index="0" name="node" type="ListNode"> |
142 | 199 | </argument> |
143 | 200 | <description> |
| 201 | + Removes (and deletes) an existing node from the list. |
144 | 202 | </description> |
145 | 203 | </method> |
146 | 204 | <method name="size" qualifiers="const"> |
147 | 205 | <return type="int"> |
148 | 206 | </return> |
149 | 207 | <description> |
| 208 | + Returns the total number of nodes in the list. |
150 | 209 | </description> |
151 | 210 | </method> |
152 | 211 | <method name="sort"> |
153 | 212 | <return type="void"> |
154 | 213 | </return> |
155 | 214 | <description> |
| 215 | + Sorts the list in alphabetical order if the list contains [String]s. If the list contains nodes with different types of values, these are sorted according to the order of [enum @GlobalScope.Variant.Type]. |
156 | 216 | </description> |
157 | 217 | </method> |
158 | 218 | <method name="swap"> |
|
163 | 223 | <argument index="1" name="node_B" type="ListNode"> |
164 | 224 | </argument> |
165 | 225 | <description> |
| 226 | + Moves [code]node_A[/code] to the position of [code]node_B[/code], and moves [code]node_B[/code] to the original position of [code]node_A[/code]. If [code]node_A == node_B[/code], does nothing. |
166 | 227 | </description> |
167 | 228 | </method> |
168 | 229 | </methods> |
169 | 230 | <members> |
170 | 231 | <member name="back" type="ListNode" setter="" getter="get_back"> |
| 232 | + The last node in the list. Can be [code]null[/code] if the list is [method empty]. |
171 | 233 | </member> |
172 | 234 | <member name="front" type="ListNode" setter="" getter="get_front"> |
| 235 | + The first node in the list. Can be [code]null[/code] if the list is [method empty]. |
173 | 236 | </member> |
174 | 237 | </members> |
175 | 238 | <constants> |
|
0 commit comments