
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


' FB 1.05.0 Win64
 
#Include "queue_rosetta.bi"  '' include macro-based generic Queue type used in earlier task
 
Declare_Queue(String) '' expand Queue type for Strings
 
Dim stringQueue As Queue(String)
With stringQueue  '' push some strings into the Queue
  .push("first")
  .push("second")
  .push("third")
  .push("fourth")
  .push("fifth")
End With
Print "Number of Strings in the Queue :" ; stringQueue.count
Print "Capacity of string Queue       :" ; stringQueue.capacity
Print
' now pop them
While Not stringQueue.empty
  Print stringQueue.pop(); " popped"
Wend
Print
Print "Number of Strings in the Queue :" ; stringQueue.count
Print "Capacity of string Queue       :" ; stringQueue.capacity   '' capacity should be unchanged
Print "Is Queue empty now             : "; stringQueue.empty
Print
Print "Press any key to quit"
Sleep