
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
 Illustration of FIFO behavior
Task

Create a queue data structure and demonstrate its operations.

(For implementations of queues, see the FIFO task.)



Operations:

   push       (aka enqueue) - add element
   pop         (aka dequeue) - pop first element
   empty     - return truth value when empty


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


sub push(x$)
    queue$ = queue$ + x$ + "#"
end sub
 
sub pop$()
    local i, r$
 
    if queue$ <> "" then
        i = instr(queue$, "#")
        if i then
            r$ = left$(queue$, i-1)
            stack$ = right$(queue$, len(queue$) - i)
        else
            r$ = queue$
            queue$ = ""
        end if
        return r$
    else
        print "--Queue is empty--"
    end if
end sub
 
sub empty()
    return queue$ = ""
end sub
 
// ======== test ========
 
for n = 3 to 5
    print "Push ", n : push(str$(n))
next
 
print "Pop ", pop$()
 
print "Push ", 6 : push(str$(6))
 
while(not empty())
    print "Pop ", pop$()
wend
 
print "Pop ", pop$()