Stack: Difference between revisions

998 bytes added ,  5 months ago
Added GDScript implementation.
m (BBC BASIC moved to the BASIC section.)
imported>LongPtrCall
(Added GDScript implementation.)
Line 3,469:
top value: four
four popped. stack: [3]
</pre>
 
=={{header|GDScript}}==
In GDScript there is built-in Array class, that implements either 'push', 'pop', 'top' and 'empty' methods. Method names are:
<ul>
<li>push -> push_back</li>
<li>pop -> pop_back</li>
<li>top -> back</li>
<li>empty -> is_empty</li>
</ul>
<syntaxhighlight lang="gdscript">
extends Node2D
 
func _ready() -> void:
# Empty stack creation.
var stack : Array = []
# In Godot 4.2.1 nothing happens here.
stack.pop_back()
if stack.is_empty():
print("Stack is empty.")
stack.push_back(3)
stack.push_back("Value")
stack.push_back(1.5e32)
print(stack)
print("Last element is: " + str(stack.back()))
stack.pop_back()
print(stack)
print("Last element is: " + str(stack.back()))
if not stack.is_empty():
print("Stack is not empty.")
return
</syntaxhighlight>
{{out}}
<pre>
Stack is empty.
[3, "Value", 149999999999999999042044051849216]
Last element is: 149999999999999999042044051849216
[3, "Value"]
Last element is: Value
Stack is not empty.
</pre>
 
Anonymous user