September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
51
Task/Stack/ALGOL-W/stack.alg
Normal file
51
Task/Stack/ALGOL-W/stack.alg
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
begin
|
||||
% define a Stack type that will hold StringStackElements %
|
||||
% and the StringStackElement type %
|
||||
% we would need separate types for other element types %
|
||||
record StringStack ( reference(StringStackElement) top );
|
||||
record StringStackElement ( string(8) element
|
||||
; reference(StringStackElement) next
|
||||
);
|
||||
% adds e to the end of the StringStack s %
|
||||
procedure pushString ( reference(StringStack) value s
|
||||
; string(8) value e
|
||||
) ;
|
||||
begin
|
||||
reference(StringStackElement) newElement;
|
||||
top(s) := StringStackElement( e, top(s) )
|
||||
end pushString ;
|
||||
% removes and returns the top element from the StringStack s %
|
||||
% asserts the Stack is not empty, which will stop the %
|
||||
% program if it is %
|
||||
string(8) procedure popString ( reference(StringStack) value s ) ;
|
||||
begin
|
||||
string(8) v;
|
||||
assert( not isEmptyStringStack( s ) );
|
||||
v := element(top(s));
|
||||
top(s):= next(top(s));
|
||||
v
|
||||
end popStringStack ;
|
||||
% returns the top element of the StringStack s %
|
||||
% asserts the Stack is not empty, which will stop the %
|
||||
% program if it is %
|
||||
string(8) procedure peekStringStack ( reference(StringStack) value s ) ;
|
||||
begin
|
||||
assert( not isEmptyStringStack( s ) );
|
||||
element(top(s))
|
||||
end popStringStack ;
|
||||
% returns true if the StringStack s is empty, false otherwise %
|
||||
logical procedure isEmptyStringStack ( reference(StringStack) value s ) ; top(s) = null;
|
||||
|
||||
begin % test the StringStack operations %
|
||||
reference(StringStack) s;
|
||||
s := StringStack( null );
|
||||
pushString( s, "up" );
|
||||
pushString( s, "down" );
|
||||
pushString( s, "strange" );
|
||||
pushString( s, "charm" );
|
||||
while not isEmptyStringStack( s ) do write( popString( s )
|
||||
, if isEmptyStringStack( s ) then "(empty)"
|
||||
else peekStringStack( s )
|
||||
)
|
||||
end
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue