SyntaxBomb - Indie Coders
Languages & Coding => Blitz Code Archives => Algorithms => Topic started by: BlitzBot on June 29, 2017, 00:28:38
Title : Simple TStack
Author : Galaxy613
Posted : 1+ years ago
Description : I just needed a simple way to adding objects to a list and then when I take one off the top, it goes down a level... then I remember stacks, and found out that BlitzMax doesn't have them! I think a few others have submitted their version of doing stacks, but this is how I did it...
Code :
Type TStack
Field Top:TStackElement
Field Bottom:TStackElement
Field Depth% = 0
Method Pop:Object()
If Top = Null Then Return Null
Local tmp:TStackElement = Top
If Top <> Bottom Then
Top = Top.Parent
Else
Top = Null
Bottom = Null
EndIf
Depth:-1
Return Tmp.Data
End Method
Method Push(Data:Object)
Local tmp:TStackElement = New TStackElement
tmp.data = data
tmp.parent = Top
Top = tmp
Depth:+1
If Bottom = Null Then Bottom = tmp
End Method
End Type
Type TStackElement
Field Parent:TStackElement
Field Data:Object
End Type
''' TStack Test
Global testStack:TStack = New TStack
testStack.Push "1"
Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)
testStack.Push "2"
Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)
testStack.Push "3"
Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)
Local tmpStr$ = String testStack.Pop()
Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)+" "+tmpStr
tmpStr$ = String testStack.Pop()
Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)+" "+tmpStr
tmpStr$ = String testStack.Pop()
Print tmpStr
tmpStr$ = String testStack.Pop()
Comments : none...