0% found this document useful (0 votes)
3 views

Copy of Stack_and_Queue_Level-II

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Copy of Stack_and_Queue_Level-II

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Stack and Queue

Exercises
1. Consider a railroad switching network as below.

Railroad cars numbered 1,2,3, ..., n are at the right. Each car is brought into the stack and removed
at any time. For instance, if n = 3, we could move 1 in, move 2 in, move 3 in and then take the
cars out producing the new order 3,2,1. For n = 3 and 4 what are the possible permutations of the
cars that can be obtained? Are any permutations not possible?

2. Using a Boolean variable to distinguish between a circular queue being empty or full, write insert
and delete procedures.

3. A double ended queue (deque) is a linear list in which additions and deletions may be made at
either end. Obtain a data representation mapping a deque into a one dimensional array. Write
algorithms to add and delete elements from either end of the deque.

4. A linear list is being maintained circularly in an array C(0: n - 1) with F and R set up as for circular
queues.
a) Obtain a formula in terms of F, R and n for the number of elements in the list.
b) Write an algorithm to delete the kth element in the list.
c) Write an algorithm to insert an element Y immediately after the kth element.
d) What is the time complexity of your algorithms for b) and c)?

5. Let L = (a1, a2, ..., an) be a linear list represented in the array V(1:n) using the mapping: the ith
element of L is stored in V(i). Write an algorithm to make an inplace reversal of the order of
elements in V. I.e., the algorithm should transform V such that V(i) contains the n - i + 1'st element
of L. The only additional space available to your algorithm is that for simple variables. The input
to the algorithm is V and n. How much time does your algorithm take to accomplish the reversal?

6. What is the maximum path length from start to finish in any maze of dimension n × m?

7. Write the postfix form of the following expressions:


a) A ** B ** C
b) -A + B - C + D
c) A ** -B + C
d) (A + B) * D + E/(F + A * D) + C
8. Write an algorithm to transform from prefix to postfix. Carefully state any assumptions you make
regarding the input. How much time and space does your algorithm take?

9. Write an algorithm to generate fully parenthesized infix expressions from their postfix form. What
is the complexity (time and space) of your algorithm?
10. Obtain a data representation mapping a stack S and a queue Q into a single array V(1:n). Write
algorithms to add and delete elements from these two data objects. What can you say about the
suitability of your data representation?

11. The array-based stack implementation uses a fixed length array. As a result, it is possible for the
stack to become full.
a) Rewrite the push method so that it doubles the length of the array when the array is full.
b) Rewrite the pop method so that it halves the length of the array when the array is less
than half full.
c) Show that the average time for both push and pop operations is O(1).

12. Consider a sequence S of push and pop operations performed on a stack that is initially empty.
The sequence S is a valid sequence of operations if at no point is a pop operation attempted on an
empty stack and if the stack is empty at the end of the sequence. Design a set of rules for
generating a valid sequence.

13. Devise an implementation of the queue abstract data type using two stacks. Give algorithms for
the enqueue and dequeue operations, and derive big-oh expressions for the running times of your
implementation.

14. Write each of the following infix expressions in postfix notation:


a) a+b*c%d
b) a+b*(c%d)
c) (a+b)*c%d
d) (a+b)*(c%d)
e) (a+b*c)%d
f) (c%d)*(a+b)

15. Write each of the following postfix expressions in infix notation:


a) wxy%z*-
b) wxyx*%-
c) wx-y%z*
d) wx-yz*%
e) wxy%-z*
f) yz*wx-%

16. The array-based queue implementation uses a fixed length array. As a result, it is possible for the
queue to become full.
a) Rewrite the enqueue method so that it doubles the length of the array when the array is
full.
b) Rewrite the dequeue method so that it halves the length of the array when the array is
less than half full.
c) Show that the average time for both enqueue and dequeue operations is O(1).

17. Stacks and queues can be viewed as special cases of deques. Show how all the operations on
stacks and queues can be mapped to operations on a deque. Discuss the merits of using a deque
to implement a stack or a queue.

18. Suppose we add a new operation to the stack ADT called findMinimum that returns a reference
to the smallest element in the stack. Show that it is possible to provide an implementation for
findMinimum that has a worst case running time of O(1).

You might also like