Pre-goodbye
We did not want to conclude this issue without having created something new. As is our
custom we try to integrate a little bit of computer science into our programs. Our final
program will demonstrate a stack and a queue.
The stack
The stack is a data structure that operates on a Last In First
Out (LIFO) mode of operation. The stack normally has two operations associated with
it Push and Pop. The Push action is used to place a value on the stack,
while Pop is used to remove an element from it.
The most commonly quoted example of a stack are canteen plate holders. These hollow
structures are packed with plates and are emptied as consumers take plates out of them.
Plates are not the only
things that work on a stack concept. For example your lazer or inkjet printer could very
well work like that. Empty the printers paper tray and get yourself some coloured
paper. Place the paper in your printer, noting down the colour order. Print something.
Which coloured paper came out first. If it is the last one, then you have another example
of a stack.
But what have stacks got to do with computers? Try debugging an MSM-Workstation program
and look out for the debugger window Stack (File menu, option Test).
Press F6 a few times and observe how code gets placed on the stack and removed. Stacks are
the data structures used to keep tab of module calling in programming languages.
The queue
I dont think that one has to burn ones grey cells to find a real life
example of a queue! Whenever you wait in line at a bank, in your car while the lights are
red, or to pay for your groceries at one of these home owned shops with one cash point,
youre playing the queue game. In computers queues are also frequently used
structures. For example, pressing letters on your keyboard in response to an input, will
cause the first letter you press to be processed first. The last letter entered will be
processed at the very end.
In both real life and in computers one can find different varieties of queues, for
example simple queues and priority queues. We will limit ourselves to simple queues here.
For those who are curious about priority queues reason out the following real life cases:
- Youre waiting in line when someone important (has a higher priority) is allowed to
jump the line and get served before you.
- In the emergency department of a hospital, different cases will normally be given
different attention, depending on the gravity (priority) of the case.
The Code
Our application is called CompSci and it is made up of the following components:
Type |
Name |
Window |
CompSci |
Window |
Stack |
Window |
Queue |
Application
CompSci only has one function that of %%DoWin(CompSci).
Control Button1 Push
%%Cancel
Control Button2 Push
%%DoWin(Stack)
Control Button3 Push
%%DoWin(Queue)
Of interest when Stack is called up is that uses the macro %%Hide to hide the menu (CompSci). It also
initialises the variables that will be used and calls routine Focus.
Focus uses %%Enable
and %%Disable to grey
out certain elements making them unavailable to the user. For example is the stack is
empty you cannot pop a number. So why not intelligently tell the user that. Same reasoning
applies when the structure is full (20 entries).
As explained above, the stack operates on Last In First Out. This logic is
programmed in the two buttons Push and Pop.
When Push is pressed:
N Array,I
; Don't push an invalid entry
Q:var'?1N.N
%%GetChoice(Structure, Array)
S
I=""
F S I=$O(Array(I),-1) Q:I="" S Array(I+1)=Array(I)
S Array(1)=var
%%ResetChoice(Structure, Array)
S StackCnt=StackCnt+1
D UpdSel
Q |
Encapsulate all variables used
locally. Reject invalid numbers.
Read the values into Array
Shift existing entries one down.
Insert the new entry at the top or array.
%%ResetChoice to update list,
increment the stack counter
Call procedure that highlights first entry. |
PU
S
H |
N Array,I
%%GetChoice(Structure,Array)
S I=1
F S I=$O(Array(I)) Q:I=""
S Array(I -1)=Array(I)
K Array(StackCnt)
%%ResetChoice(Structure,Array)
S StackCnt=StackCnt-1
D UpdSel
Q |
Encapsulate all variables used
locally.
Read the values into Array Shift all entries one up (overwriting element 1).
Delete the last element of Array
%%ResetChoice to update list,
decrement the stack counter
Call procedure that highlights first entry. |
PO
P |
Continued...
E&OE