Code archives/Algorithms/Simple TStack

This code has been declared by its author to be Public Domain code.

Download source code

Simple TStack by Galaxy6132010
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...
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.

Code Archives Forum