Modula-2 Handout

Modula-2 Handout - DEFINITION MODULE genericstack This module defines the public interface for the generic stack abstract data type It imports the

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
DEFINITION MODULE genericstack; (* This module defines the public interface for the generic stack abstract data type. It imports the type WORD, which permits the type of elements to be stored in the stack to be bound to the type ARRAY OF WORD, thereby permitting generic elements to be stored in the stack. *) FROM SYSTEM IMPORT (* type *) WORD; EXPORT QUALIFIED (* type *) stack, (* proc *) define, makeempty, empty, push, pop; TYPE stack; PROCEDURE define ( VAR s : stack (* out *) ); (* Creates an empty stack. Must be used before any other stack operation. *) PROCEDURE makeempty ( VAR s : stack (* in/out *) ); (* Reinitializes an existing stack s to an empty stack by removing all elements contained in the stack. *) PROCEDURE empty ( s : stack (* in *) ); (* Returns true if the stack s contains no elements, otherwise returns false. *) PROCEDURE push ( VAR s : stack (* in/out *) ; item : ARRAY OF WORD (* in *) ); (* Adds item to the top of stack s. *); PROCEDURE pop ( VAR s : stack (* in/out *) ; VAR item : ARRAY OF WORD (* out *) ); (* Removes item from the top of stack s. *) END generic stack.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Notice that our interface to the generic stack ADT is identical to the interface presented earlier except that the operation “topofstack” is not included. Any program units that use the earlier versions of the stack ADT can use this version without modification, provided they do not use the “topofstack” operation. Why have we excluded the “topofstack” operation? To be compatible with the earlier versions of the stack ADT, the interface to the generic version would have to have the form: PROCEDURE topofstack( s : stack (* in *) ) : ARRAY OF WORD; This statement is syntactically incorrect, since the type ARRAY OF WORD is not bound to any type. However, we can avoid this problem by defining this operation to be a procedure rather than a function with the following interface: PROCEDURE topofstack( s : stack (* in *) ; VAR item : ARRAY OF WORD (* out *) ); In this case, the type ARRAY OF WORD is bound to the type of item. We leave it as an exercise to implement the generic version of this operation. Now let us work through the implementation details by considering
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/23/2011 for the course CSE 324 taught by Professor Soliman during the Spring '11 term at NMT.

Page1 / 7

Modula-2 Handout - DEFINITION MODULE genericstack This module defines the public interface for the generic stack abstract data type It imports the

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online