TStack type

BlitzMax Forums/BlitzMax Tutorials/TStack type

yossi(Posted 2014) [#1]
as a former c# programmer I used a lot the TStack class to implement algorithms.

I get the impression that this type is missing from blitz max and therefore I wish to implement this type with blitz max and of curse give it to the community as a free open source when it ready.

before I start a project on this I want to make shore that it is not already exist as part of the language or as an external module (I don't wish to spend my time about something already exist).

so if one know about the existence of such a type in blitz max please let me know.


yossi(Posted 2014) [#2]
sorry.
I put it in the wrong place.
please forgive me.


jkrankie(Posted 2014) [#3]
It's missing the eachin method, but that's easy enough to add yourself (look in the docs or the linked list source code), but it's a stack...

Type stack
	Field First:stackNode = Null
	Field N:Int = 0
	
	Method New()
		First = Null
		N = 0
		Assert Check()
	End Method
	
	Method isEmpty:Int()
		Return First = Null
	End Method
	
	Method size:Int()
		Return N
	End Method
	
	Method push(o:Object)
		Local oldfirst:stackNode = First
		First = New stackNode
		First.obj = o
		First.nxt = oldfirst
		N:+1
		Assert check()
	End Method
	
	Method pop:Object()
		If isEmpty() Return Null
		Local o:Object = First.obj
		First = First.nxt
		N:-1
		Assert check()
		Return o
	End Method
	
	Method getFirst:Object()
		If isEmpty() Return Null
		Return First.obj
	End Method
	
	Method check:Int()
		If N = 0
			If First <> Null Return False
		ElseIf N = 1
			If First = Null Return False
			If First.nxt <> Null Return False
		Else
			If First.Nxt = Null Return False
		End If
		
		Local numNodes:Int = 0
		Local x:stackNode = First
		While x <> Null
			numNodes:+1
			x = x.nxt
		Wend
		If numNodes <> N Return False
		
		Return True
		
	End Method
	
EndType

Type stackIterator
	
	Field Current:stackNode

	Method Create:stackIterator(inS:stack)
		If inS.First = Null Return Null
		Current = inS.First
		Return Self
	End Method
	
	Method HasNext:Int()
		If Current.nxt <> Null Return True
		Return False
	End Method
	
	Method getNext:Object ()
		If Current.nxt = Null Return Null
		Current = Current.nxt
		Return Current.obj
	End Method
	
EndType

Private
Type stackNode
	Field obj:Object
	Field nxt:stackNode
End Type
Public


edit: the assert stuff can probably be commented out.

Cheers
Charlie