News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Queue - Data Structures

Started by kevin, January 14, 2006, 04:58:15 PM

Previous topic - Next topic

kevin

PlayBASIC Code: [Select]
; *=-------------------------------------------------------------------------=*
;
; This example shows how you can roll some functions to treat arrays like
; a QUEUE. In this example were using a 1D integer array, but you could use
; floats, strings or even typed arrays if you wanted.
;
; What is a QUEUE ?
; =================
;
; Conceptually, QUEUE's are simply a data storage method that allows adding
; and removing elements in a particular order. Every time an element is added,
; it goes on the end of the queue, the only elements that can be removed is the
; element that was at the FRONT of queue.
;
; Consequently, a queue is said to have
; "first in first out" behavior (or "last in, last out"). The first item added
; will be the first item removed from it.
;
; If if add the numbers 30,10,50.. They'd come back in the order they
; were adding. Ie 30-> then 10-> then 50
;
; *=-------------------------------------------------------------------------=*


Dim MyQueue(0)

Print "Adding Data to Queue"
For lp=0 to 10
; grab a random value to store on our MyQueue() array
ThisValue=rnd(1000)

; display the value so we can see it
print ThisValue

; add item to our queue
AddToQueue(MyQueue(),ThisValue)
next



SetCursor 0,200
print "Queue CONTENTS:"
print ShowQueue(MyQueue())


; Display the values from our queue
setcursor 0,250
Print "Queue values"

For lp=1 to QueueSize(MyQueue())
Print NextInQueue(MyQueue())
next


; show the contents after popping them
setcursor 0,420
print "Queue CONTENTS:"
print ShowQueue(MyQueue())


Sync
waitkey



; =-----------------------------------------------------------------=*
; Get the Size of this QUE array
; =-----------------------------------------------------------------=*


FUnction QueueSize(Array())
Size=GetArrayElements(Array(),1)
EndFunction Size




; =-----------------------------------------------------------------=*
; Push a Integer value onto our QUEUE array
; =-----------------------------------------------------------------=*


Function AddToQueue(Array(),ThisValue)
; Get the current size of this array plus 1
Size=GetArrayElements(Array(),1)+1

; redim it (resize) so it's one element bigger
redim Array(size)

; insert the new element at the end of array
array(Size)=ThisValue
EndFunction



; =-----------------------------------------------------------------=*
; Read the element at the head of the que
; =-----------------------------------------------------------------=*

Function NextInQueue(Array())
; Get the size of the array
Size=GetArrayElements(Array(),1)

; check if there's at least 1 item in the queue
if size>0

; grab the FIRST item at array index #1
ThisValue=Array(1)

; copy all the following cells Up. So now element
; 2 is in element 1's position and so on
CopyArrayCells array(),1,1,array(),0,1,size

; reduce the arrays size by 1 element
redim Array(size-1)
endif
EndFunction ThisValue



; =-----------------------------------------------------------------=*
; Build a string of the Queue's contents for displaying
; =-----------------------------------------------------------------=*

Function ShowQueue(Array())
Size=GetArrayElements(Array(),1)
if size>0
for lp=1 to size
s$=s$+str$(Array(lp))
if lp<>size then s$=s$+","
next
else
S$="Queue Is Empty"
endif
EndFunction s$











Adaz

I've never heard of Que, is this different than Queue?

Ádáz

Hungary

thaaks

No, it's not. A que is a queue is a queue.

I'm off, taking the last queue as a cue for some pool billiard  ;D