Lab 2
Lab 2
Note: You can select and do some questions according to your ability only. We would
like to note you that the more questions you do the better for you in doing final
practical and writing exams.
Writing exercises
1. Assume that x, y, z are integer variables and that s is a stack of integers, state the
output of each program fragment.
x=3y=5z=2
s.makeEmpty( )
s.push(x) s.push(4) s.pop()
s.push(y) s.push(3) s.push(z)
s.pop() s.push(2) s.push(x)
while not s.isEmpty( ) : print(‘s.pop() " "’)
2. x = 3 y = 1
s.makeEmpty()
s.push(5) s.push(7) s.pop()
x += y
s.pop()
s.push(x) s.push(y) s.push(2) s.pop() s.pop()
while not s.isEmpty( ) :
y = s.pop()
print(y)
print(‘x = ’, x)
(‘y = ’, y)
3. A letter means push and an asterisk means pop in the following sequence. Give
the sequence of values returned by the pop operations when this sequence of
operations is performed on an initially empty stack.
E A S * Y * Q U E * * * S T * * * I O * N * * *
4. A letter means push and an asterisk means pop in the following sequence. Give
the contents of s[0], ..., s[4] after this sequence of operations is performed on an
initially empty stack (the stack is implemented by array s[]).
L A * S T I * N * F I R * S T * * O U * T * * * * * *
5. A letter means enqueue and an asterisk means dequeue in the following sequence.
Give the sequence of values returned by the dequeue operation when this sequence of
operations is performed on an initially empty queue.
E A S * Y * Q U E * * * S T * * * I O * N * * *
6. A letter means enqueue and an asterisk means dequeue in the following sequence.
Give the contents of q[0], ..., q[4] after this sequence of operations is performed on an
initially empty queue (the queue is implementated by circular array q[] with size 5).
E A S * Y * Q U E * * * S T * * * I O * N * * *
Practical exercises
Question 1. Write a program in Python to implement a stack of integer values with
the following operations :
1. def isEmpty() - return true if the stack is empty and false otherwise.
2. def clear() - clear the stack.
3. def push(x) - insert a node with value x at the top of the stack.
4. def pop() - remove the top element on the stack and return it's value; throw
EmptyStackException for empty stack.
5. def top() - return value of a node at the top of the stack; throw
EmptyStackException for empty stack.
6. def traverse() - display all values in the stack from the top to the bottom.
7. Use a stack to convert an integer number in decimal system to binary system and
display on the screen.
Note: You can write some constructors and other methods as you see they are
necessary.
5. def first() - return value of the first node of the queue; throw Exception for empty
queue.
4. def traverse() - display all values in the queue from the front to the rear.
6. Use a queue to convert a real number less than 1 in decimal system to binary
system and display on the screen.
Note: You can write some constructors and other methods as you see they are
necessary.
Question 3. Modify programs in questions 1 and 2 so that elements in the stack and
queue are String types and write the Main function to test them.
Question 4. Modify programs in questions 1 and 2 so that elements in the stack and
queue are character types and write the Main function to test them.
Question 5. Modify programs in questions 1 and 2 so that elements in the stack and
queue are Object types and write the Main function to test them (e.g. stack and queue
contains computer names).