Type TSTACKNODE
    As long iParam
    As TSTACKNODE Ptr pNext = 0
End Type

Type TSTACK
    Declare Sub push(As long)
    Declare Function pop() As long
    Declare Function front() As long
    declare sub freestack()
    As Long iSize = 0
    As TSTACKNODE Ptr pFirst = 0
End Type

Sub TSTACK.push(iParam As long)
    Dim As TSTACKNODE Ptr pTemp = New TSTACKNODE
    if pTemp then
		pTemp->iParam = iParam
		If pFirst = 0 Then
			pFirst = pTemp
			iSize = 1
		Else
			pTemp->pNext = pFirst
			pFirst = pTemp
			iSize += 1
		Endif
    endif
End Sub

Function TSTACK.pop() As long
    If iSize Andalso pFirst Then
        Dim As TSTACKNODE Ptr p = pFirst->pNext
        Function = pFirst->iParam
        Delete(pFirst)
        pFirst = p
        iSize -=1
    else
        return -2
    Endif
End Function

sub TSTACK.freestack()
    If iSize Then
        Dim As TSTACKNODE Ptr p
        while pFirst
            p = pFirst->pNext
            Delete(pFirst)
            pFirst = p
        Wend
        iSize = 0
    Endif
End Sub

function NewStackFreeIndexes() as any ptr
    dim p as TSTACK ptr = new TSTACK
    return p
End Function

sub PushStackFreeIndexes(p as any ptr , iValue as Long)
    if p then
        cast(TSTACK ptr , p)->push(iValue)
    EndIf
End Sub

function PopStackFreeIndexes(p as any ptr) as Long
    if p then
        return cast(TSTACK ptr , p)->pop()
    EndIf
End Function

function SizeStackFreeIndexes(p as any ptr) as Long
    if p then
        return cast(TSTACK ptr , p)->iSize
    EndIf
End Function

sub FreeStackFreeIndexes(p as any ptr)
    if p then
        cast(TSTACK ptr , p)->freestack()
    EndIf
End Sub

sub FreeStackPointer(p as any ptr)
    if p then
        delete cast(TSTACK ptr , p)
    EndIf
End Sub