December
1998 QUESTION 4 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS
|
(a) | Define and explain the properties of a queue abstract data type. In particular, identify any error conditions that may be raised when manipulating a queue. | [3] |
Properties
of a stack.
one mark for each point, up to a maximum of three marks.
|
||
(b) | The following procedures and
functions provide an interface to a stack (of integers) abstract data types: PROCEDURE CreateStack(VAR stack: StackType); PROCEDURE Push(VAR stack: StackType; element: Integer); FUNCTION Pop(VAR stack: StackType):Integer; FUNCTION IsEmpty(stack: StackType): Boolean; Given the definition of the procedure anonymous below: PROCEDURE anonymous(VAR stack: StackType);
|
|
(i) What happens if an empty stack is passed to anonymous? | [1] | |
if an empty
stack is passed to anonymous, then an empty stack will be returned.
|
||
(ii) If the procedure anonymous is passed the stack shown below as an argument draw the state of the stack on exit from the procedure | [2] | |
Two marks
for the following picture of the reversed stack: |
||
(iii) Describe the effect of the
procedure anonymous when given an arbitrary stack as its argument. The following questions manipulate a linked-list representation of a stack: TYPE StackType = StackItem; StackItem = RECORD
|
[2] | |
The procedure anonymous
reverses the elements on the stack.
|
||
(c) | Using iteration, write a
function SizeStack, that has the
type shown below, that manipulates the linked-list representation of the StackType, and returns the number of elements in
the stack. FUNCTION SizeStack(stack: StackType):Integer;
|
[4] |
A definition of
SizeStack is shown below: FUNCTION
SizeStack(stack: StackType):Integer;
|
||
(d) | Using recursion, write a function MaxStack, that has the type shown below, that
returns the largest integer held in the stack. You can assume that the stack passed as
argument to the function contains at least one element. FUNCTION MaxStack(stack: StackType):Integer;
|
[4] |
FUNCTION
MaxStack(stack: StackType):Integer; VAR rest:Integer; BEGIN IF (stack^.next=Nil) THEN MaxStack := stack^.data; ELSE BEGIN rest := MaxStack(stack^.next); IF (rest>stack^.data) MaxStack := rest; ELSE MaxStack := stack^.data; END END;
|
||
(e) | Write a function IndexStack, that has the type shown below, that
returns the nth element from the
top of a non-empty stack (zero is the top of the stack). If n + 1 is greater than
the size of the stack, then the function should return the bottom element of the stack. FUNCTION IndexStack(stack: StackType, n: Integer): Integer;
|
[4] |
A definition
of IndexStack is shown below: FUNCTION IndexStack(stack: StackType, n: Integer): Integer; and the following marking scheme should be used:
|