Singly-linked list/Element definition: Difference between revisions
Content added Content deleted
imported>Chinhouse |
imported>Chinhouse |
||
Line 983: | Line 983: | ||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
Implementing linked lists in MiniScript is an academic exercise. For practical applications, use the built-in list type. |
|||
<syntaxhighlight lang="miniscript"> |
<syntaxhighlight lang="miniscript"> |
||
Node = {"item": null, "next": null} |
Node = {"item": null, "next": null} |
||
Line 1,002: | Line 1,003: | ||
end if |
end if |
||
end function |
end function |
||
LinkedList.insert = function(aftItem, item) |
|||
newNode = Node.init(item) |
|||
cursor = self.head |
|||
while cursor.item != aftItem |
|||
print cursor.item |
|||
cursor = cursor.next |
|||
end while |
|||
newNode.next = cursor.next |
|||
cursor.next = newNode |
|||
end function |
|||
LinkedList.traverse = function |
|||
cursor = self.head |
|||
while cursor != null |
|||
// do stuff |
|||
print cursor.item |
|||
cursor = cursor.next |
|||
end while |
|||
end function |
|||
test = new LinkedList |
|||
test.append("A") |
|||
test.append("B") |
|||
test.insert("A","C") |
|||
test.traverse |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||