How to create a Dynamic Array as stack

Started by Zlatko Vid, February 04, 2025, 08:21:12 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Zlatko Vid

HI Charles
I ran trough all examples and don't find
anything similar to stack array ?
any example maybe ?

ps:oups
there is one for strings but that one is OOP
https://forum.it-berater.org/index.php/topic,6336.msg27157.html#msg27157
Charles ..is there something simplier ..without OOP?
thanks

Charles Pegge

#1
Hi Aurel,

All local variables use the stack, so it is possible to create small fixed local arrays. (the stack is normally  only 1meg). It will disappear when you exit the function.

Dynamic Arrays can also be created locally, but you will need to del them before exiting the function.

'LOCAL DYNAMIC ARRAY
function ff()
  int i=200
  redim int aa(i) 'local by default
  'redim int aa(i) static
  aa[50]=123
  '
  'testing resize
  i=70
  redim int aa(i)        'elements conserved by default
  'redim int aa(i) clear 'all elements set to null
  print aa[50]
  del aa
end function
ff()


PS: I think you meant create your own stack:


This one builds upwards:
int StackMax=1000
redim string stack[StackMax]
int stacki=1

function PushStack(string v)
if stacki>StackMax
  'overflow error or redimension
  'or
  'StackMax+=1000
  'redim string Stack(StackMax)
endif
stack[stacki]=v
stacki++
end function
'
function PopStack() as string
if stack<1
  'empty error
endif 
stacki--
return stack(stacki)
end function
'
'TEST:
pushStack 1
pushStack 2
pushStack 3
print PopStack '3
print PopStack '2
print PopStack '1

del stack 'release memory

Zlatko Vid

#2
Hi
QuoteDynamic Arrays can also be created locally, but you will need to del them before exiting the function.

yeah..i like to avoid this method because i will probably forget del and make mess.
Also redim probably eat processing time (i guess)  ::)

QuotePS: I think you meant create your own stack:

Yes Charles
I will test your code  ;)

Charles Pegge

I've just changed it to use a dynamic string array :)

Zlatko Vid

Charles
code compile and work but why is here stack variable instead array
function PopStack() as int
if stack < 1  ' how this work ? should be array ?
  'empty error
endif
stacki--
return stack[stacki]
end function

Zlatko Vid

QuoteI've just changed it to use a dynamic string array

Oh you are too quick  :D

Ok i will try that one to, but i prefer to avoid redim

Zlatko Vid

In fact i need this operation for my interpreter
and looks to me too simple
do you agree that i need stackPointer ?
and stackFrame to hold last function params values ?

Zlatko Vid

#7
Hi Charles
I tried this , i think is not very good example
because i don't use Peek() function
'create stack using array (...of integers) o2 by Aurel 13.1.2025
string crlf = chr(13) + chr(10), stbuff
int stack[16] ,size = 16
int top = 0
! push(int i)
! pop()  as int
! peek() as intint StackMax=1000
redim string stack[StackMax]
int stacki=1

function PushStack(string v)
if stacki>StackMax
  'overflow error or redimension
  'or
  'StackMax+=1000
  'redim string Stack(StackMax)
endif
stack[stacki]=v
stacki++
end function
'
function PopStack() as string
if stacki<1
  'empty error
endif
stacki--
return stack(stacki)
end function
'
'TEST:
pushStack 1
pushStack 2
pushStack 3
print PopStack '3
print PopStack '2
print PopStack '1

del stack 'release memory


sub push(int a)
    if top = size - 1
       print "Stack is Overflow!" : goto exitProgram
    else
      top = top + 1      ' increment stack index
      stack[top] = a     ' enter value
    end if
end sub

sub pop()
    if top = 0
       print "Stack is Underflow!" : goto exitProgram
    else
      stack[top] = 0  ' remove curent value
      top = top - 1   ' decrement stack index
    end if 
end sub

sub printStack()
int i
    for i = 1 to top
        stbuff = stbuff + str(i) + " : " + str(stack[i]) + crlf
    next i
'show stack
print stbuff
end sub

'push some values on the stack
int v=1,x=10
while v < 11
   push(x)
   x = x + 10
   v=v+1
wend

'test 1......
printStack()

exitProgram:
print "EXIT..."

Charles Pegge

Referring to the last example:

You can change redim into dim if you want a fixed stack size.

Stacki performs the role of a stack pointer but it is really just the stack index.

You dont need to use functions to push/pop your stack, just adjust Stacki.

Zlatko Vid

QuoteThis one builds upwards:

hmm yes ..sorry i have hard times to understand how
this should work properly ..especially under recursion

Zlatko Vid

QuoteYou can change redim into dim if you want a fixed stack size.

Stacki performs the role of a stack pointer but it is really just the stack index.

You dont need to use functions to push/pop your stack, just adjust Stacki.

Ok Charles
i think that i now understand(still not very sure  :-[ )


Zlatko Vid

Also i don't get it how this work without array brackets

if stack < 1   ' how this work ? should be array ?
i think that should be

if stack[] < 1  ...is that have any sense?

Zlatko Vid

Charles
I would like to refer to first example
this one :
int stack[1000]  ' is array
int stacki=1

function PushStack(int v)
if stacki > 999
  'overflow error
endif
stack[stacki] = v
stacki++
end function
'
function PopStack() as int
if stack[] < 1   ' how this work ? should be array ?
  'empty error
endif
stacki--
return stack[stacki]
end function
'
'TEST:
pushStack 1
pushStack 2
pushStack 3
print PopStack '3
print PopStack '2
print PopStack '1

...my  question would be how to push it in reverse order?
so that we can read in right order ?

Zlatko Vid

I mean is this code fine ?
int stack[1000]  ' is array
int stacki=1

function PushStack(int v)
if stacki > 999
  'overflow error
endif
stack[stacki] = v
stacki++
end function
'
function PopStack() as int
if stack[] < 1   ' how this work ? should be array ?
  'empty error
endif
stacki--
return stack[stacki]
end function
'
'TEST:
int n,size=3
n=size
pushStack (n)   : n=n-1
pushStack (n)   : n=n-1
pushStack (n)   : ' n=1
print PopStack '3
print PopStack '2
print PopStack '1

Charles Pegge

Typo in my code, sorry:

if stack<1
  'empty error
endif

should be

if stacki<1
  'empty error
endif