Page 1 of 1

String stack

Posted: Tue Jun 29, 2004 11:16 pm
by thefool
Hi!

Here is how to make use of stack for strings:

off course you dont need all that info about how a stack works, so just copy the procedures if interested..

Code: Select all

;String Stack 1.0
;Copyright(c) 2004 Daniel Middelhede  [THEFOOL]
;
;This is a simple code that makes
;an user available to use a stringstack.
;The stack works like the assembler stack.
;Used for fast switching of 2 strings and so.
;
;The stack uses the last in first out method.
;Explained in a graphic way:
;you push 2 strings, "test1" and "test2"
;
; |-----|
; |test1| 
; |test2| <---last in!
; |-----|
; 
;
; |-----|
; |test1| 
; |test2| <---the stackpointer works from bottom!
; |-----|
;
; So "test2" will get out first.
; 
; Procedures syntax(if you use it)
; 
; initSstack(SIZE.l)
; -initializes the stack and gives maximum
; 
; spush(STRING.s)
; -Pushes a string and moves stack pointer
;
; result.s=spop()
; -pops/retrieves a string from the stack

Procedure initStack(size.l)
Dim stack.s(size.l)
Global stackptr
stackptr=-1
EndProcedure

Procedure spush(string.s)
stackptr+1
stack(stackptr)=string.s
EndProcedure

Procedure.s spop()
popstr.s=stack(stackptr)
stackptr-1
ProcedureReturn popstr.s
EndProcedure

;Test code

;Initialises the stack.
initstack(5)
;Pushes 2 lines.
spush("line 1")
spush("line 2")
;Pops the 2 lines.
Debug spop()
Debug spop()
so here is the cut'n paste version:

Code: Select all

Procedure initStack(size.l)
Dim stack.s(size.l)
Global stackptr
stackptr=-1
EndProcedure

Procedure spush(string.s)
stackptr+1
stack(stackptr)=string.s
EndProcedure

Procedure.s spop()
popstr.s=stack(stackptr)
stackptr-1
ProcedureReturn popstr.s
EndProcedure
(same as above but without test code and explanation.)