Singly-linked list/Element insertion: Difference between revisions
Content added Content deleted
imported>Chinhouse No edit summary |
imported>Chinhouse |
||
Line 1,548: | Line 1,548: | ||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
We're choosing here to use the built-in list type, rather than make our own from scratch, since this is more representative of how one is likely to actually use MiniScript. |
|||
<syntaxhighlight lang="miniscript"> |
<syntaxhighlight lang="miniscript"> |
||
> myList = [100, 101, 102] |
|||
LinkedList = {"head": null, "tail": null} |
|||
> myList.push 103 |
|||
LinkedList.append = function(item) |
|||
[100, 101, 102, 103] |
|||
newNode = Node.init(item) |
|||
> myList.insert 0, 99 |
|||
if self.head == null then |
|||
[99, 100, 101, 102, 103] |
|||
self.head = newNode |
|||
> myList.insert 3,101.5 |
|||
self.tail = self.head |
|||
[99, 100, 101, 101.5, 102, 103] |
|||
else |
|||
⚫ | |||
self.tail.next = newNode |
|||
self.tail = self.tail.next |
|||
end if |
|||
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 |
|||
test = new LinkedList |
|||
test.append("A") |
|||
test.append("B") |
|||
test.insert("A","C") |
|||
⚫ | |||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
<syntaxhighlight lang="modula3">MODULE SinglyLinkedList EXPORTS Main; |
<syntaxhighlight lang="modula3">MODULE SinglyLinkedList EXPORTS Main; |