SIBT COMP 125

Week 5 Tutorial Exercises

Exercises

1. Suppose you have an empty stack, S, of real numbers, and the real variables X and Y have the values 17 and 3 respectively. I perform the following operations:

S.Put (X)

S.Put (X)

S.Put (Y)

Z := S.Item

S.Remove

Z := Z * S.Item

S.Put (Z)

W := S.Item

S.Remove

W := W - S.Item

S.Put (W)

V := S.Item

io.put_integer (V)

 What will be the output, and what will S look like at the end? (Please note: There is no actual point to this exercise apart from getting some practice at using stacks).

2. In an earlier tutorial, you were asked to reverse a word using STRING class commands. In the lectures I said it was quite possible to do the same thing with a stack. Write Eiffel code using the stack class BOUNDED_STACK to do this. What problems are encountered when using a stack to do this operation?

3. This question refers to the STRING_STACK class. Given the declarations

	my_stack : STRING_STACK
	next_top : STRING
	no_next_top : BOOLEAN
	

write code which removes the second from top element of my_stack and attaches next_top to it. If there is no second from top element, set no_next_top to true. my_stack should be unchanged except for the removal of the second from top element.

In this question you are a client of STRING_STACK, and can only use the operations of the stack ADT.

 

4. HOMEWORK: Using the STRING_STACK class discussed above, write code which will search through the stack and discard a given string. The procedure should start:

remove_string(unwanted : STRING) is

and should remove the unwanted string from the stack. The stack should remain unchanged otherwise. (Hint: Use a temporary stack). Again, you are a client of STRING_STACK, and can only use the operations of this ADT.