
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

Implement a FIFO queue.

Elements are added at one side and popped from the other in the order of insertion.



Operations:

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


Errors:

   handle the error of trying to pop from an empty queue (behavior depends on the language and platform)


See

   Queue/Usage   for the built-in FIFO or queue of your language or standard library.


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


 0  DEF  FN E(MPTY) = SP = FIRST
 10  GOSUB 150EMPTY
 20  LET A$ = "A": GOSUB 100PUSH
 30  LET A$ = "B": GOSUB 100PUSH
 40  GOSUB 150EMPTY
 50  GOSUB 120PULL FIRST
 60  GOSUB 120PULL FIRST
 70  GOSUB 150EMPTY
 80  GOSUB 120PULL FIRST
 90  END
 100  PRINT "PUSH "A$
 110  LET S$(SP) = A$:SP = SP + 1: RETURN
 120  GOSUB 130: PRINT "POP "A$: RETURN
 130  IF  FN E(0) THEN  PRINT "POPPING FROM EMPTY QUEUE";: STOP
 140  A$ = S$(FI): FI = FI + 1 : RETURN
 150  PRINT "EMPTY? " MID$ ("YESNO",4 ^  FN E(0),3): RETURN