Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Zlatko Vid on February 04, 2025, 08:21:12 AM

Title: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 08:21:12 AM
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
Title: Re: How to create a Dynamic Array as stack
Post by: Charles Pegge on February 04, 2025, 09:23:57 AM
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
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 09:56:42 AM
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  ;)
Title: Re: How to create a Dynamic Array as stack
Post by: Charles Pegge on February 04, 2025, 10:01:24 AM
I've just changed it to use a dynamic string array :)
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:03:33 AM
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
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:06:24 AM
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
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:13:05 AM
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 ?
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:30:49 AM
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..."
Title: Re: How to create a Dynamic Array as stack
Post by: Charles Pegge on February 04, 2025, 10:31:58 AM
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.
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:33:02 AM
QuoteThis one builds upwards:

hmm yes ..sorry i have hard times to understand how
this should work properly ..especially under recursion
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:36:54 AM
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  :-[ )

Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:38:28 AM
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?
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:48:42 AM
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 ?
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 10:53:57 AM
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
Title: Re: How to create a Dynamic Array as stack
Post by: Charles Pegge on February 04, 2025, 02:17:32 PM
Typo in my code, sorry:

if stack<1
  'empty error
endif

should be

if stacki<1
  'empty error
endif
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 04, 2025, 03:54:54 PM
OK
i am confused what it is
but even is that still compile and ran,,,wow  :D
Title: Re: How to create a Dynamic Array as stack
Post by: Theo Gottwald on February 04, 2025, 08:44:26 PM
Here is the complete code for an Array that can be used as STACK,QUE,DEQUE and List .... in PB.
But should be easy to convert in times of Deepseek ....

Stans Datatypes (https://forum.it-berater.org/index.php/topic,6223.0.html)
Title: Re: How to create a Dynamic Array as stack
Post by: Zlatko Vid on February 05, 2025, 08:34:40 AM
sorry Theo but that one not compile as i already mention in the topic
also ..it is far from thematic about STACK ..and is too complex filled
with gui controls managing.
Title: Re: How to create a Dynamic Array as stack
Post by: Theo Gottwald on February 05, 2025, 09:03:53 AM
Yes, you are right, i have therefore just added the Original ZIP-File to the post, please take a look.