Sourceforest
0
Q:

excel vba stack memory structure filo first in last out

'VBA does not include a native STACK (LIFO: LAST-In-FIRST-Out) memory 
'structure. It is possible to implement one using nothing but a native
'array or collection.

'However, the easiest solution is to have VBA create and use a .NET Stack:
Set stack = CreateObject("System.Collections.Stack")
Stack.Push "I"  
Stack.Push "was"  
Stack.Push "here."  
  
MsgBox Stack.Pop			'<--displays: here.
'Note:  Pop() removes the top (last-added) item from the Stack.
  
'  		Peek() will return the top value without removing it:
MsgBox Stack.Peek  			'<--displays: was

'Stack interrogation:  
MsgBox Stack.Count			'<--displays: 2.  Count() returns 0 if Stack empty.
MsgBox Stack.Contains("I")	'<--displays: True

'Stack to 1D VBA array (zero based):
arr = Stack.toArray
MsgBox arr(0)				'<--displays: I   Stack is unaltered.
  
'Clone the Stack:
Set Stack2  = Stack.Clone	'<--Stack2 is not affected by ops on Stack.
    
'Empty the Stack:
Stack.Clear  				'<--Stack now empty, but can take a new Push().
    
    
'NB:	Stacks can hold simple values and also complex objects.
    
'Reference: 
'    https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack
 
'FILO and LIFO are interchangeable and mean the same thing.
10
'VBA does not include a native QUEUE (FIFO: FIRST-In-FIRST-Out) memory 
'structure. It is possible to implement AN a QUEUE using nothing but a native
'array or collection.

'However, the easiest solution is to have VBA create and use a .NET Queue:
Set Queue = CreateObject("System.Collections.Queue")
Queue.Enqueue "I"  
Queue.Enqueue "was"  
Queue.Enqueue "here."  
  
MsgBox Queue.Dequeue		'<--displays: I
'Note:  Dequeue() removes the bottom (first-added) item from the Queue.
  
'  		Peek() will return the bottom value without removing it:
MsgBox Queue.Peek  			'<--displays: was

'Queue interrogation:  
MsgBox Queue.Count			'<--displays: 2.  Count() returns 0 if Stack empty.
MsgBox Queue.Contains("I")	'<--displays: False

'Queue to 1D VBA array (zero based):
arr = Queue.toArray
MsgBox arr(0)				'<--displays: was   Queue is unaltered.
  
'Clone the Queue:
Set Queue2  = Queue.Clone	'<--Queue is not affected by ops on Stack.
    
'Empty the Queue:
Queue.Clear  				'<--Stack now empty, but can take a new Push().
    
    
'NB:	Queues can hold simple values and also complex objects.
    
'Reference: 
'    https://docs.microsoft.com/en-us/dotnet/api/system.collections.queue
 
4

New to Communities?

Join the community