Adam Glauser wrote:
Thanks for bringing that up ... this could be a really nice way to 
implement queues and stacks of fixed-length elements.
Shifting the entire array each time could be punishing for performance 
depending on the size of each element.
For a stack, it's easy to add new elements at the end, and just 
decrement the number of elements to pop them off.
For a queue, you can keep a circular array, where the logical first 
element is not necessarily arr(1).  arr(1) may logically follow arr(max).
Example: arr has 20 actual elements
  Actual values are cde...............ab
    arrFirst = 19
    arrNum = 5
    arrLast = 3
  Logical values are abcde
Remove an element from the beginning of the queue:
   arrFirst += 1
   if arrFirst > %elem(arr)
      arrFirst = 1
Add an element to the end of the queue
   arrNum += 1
   if arrNum > %elem(arr)
      error
   arrLast += 1
   if arrLast > %elem(arr)
      arrLast = 1
   arr(arrLast) = newvalue
Process the queue in order
   i = arrFirst
   for x = 1 to arrNum
      process(arr(i))
      i += 1
      if i > %elem(arr)
         i = 1
   endfor
As an Amazon Associate we earn from qualifying purchases.
	
 
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.