Stack Queue
Stack Queue
Stack Queue
Tutorial Sheet
Stacks
1) Which one of the following is an application of Stack Data Structure?
a) Managing function calls
b) The stock span problem
c) Arithmetic expression evaluation
d) All of the above
3) Let S be a stack of size n >= 1. Starting with the empty stack, suppose we push the first n
natural numbers in sequence, and then perform n pop operations. Assume that Push and Pop
operation take X seconds each, and Y seconds elapse between the end of one such stack
operation and the start of the next operation. For m >= 1, define the stack-life of m as the time
elapsed from the end of Push(m) to the start of the pop operation that removes m from S. The
average stack-life of an element of this stack is
a) n(X+ Y)
b) 3Y + 2X
c) n(X + Y)-X
d) Y + 2X
4) A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from
opposite ends of the array. Variables top1 and top2 (top 1< top 2) point to the location of the
topmost element in each of the stacks. If the space is to be used efficiently, the condition for
“stack full” is
a) (top1 = MAXSIZE/2) and (top2 = MAXSIZE/2+1)
b) top1 + top2 = MAXSIZE
c) (top1= MAXSIZE/2) or (top2 = MAXSIZE)
d) top1= top2 -1
5) Assume that the operators +, -, × are left associative and ^ is right associative. The order of
precedence (from highest to lowest) is ^, x , +, -. The postfix expression corresponding to the
infix expression a + b × c - d ^ e ^ f is
a) abc × + def ^ ^ -
b) abc × + de ^ f ^ -
c) ab + c × d - e ^ f ^
d) - + a × bc ^ ^ def
6) A function f defined on stacks of integers satisfies the following properties. f(∅) = 0 and f (push
(S, i)) = max (f(S), 0) + i for all stacks S and integers i. If a stack S contains the integers 2, -3,
0 1 2 3 4 5 6 7 8 9
7) Suppose a stack is to be implemented with a linked list instead of an array. What would be the
effect on the time complexity of the push and pop operations of the stack implemented using
linked list (Assuming stack is implemented efficiently)?
a) O(1) for insertion and O(n) for deletion
b) O(1) for insertion and O(1) for deletion
c) O(n) for insertion and O(1) for deletion
d) O(n) for insertion and O(n) for deletion
8) Consider n elements that are equally distributed in k stacks. In each stack, elements of it are
arranged in ascending order (min is at the top in each of the stack and then increasing
downwards). Given a queue of size n in which we have to put all n elements in increasing
order. What will be the time complexity of the best known algorithm?
a) O(n)
b) O(n 2 )
c) O(n log (n))
d) O(nk)
9) The data structure required to check whether an expression contains balanced parenthesis is?
a) Stack
b) Queue
c) Array
d) Tree
10) The process of accessing data stored in a serial access memory is similar to manipulating data
on a ______?
a) Heap
b) Binary Tree
c) Array
d) Stack
11) Which data structure is needed to convert infix notation to postfix notation?
a) Branch
b) Tree
c) Queue
d) Stack
14) Which of the following statement(s) about stack data structure is/are NOT correct?
a) Stack data structure can be implemented using linked list
b) New node can only be added at the top of the stack
c) Stack is the FIFO data structure
d) The last node at the bottom of the stack has a NULL link
15) Consider the linked list implementation of a stack. Which of the following node is considered as
Top of the stack?
a) First node
b) Last node
c) Any node
d) Middle node
18) Which of the following operation take worst case linear time in the array implementation of
stack?
a) Push
b) Pop
c) IsEmpty
d) None
20) What is the minimum number of stacks of size n required to implement a queue of size n?
a) One
b) Two
c) Three
d) Four
Programming [stack]:-
1) Write a program to Implement stack using queues. You have operations of queue (enqueue,
dequeue) and you have to implement operations of stack (push, pop, etc…)
2) Write a program to Implement stack using array.
3) Write a program to Implement stack using linked list.
4) Write a program to convert a given infix expression to post fix expression.
5) Write a program to reverse a stack using recursion.
Queue
1) A Queue is best characterized as
2) Given an empty queue Q, what does it look like after the following operations?
Q.enqueue(5)
Q.enqueue(2)
Q.dequeue()
Q.enqueue(3)
Q.dequeue()
a) 3
b) 5
c) 9
d) none of the above
3) Given a 5 element stack S (from top to bottom: 2, 4, 6, 8, 10), and an empty queue Q, remove
the elements one-by-one from S and insert them into Q, then remove them one-by-one from Q
and re-insert them into S. S now looks like (from top to bottom).
a) 2, 4, 6, 8, 10
b) 10, 2, 4, 6, 8
c) 10, 8, 6, 4, 2
d) none of the above
4) What is the reason for using a "circular queue" instead of a regular one?
a) the running time of enqueue() is improved.
b) reuse empty spaces.
c) you can traverse all the elements more efficiently.
d) None of the above.
5) Given an efficient circular array-based queue Q capable of holding 10 objects. Show the final
contents of the array after the following code is executed:
for (int k = 1; k <= 7; k++)
Q.enqueue(k);
for (int k = 1; k <= 4; k++)
Q.enqueue(Q.dequeue());
Final Answer:
7) Assume that you have an empty circular queue (array-based implementation) which can
hold only four elements. What are the array indexes of the back and the front elements
after executing this series of queue operations? (indexes start with 0)
enqueue(“a”), enqueue(“b”), getFront(), enqueue(“c”), enqueue(“d”), dequeue(), dequeue(),
dequeue(), enqueue(“a”), enqueue(“b”), enqueue(“c”), dequeue(), getFront(), enqueue(“d”),
dequeue()
front index = ____________ back index = ________________
b) When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes
c) Load Balancing
9) How many stacks are needed to implement a queue. Consider the situation where no
other data structure like arrays, linked list is available to you.
a) 1
b) 2
c) 3
d) 4
10) A priority queue can efficiently implemented using which of the following data structures?
Assume that the number of insert and peek (operation to see the current highest priority
item) and extraction (remove the highest priority item) operations are almost same.
a) Array
b) Linked List
11) Which of the following is true about linked list implementation of queue?
a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
b) In push operation, if new nodes are inserted at the end, then in pop operation, nodes
must be removed from the beginning.
a) 10, 8, 7, 5, 3, 2, 1
b) 10, 8, 7, 2, 3, 1, 5
c) 10, 8, 7, 1, 2, 3, 5
d) 10, 8, 7, 3, 2, 1, 5
14) Consider the following operation along with Enqueue and Dequeue operations on queues,
where k is a global parameter.
MultiDequeue(Q)
{
m = k
while (Q is not empty and m > 0) {
Dequeue(Q)
m = m - 1
}
}
What is the worst case time complexity of a sequence of n MultiDequeue() operations on
an initially empty queue?
a) Θ(n)
b) Θ(n + k)
c) Θ(n * k)
d) Θ(n 2 )
15) Suppose you are given an implementation of a queue of integers. The operations that can
be performed on the queue are:
1) isEmpty (Q) — returns true if the queue is empty, false otherwise.
2) delete (Q) — deletes the element at the front of the queue and returns its value.
c) insert (Q, i) — inserts the integer i at the rear of the queue.
Consider the following function:
void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
i = delete(Q);
f(Q);
insert(Q, i);
}
}
What operation is performed by the above function f ?
a) Leaves the queue Q unchanged
b) Reverses the order of the elements in the queue Q
c) Deletes the element at the front of the queue Q and inserts it at the rear keeping the
other elements in the same order
d) Empties the queue Q
16) The data structure required for Breadth First Traversal on a graph is?
a) Stack
b) Array
c) Queue
d) Tree
17) aIn linked list implementation of a queue, where does a new element be inserted?
a) At the head of link list
b) At the tail of the link list
c) At the centre position in the link list
d) None
18) In linked list implementation of queue, if only front pointer is maintained, which of the following
operation take worst case linear time?
a) Insertion
b) Deletion
c) To empty a queue
d) Both a) and c)
19) A data structure in which elements can be inserted or deleted at/from both the ends but not in
the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue
Programming [Queue]
1) Write a program to implement queue using stacks. You have only stack operations push, pop
and you need to implement queue operation like enqueue dequeue etc.
2) Write a program to Implement a queue data structure using array.
3) Write a program to Implement a queue data structure using linked list.
4) Write a program to Implement min-heap using array. implement create heap, delete min, insert
element etc..
5) Write a program to check whether a given Binary Tree is Complete or not. for this you need to
use Breadth first search using queue.