I'm trying to make a stack type data structure where one stack can hold values of multiple types. I have this code, but what i want only works with a push and pop function for EVERY type. Can someone help me either find a better way or to optomize this code?
s:stack=New stack
s.pushbyte(100)
s.pushshort(20000)
s.pushint(300000)
s.pushlong(4000000)
s.pushfloat(5.005)
s.pushdouble(6.00006)
s.pushstring("YEEHAW!!!")
s.pushobject(s)
Print String(s.popobject()=s)
Print String(s.popstring())
Print String(s.popdouble())
Print String(s.popfloat())
Print String(s.poplong())
Print String(s.popint())
Print String(s.popshort())
Print String(s.popbyte())
End
Type stack
Field _head:block
'*****PUSH*****'
Method pushbyte:block(value:Byte)
b:block=New block
b._vbyte=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushshort:block(value:Short)
b:block=New block
b._vshort=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushint:block(value:Int)
b:block=New block
b._vint=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushlong:block(value:Long)
b:block=New block
b._vlong=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushfloat:block(value:Float)
b:block=New block
b._vfloat=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushdouble:block(value:Double)
b:block=New block
b._vdouble=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushstring:block(value:String)
b:block=New block
b._vstring=value
b._fore=_head
_head=b
Return b
EndMethod
Method pushobject:block(value:Object)
b:block=New block
b._vobject=value
b._fore=_head
_head=b
Return b
EndMethod
'*****POP*****'
Method popbyte:Byte()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vbyte:Byte=b._vbyte
Release b
Return vbyte
EndMethod
Method popshort:Short()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vshort:Short=b._vshort
Release b
Return vshort
EndMethod
Method popint:Int()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vint:Int=b._vint
Release b
Return vint
EndMethod
Method poplong:Long()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vlong:Long=b._vlong
Release b
Return vlong
EndMethod
Method popfloat:Float()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vfloat:Byte=b._vfloat
Release b
Return vfloat
EndMethod
Method popdouble:Double()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vdouble:Double=b._vdouble
Release b
Return vdouble
EndMethod
Method popstring:String()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vstring:String=b._vstring
Release b
Return vstring
EndMethod
Method popobject:Object()
If _head=Null Then Return Null
b:block=_head
_head=_head._fore
vobject:Object=b._vobject
Release b
Return vobject
EndMethod
EndType
Type block
Field _vbyte:Byte
Field _vshort:Short
Field _vint:Int
Field _vlong:Long
Field _vfloat:Float
Field _vdouble:Double
Field _vstring:String
Field _vobject:Object
Field _fore:block
EndType
Yes, it is an eyesore, but I can't figure any way around it. Please any help is greatly appreciated!
|