PSUEDO STACK IN BASIC

All aspects of programming on the Commander X16.
Post Reply
User avatar
ahenry3068
Posts: 1195
Joined: Tue Apr 04, 2023 9:57 pm

PSUEDO STACK IN BASIC

Post by ahenry3068 »

Many useful algorithms use recursion. There are work arounds
but its difficult in a language without variables that are local to a routine
to aid in these things it's useful to set up a "Faux/Psuedo stack" using an array

The following code hasn't been tested but its a pretty simple concept
I think it could be useful....

Let me know if anyone finds Bugs.... I'm very open to advice.

Code: Select all

REM IMPLEMENT A PSUEDO STACK IN BASIC
REM USEFUL FOR IMPLEMENTING RECURSIVE GOSUBS.
REM TO "PUSH" A VALUE -->  PU=Value:GOSUB 3000
REM TO "POP" A VALUE -->   GOSUB 3500: Value=PO
REM FOR THIS TO WORK THE FOLLOWING VARIABLE NAMES 
REM ARE RESERVED   SS,SP,PL,PO,PU

10 SS = 200 : REM SIZE OF STACK
15 SP = 0       : REM STACK POINTER
20 DIM PLATES(SS): REM THE STACK

3000 SP = SP + 1
3010 IF SP <= SS THEN 3050
3020 PRINT "PSEUDO STACK OVERFLOW....  CHECK YOUR CODE":END
3050 PLATES(SP) = PU  :  REM SET PU TO THE VARIABLE TO PUSH
3060 RETURN

3500 PO = PLATES(SP)
3510 SP = SP - 1
3520 IF SP >=0 THEN RETURN
3530 PRINT "CAN'T POP FROM PSEUDO STACK WHEN EMPTY..CHECK YOUR CODE":END
  
User avatar
ahenry3068
Posts: 1195
Joined: Tue Apr 04, 2023 9:57 pm

Re: PSUEDO STACK IN BASIC

Post by ahenry3068 »

At some point I'm going to use this code to implement QuickSort.
User avatar
ahenry3068
Posts: 1195
Joined: Tue Apr 04, 2023 9:57 pm

Re: PSUEDO STACK IN BASIC

Post by ahenry3068 »

I have since tested the code... Seems to accomplish it's task.
Post Reply