-
Notifications
You must be signed in to change notification settings - Fork 1
Queue: a first in, first out data structure
TypePun edited this page Jan 30, 2019
·
4 revisions
A queue is a first-in, first-out data structure. Items Pushed are inserted at the rear, or 'tail' of the queue. Items removed, or Popped are taken from the 'front' of the queue.
/declare q queue
The type name is case sensitive. That is, queue will work, but Queue, QUEUE or any other permutation will not create the type.
| Name | Arguments | Returns | Comments |
|---|---|---|---|
| Count | None | Integer | Number of items inserted onto the queue. |
| Push | String | Bool | True if the item was pushed successfully. |
| Pop | None | String | False is returned if IsEmpty is true. |
| IsEmpty | None | Bool | True if Count = 0, False otherwise. |
| Peek | None | String | False is returned if IsEmpty is true. |
Sub QueueTest
/declare q queue
/echo 'Starting Queue Test'
/echo 'Queue is Empty: ${q.IsEmpty}'
| Push entries on the queue and pop them off.
| Note: entries are inserted (Pushed) in and removed
| (Popped) in the same order. That is, Pushing A, B, C, D, E
| and then Popping them will return A, B, C, D, E.
/echo 'Pushing items onto the Queue.'
/if (!${q.Push[A]}) {
/echo 'Queue Push of A failed.'
/endmacro
}
/if (!${q.Push[B]}) {
/echo 'Queue Push of B failed.'
/endmacro
}
/if (!${q.Push[C]}) {
/echo 'Queue Push of C failed.'
/endmacro
}
/if (!${q.Push[D]}) {
/echo 'Queue Push of D failed.'
/endmacro
}
/if (!${q.Push[E]}) {
/echo 'Queue Push of E failed.'
/endmacro
}
/declare count int
/varset count ${q.Count}
/if (${count} != 5) {
/echo 'Queue count is: ${count} and should be 5.'
/endmacro
}
/echo 'Queue is Empty: ${q.IsEmpty}'
| Peek at the front item:
/echo 'Front item is: ${q.Peek}'
| Pop items off the queue.
/echo 'Popping item off queue: ${q.Pop}'
/echo 'Popping item off queue: ${q.Pop}'
/echo 'Popping item off queue: ${q.Pop}'
/echo 'Popping item off queue: ${q.Pop}'
/echo 'Popping item off queue: ${q.Pop}'
/echo 'Queue is Empty: ${q.IsEmpty}'
/echo 'Ending Queue Test'
/return
-
PopdecreasesCountby 1 unlessIsEmptyis true. -
PushincreasesCountby 1.IsEmptywill never be true after callingPush. -
Peekretrieves the oldest elementPushed onto the queue.Countis not modified by callingPeek.