December 03, 2020, 08:31:14 PM

Author Topic: [bmx] Simple TStack by Galaxy613 [ 1+ years ago ]  (Read 647 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Simple TStack by Galaxy613 [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
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 :
Code: BlitzMax
  1. Type TStack
  2.         Field Top:TStackElement
  3.         Field Bottom:TStackElement
  4.         Field Depth% = 0
  5.        
  6.         Method Pop:Object()
  7.                 If Top = Null Then Return Null
  8.                 Local tmp:TStackElement = Top
  9.                 If Top <> Bottom Then
  10.                         Top = Top.Parent
  11.                 Else
  12.                         Top = Null
  13.                         Bottom = Null
  14.                 EndIf
  15.                 Depth:-1
  16.                 Return Tmp.Data
  17.         End Method
  18.        
  19.         Method Push(Data:Object)
  20.                 Local tmp:TStackElement = New TStackElement
  21.                 tmp.data = data
  22.                 tmp.parent = Top
  23.                 Top = tmp
  24.                 Depth:+1
  25.                 If Bottom = Null Then Bottom = tmp
  26.         End Method
  27. End Type
  28.  
  29. Type TStackElement
  30.         Field Parent:TStackElement
  31.         Field Data:Object
  32. End Type
  33.  
  34. ''' TStack Test
  35.  
  36. Global testStack:TStack = New TStack
  37.  
  38. testStack.Push "1"
  39. Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)
  40.  
  41. testStack.Push "2"
  42. Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)
  43.  
  44. testStack.Push "3"
  45. Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)
  46.  
  47. Local tmpStr$ = String testStack.Pop()
  48. Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)+" "+tmpStr
  49.  
  50. tmpStr$ = String testStack.Pop()
  51. Print (String testStack.Top.data)+" "+(String testStack.Bottom.data)+" "+tmpStr
  52.  
  53. tmpStr$ = String testStack.Pop()
  54. Print tmpStr
  55.  
  56. tmpStr$ = String testStack.Pop()


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal