next up previous contents
Next: Semantic Specification in Modula-2 Up: Specification of Abstract Data Previous: Constructors and Accessors

Exercises

Exercise 8.2

Observe that the task of ``popping'' a stack has been separated into two distinct operations pop and top. How would the DEFINITION MODULE of Fig. 8.1 be amended if a single operation pop_stack is to be provided which retrieves the top-most element of a stack and produces a new stack with the top-most element removed? Suppose an application requires the entire contents of a non-empty stack to be removed and displayed. This could be achieved as follows :

   WHILE NOT isempty(s) DO
      pop_stack(s,value);
      Write(value)
   END;
What is the corresponding Pascal-like code fragment using the specification of Fig. 8.1?

Exercise 8.3

Another widely used data structure is the queue which is a FIFO structure (first-in-first-out). The behaviour of a queue can be described in terms of the following set of operations :

If the data elements of the queue are natural numbers, produce a Modula-2 DEFINITION MODULE Queues for the abstract data type. For example, the procedure remove will have the header
PROCEDURE remove(q : Queue) : Queue;
Demonstrate, by appropriate renaming of the operations (PROCEDURE names) and TYPE identifiers, that the abstract data types Stacks and Queues have the same syntax up to renaming.
next up previous contents
Next: Semantic Specification in Modula-2 Up: Specification of Abstract Data Previous: Constructors and Accessors
Lee McCluskey
2002-12-18