Theory Assignment 2
Theory Assignment 2
Islamabad Campus
Department of Computer Science
Question # 1
Indicate whether a stack would be a suitable data structure for each of the following application. Justify your
answer.
Application Stack Suitable or not[Y/N] Reason
A bank simulation of its teller
operation to see how waiting times
would be affected by adding
another teller
An address book
A program to receive data that are
to be saved and processes in the
reverse order
A word processor to have a PF key
that causes the preceding command
to be redisplayed. Every time the
user press the PF key, the program
shows the command that preceded
the one currently displayed.
A program to evaluate arithmetic
expressions according to the
specific order of operators
A dictionary of words used by a
spelling checker to be built and
maintained
A data structure used to keep track
of return addresses for nested
function while a program is
running
A program to keep track of patients
as they check into a medical clinic,
assigning patient to doctor on a
first come, first served basis
Question # 2
Fall-2024 Page 1
Two stacks of positive integers are needed, one containing elements with values less than or equal to 1,000
and other containing elements with values larger than 1,000. The total number of elements in the small – value
stack and the large – value stack combined are not more than 200 at any time, but we cannot predict how
many are in each stack. (All of the elements could be in the small –value stack, they could be evenly divided,
both stacks could be empty, and so on). Can you think of a way to implement both stacks in one array?
a) Draw a diagram of how the stack might look.
b) Write the definitions for such a double – stack structure.
c) Write the algorithm for Push operation; it should store the new item into the correct stack according to
its value.
Question # 3
In each plastic container of Pez candy, the colors are stored in random order (See figure below).
Your little brother likes only yellow ones, so he painstakingly takes out all the candies, one by one, eats the
yellow ones, and keeps the other in order, so that he can return them to container in exactly the same order as
before- minus the yellow candies, of course. Write the algorithm to simulate this process. You may use any of
the stack operations defined in the stack ADT, but may not assume any knowledge of stack’s implementation.
ANSWER IN THIS BOX
i. Draw a schematic diagram about how to plan six stacks in a single array.
Fall-2024 Page 2
ii. How would you implement a stack of stack?
Question # 4 (Applications of Stack)
Write an equivalent postfix expression for the infix expression. Write each step of this conversion.
1. (A + B)*(C – D )
2. A ^ B * C – D + E/F
3. A/(B+C*D-E)
4. A-B*C+D/E
5. (A+B)^2 -(C-D)/2
ANSWER IN THIS BOX
Question # 5
What is the value of the postfix expression? Write each step of this conversion.
1. AB+CD–*
2. A B ^ C*D – E F/+
3. ABCD*+E-/
4. ABC*-DE/+
5. AB+2 ^CD-2/-
Where A = 12 , B = 3 ,C = 7 , D = 4 ,E = 2 and F = 5
ANSWER IN THIS BOX
Question # 6
i. Write an algorithm for converting an infix expression into prefix expression
ii. Write an algorithm for evaluation of prefix expression
Fall-2024 Page 3
ANSWER IN THIS BOX
Question # 7
i. Convert the following infix expression into equivalent prefix expression
A^B*C-D+E/F/(G+G)
ii. Evaluate the following prefix expression:
+ A*B+CD if A =2 , B =3, C = 4, D = 5
ANSWER IN THIS BOX
Fall-2024 Page 4
Question # 9 (Recursion)
Consider the following recursion for integer multiplication of two positive number a and b:
a* 1 = a
a*b = a(b -1) + a
This can be implemented using following recursive algorithm as follows:
Algorithm recursive_multiplication(a, b)
if b = 1 then
return a
else
return a + recursive_multiplication ( a, b-1)
a) Convert above recursive algorithm in to an iterative algorithm? Present your iterative version into
Pseudo code.
b) Mathematically the following definition for integer multiplication is valid:
a multiply by b : a*b = a(b+1) – a
Can we have recursive algorithm for calculating integer multiplication based on above definition?
Explain carefully.
Fall-2024 Page 5
Question # 10
Indicate whether each of the following application would be a suitable for a queue. Justify your answer.
Fall-2024 Page 6
Question # 11
Write an algorithm that will reverse all the elements in a queue.
Question # 12
i. How would you implement a queue of stacks?
ii. How would you implement a stack of queues?
iii. How would you implement a queue of queues?
a. Draw a diagram of how each of these data structures might look.
b. Write algorithms/ routines to implement the appropriate operations of each of these data
structures
Fall-2024 Page 7
Question # 13
Write algorithms (for insertion and deletion) that implement two queues in one array where first queue will
start from 0th position and second queue will start from last position of the array.
Question # 14
Write an algorithm that uses stack in order to reverse the elements of a circular queue, which is stored in an
array. For example, if the initial queue is that given in Fig-1 as under, then the resulting Queue is that given in
Fig-2.
Question # 15
It is required to split a circular queue into two circular queues (say CQueue1 and CQueue 2) so that all the
elements in odd positions are in one queue and those in even positions are in another queue as shown in the
following figure. Write a C++ program to accomplish this. Assume that queue is maintained in an array.
Fall-2024 Page 8
CQueue2:
CQueue1:
Before
Question # 16
Can a queue be represented by a circular linked list with only one pointer pointing to the tail of the queue.
Write C++ functions for add and delete operations on such a queue.
Question # 17
A DEQUE is a data structure consisting of a list of items, on which the following operations are possible:
PUSH ( X,D) : Insert item X on the front end of DEQUE D.
POP(D) : Remove the front item from DEQUE D and return it.
Inject(X, D) : Insert item X on the rear end of DEQUE D.
Eject(D) : Remove the rear item from DEQUE D and return it.
Write C++ program (complete program) that support the above DEQUE operations.
Stack Implementation:
Question # 18
Palindrome checking by Stack of Array, Singly and doubly linked list of Stack
Question # 19
Question # 20
Question # 21
Question # 22
Consider a scenario in which you have to design the algorithm for Pizza Hutt. They accept a maximum
M number of orders. Orders are served in first come first served basis. Order once placed cannot be
cancelled. Design the algorithm by using the most suitable Static Data Structure.
Question # 24
Pizza Hutt changes its policy and adds some constraint in their policy. They changed policy and serve
the customers on the basis of their age so the elder customer will be served earlier and for equal age
FIFO will be applicable.
Question # 25
Pizza Hutt management decides to have two serving units with following constraints:
a. There will be only one row for waiting customers.
b. Customer can come either on front of the row or at rear but not at middle.
c. Customers can be served either by front counter or rear.
Fall-2024 Page 10