RosettaCodeData/Task/Stack/Run-BASIC/stack.basic

42 lines
1.0 KiB
Plaintext

dim stack$(10) ' stack of ten
global stack$
global stackEnd
for i = 1 to 5 ' push 5 values to the stack
a$ = push$(chr$(i + 64))
print "Pushed ";chr$(i + 64);" stack has ";stackEnd
next i
print "Pop Value:";pop$();" stack has ";stackEnd ' pop last in
print "Pop Value:";pop$();" stack has ";stackEnd ' pop last in
e$ = mt$() ' MT the stack
print "Empty stack. stack has ";stackEnd
' ------ PUSH the stack
FUNCTION push$(val$)
stackEnd = stackEnd + 1 ' if more than 10 then lose the oldest
if stackEnd > 10 then
for i = 0 to 9
stack$(i) = stack$(i+1)
next i
stackEnd = 10
end if
stack$(stackEnd) = val$
END FUNCTION
' ------ POP the stack -----
FUNCTION pop$()
if stackEnd = 0 then
pop$ = "Stack is MT"
else
pop$ = stack$(stackEnd) ' pop last in
stackEnd = max(stackEnd - 1,0)
end if
END FUNCTION
' ------ MT the stack ------
FUNCTION mt$()
stackEnd = 0
END FUNCTION