Singly-linked list/Element insertion: Difference between revisions

imported>Chinhouse
No edit summary
imported>Chinhouse
Line 1,548:
 
=={{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">
> 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
</syntaxhighlight>
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")
</syntaxhighlight>
=={{header|Modula-3}}==
<syntaxhighlight lang="modula3">MODULE SinglyLinkedList EXPORTS Main;
Anonymous user