You are on page 1of 3

COMSATS University, Islamabad

Islamabad Campus
Department of Computer Science

Read before Attempt


Assignment No. 2: STACK, QUEUE AND RECURSION
Course code and Title: CSC211, Data Structure and Algorithm
Instructor: Tanveer Ahmed
Assigned Date: October 23,2019 Due Date: November 6, 2019
Total Marks: --
CLO-2:
Apply linear data structure to various practical problems.
Marking Criteria:
Marks will be awarded for:
 Design of the data structure as general-purpose ADTs
 Making sure there are no memory leakages.
 Modularity and efficiency of the code.
 Well commented code.
 Error checking both in the code and user errors.
Good Luck! Happy coding. 
Instructions:
1. This is an individual assignment. You will submit your work individually through your logins
(course portal)
2. Try to get the concepts, consolidate your concepts and ideas from these questions
3. You should concern recommended books for clarify your concepts as handouts are not
sufficient.
4. Try to make solution by yourself and protect your work from other students. If I found
the solution files of some students are same then I will reward zero marks to all those
students.
5. Deadline for this assignment is November 6, 2019. This deadline will not be extended.

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

You might also like