Section9.64a

]>
Book Contents Previous Section 9.64 Quaternions Next Section 9.65 RadixExpansion Book Index

## 9.64a Queue

A queue is an aggregate structure which allows insertion at the back of the queue, deletion at the front of the queue and inspection of the front element. Queues are similar to a line of people where you can join the line at the back, leave the line at the front, or see the person in the front of the line.

Queues can be created from a list of elements using the queue Queue function.

a:Queue INT:= queue [1,2,3,4,5]

[ 1 , 2 , 3 , 4 , 5 ]

Type: Queue Integer

An empty queue can be created using the empty function.

a:Queue INT:= empty()

[ ]

Type: Queue Integer

The empty? function will return true if the queue contains no elements.

empty? a

true

Type: Boolean

Queues modify their arguments so they use the exclamation mark "!" as part of the function name.

The dequeue! operation removes the front element of the queue and returns it. The queue is one element smaller. The extract! does the same thing with a different name.

a:Queue INT:= queue [1,2,3,4,5]

[ 1 , 2 , 3 , 4 , 5 ]

Type: Queue Integer
dequeue! a

1

Type: PositiveInteger
a

[ 2 , 3 , 4 , 5 ]

Type: Queue Integer

The enqueue! function adds a new element to the back of the queue and returns the element that was pushed. The queue is one element larger. The insert! function does the same thing with a different name.

a:Queue INT:= queue [1,2,3,4,5]

[ 1 , 2 , 3 , 4 , 5 ]

Type: Queue Integer
enqueue!(9,a)

9

Type: PositiveInteger
a

[ 1 , 2 , 3 , 4 , 5 , 9 ]

Type: Queue Integer

To read the top element without changing the queue use the front function.

a:Queue INT:= queue [1,2,3,4,5]

[ 1 , 2 , 3 , 4 , 5 ]

Type: Queue Integer
front a

1

Type: PositiveInteger
a

[ 1 , 2 , 3 , 4 , 5 ]

Type: Queue Integer
Book Contents Previous Section 9.64 Quaternions Next Section 9.65 RadixExpansion Book Index