|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > QUE. - Queue > QUE. - Queue |
MiniRobotLanguage (MRL)
QUE.- Operation Commands
Fill and empty the Queue's
Overview
The QUE.-Operation commands allow you to interact with and manipulate the contents of a queue.
A queue is a First-In-First-Out (FIFO) data structure, meaning the first element added to the queue is the first one to be removed.
These commands enable you to add, retrieve, and remove elements from the queue, as well as perform other operations like checking the queue's type or clearing its contents.
A handle is a unique identifier that references a specific queue. It is generated when a queue is created using QUE.New and is used to interact with the queue in subsequent commands. Handles are essential for managing multiple queues simultaneously. Once a queue is deallocated using QUE.End or QUE.EndAll, its handle becomes invalid.
A queue is a linear data structure that stores elements in a specific order. It follows the First-In-First-Out (FIFO) principle, meaning:
•The first element added to the queue is the first one to be removed.
•Elements are added to the end of the queue and removed from the front.
•Queues are commonly used for:
•Task scheduling (e.g., processing tasks in the order they are received).
•Buffering data (e.g., holding data temporarily before processing).
•Managing sequences of operations or events.
A handle is a unique identifier that references a specific queue. It is generated when a queue is created using QUE.New and is used to interact with the queue in subsequent commands. Handles are essential for managing multiple queues simultaneously. Once a queue is deallocated using QUE.End or QUE.EndAll, its handle becomes invalid.
This script demonstrates how to create a queue, add elements to it, retrieve elements, and deallocate the queue.
'***********************************
' QUE.-Operation Sample 1: Basic Operations
'***********************************
QUE.New|$$QUE
DBP.New queue created with this Handle: $$QUE
' Add elements to the queue
QUE.Push|$$QUE|42
QUE.Push|$$QUE|"Hello"
QUE.Push|$$QUE|3.14
' Check the number of elements in the queue
QUE.Count|$$QUE|$$COUNT
DBP.Number of elements in queue: $$COUNT
' Retrieve and remove the first element
QUE.Pop|$$QUE|$$VAL
DBP.Value popped from queue: $$VAL
' Retrieve the first element without removing it
QUE.Peek|$$QUE|$$VAL
DBP.First value in queue (after pop): $$VAL
' Clear the queue
QUE.Clear|$$QUE
DBP.Queue cleared.
' Deallocate the queue
QUE.End|$$QUE
DBP.Queue deallocated.
ENR.
This script demonstrates how to create and manage multiple queues with different data types.
'***********************************
' QUE.-Operation Sample 2: Multiple Queues
'***********************************
' Create an integer queue
QUE.New|$$QUE_INT|i
QUE.Push|$$QUE_INT|10
QUE.Push|$$QUE_INT|20
' Create a floating-point queue
QUE.New|$$QUE_FP|f
QUE.Push|$$QUE_FP|1.5
QUE.Push|$$QUE_FP|2.7
' Create a string queue
QUE.New|$$QUE_STR
QUE.Push|$$QUE_STR|"First"
QUE.Push|$$QUE_STR|"Second"
' Retrieve and display elements from each queue
QUE.Pop|$$QUE_INT|$$VAL
DBP.Popped from integer queue: $$VAL
QUE.Pop|$$QUE_FP|$$VAL
DBP.Popped from floating-point queue: $$VAL
QUE.Pop|$$QUE_STR|$$VAL
DBP.Popped from string queue: $$VAL
' Deallocate all queues
QUE.EndAll
DBP.All queues deallocated.
ENR.
This script demonstrates how to validate a queue handle and check the queue's data type.
'***********************************
' QUE.-Operation Sample 3: Validation and Type Checking
'***********************************
QUE.New|$$QUE|i
QUE.Push|$$QUE|42
' Validate the queue handle
QUE.Validate|$$QUE|$$RES
DBP.Queue validation result: $$RES
' Check the queue type
QUE.GetType|$$QUE|$$TYPE
DBP.Type of queue: $$TYPE (Expected: 1 - Integer)
' Deallocate the queue
QUE.End|$$QUE
DBP.Queue deallocated.
ENR.
This script demonstrates how to delete the first element in a queue.
'***********************************
' QUE.-Operation Sample 4: Deleting Elements
'***********************************
QUE.New|$$QUE
QUE.Push|$$QUE|"First"
QUE.Push|$$QUE|"Second"
QUE.Push|$$QUE|"Third"
' Delete the first element
QUE.Delete|$$QUE
' Retrieve the new first element
QUE.Peek|$$QUE|$$VAL
DBP.First value after deletion: $$VAL
' Deallocate the queue
QUE.End|$$QUE
DBP.Queue deallocated.
ENR.
This script demonstrates how to clear a queue and reuse it for new elements.
'***********************************
' QUE.-Operation Sample 5: Clearing and Reusing
'***********************************
QUE.New|$$QUE
QUE.Push|$$QUE|Old1
QUE.Push|$$QUE|Old2
' Clear the queue
QUE.Clear|$$QUE
DBP.Queue cleared.
' Add new elements
QUE.Push|$$QUE|New1
QUE.Push|$$QUE|New2
' Retrieve and display the first element
QUE.Peek|$$QUE|$$VAL
DBP.First value after reuse: $$VAL
' Deallocate the queue
QUE.End|$$QUE
DBP.Queue deallocated.
ENR.
The QUE.-Operation commands provide powerful tools for working with queues.
Whether you're managing task schedules, buffering data, or processing sequences, these commands allow you to efficiently manipulate queue data.
Use the examples above as a starting point for integrating queues into your scripts.
See also:
•