Professional Documents
Culture Documents
Islamabad Campus
Department of Computer Science
STACK
Question # 1
Suppose we need to maintain two stacks of the same type of items in a program. If the two stacks are stored in separate
arrays, then one stack might overflow while there are considerable unused spaces in other. To avoid this situation, it is
better to main two stacks in the same array as shown in the following figure
FALL 20 Page 1
In this structure, one stack say A grows from one end of the array and other stack say B starts at the end and grows in
the opposite direction, that is towards the direction of A
Write algorithms as well as C++ program of the above-mentioned double stack structure for the following operations:
PUSHA, PUSHB ,POPA,POP
Question # 2
Repeat the same concept but for three stacks in an array
Question # 3
a) Write a program to input and PUSH 10 values into a stack. POP the values from stack and print only even
values on the screen
b) Write a program to input and PUSH 5 values into a stack. POP values from the stack one by one and calculate
factorial of each item.
c) Implement a small calculator through stack to calculate basic arithmetic expression
d) Write a C++ program of stack to display the elements in FIFO (First In First Out) manner
RECURSION
Question # 4
a) Write a program to find the number of nodes in the linked list using recursion
b) Write a program to compare two linked list using recursion
c) Write a program to add a new node at the end of linked list using recursion
QUEUE
Question # 5
a) Write a C++ program that will reverse all the elements in a queue (Hint: Use stack)
b) Write a C++ program 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.
c) Write a C++ program that implement two queues in one array where first queue will start from 0 th position and
second queue will start from last position of the array.
d) 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 20 Page 2
CQueue2:
CQueue1:
Before
e) Write a C++ program that merge two queues into a single queue
f) Write a C++ program that implements a priority queue using a matrix PQ and two arrays FRONT and REAR
pointing the front and end of the queue.
g) Modify the program of priority queue such that the element having least priority is deleted first.
h) Write a C++ program to insert ten names into a priority queue. Print all the names from priority queue that have
the top priority
i) 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.
FALL 20 Page 3