Push and Pop

BlitzMax Forums/BlitzMax Beginners Area/Push and Pop

vinians(Posted 2012) [#1]
Im needing to store some objetcts in a stack. There are any stack data structure in BlitzMax ? And if not what's the fast way to create one?
Thanks in advance.


ziggy(Posted 2012) [#2]
A linked list can be used as a stack, you have:
RemoveLast() This method returns and removes last item in the list.
RemoveFirst() This method retuns and removes first item in the list.
AddFirst(object) This method adds an item to the begining of the list.
AddLast(object) This method adds an item to the end of the list.

So all in all, you have a very versatile LIFO FIFO stack, use at your own pleasure. It also allows you to iterate through list contents.
There's only one slow operation, which is counting the elements of te list, as being a linked list, it implies iterating all the list items one by one.

Last edited 2012


Kryzon(Posted 2012) [#3]
I don't have the source right now to verify, but are you sure it iterates all items?

It'd be much easier to just add +1 everytime you Add an item, and -1 everytime a Remove is called, the length starting with zero.
Then the length would be ready to query.

Last edited 2012


jsp(Posted 2012) [#4]
Yes, it iterates all items.
So it depends on how many items you store and how often you need the count to better do the + - 1 manually.


ima747(Posted 2012) [#5]
TLists do indeed iterate when you ask for their count. It would be very easy to alter it to use an internal count reference but I don't know if that might have some additional impact somewhere (I doubt it but am not comfortable saying it's not possible...). Adding a counter reference would remove the overhead of counting the list, but it would add overhead (all be it very trivial since you're just incrementing an int essentially...) to adding/removing objects. It would also place a limit on the maximum size of a TList since crossing the maximum boundry for the iterator value type would cause problems, this likely wouldn't be a problem for 99.9% of things (who makes lists that long, it's the objects are likely going to eat all your memory and interacting with it would take forever...) but it would be a change in behavior over the current unlimited lists...

Best I think would be to make an extended TList type (perhaps TListFast...) that adds a counter...

You could also manually manage a count yourself if you want and never have to call Count since you'd have the count handy... possibly just put a TList into an Object with a counter...


vinians(Posted 2012) [#6]
Thanks guys I will try doing it with TList.


Oddball(Posted 2012) [#7]
It would be very easy to alter it to use an internal count reference but I don't know if that might have some additional impact somewhere (I doubt it but am not comfortable saying it's not possible...).
The reason TLists don't have an internal counter is because TLinks don't know which list they are associated with. This means if you used a TLinks Remove method it wouldn't be able to update the counter and its value would be wrong. I explained it here when someone tried to add a counter to TLists.


col(Posted 2012) [#8]
No doubt the easiest way is to use the TList thats in BMax already.

Just in case for some reason you don't want to, here's a TStack type and some wrapper functions for you :-



And some quick example usage:-



Or use the TStack type methods directly:-



Last edited 2012