You are on page 1of 5

Pir Mehr Ali Shah

Arid Agriculture University, Rawalpindi


Office of the controller of Examinations
Mid Exam / Fall 2020 (Paper Duration 24 hours)
To be filled by Teacher

Course No.: CS-443 Course Title: Data Structures and Algorithms


Total Marks: 12 Date of Exam: 03 /12/ 2020
Degree: BSCS/IT Semester: 3rd Section: A,B
Marks
Q.No. 1 2 3 4 5 6 7 8 9 10 Obtained/
Total Marks
Marks - - - - - /18
Obtained
Total Marks in Words:
Name of the teacher: Usman Ahmad
Who taught the course:Signature of teacher / Examiner: Usman Ahmad

To be filled by Student

Registration No.: ………………………………………….……… Name:……………………………………………………..

Answer the following questions.

Q.No.1 (Marks 2)

a. Write a program that will ask the user to input any number greater than 29.

b. Create a double linked list that will contain all the odd elements less than the number
which you entered in step (a). Also display this list.

c. Now ask the user to enter any pivot element. Pivot element is a simple integer number.
Use this pivot element to divide the list into two halves. First half will contain all the
elements smaller than that pivot element. Save this first half into a list called smallList.
Second half will contain all the elements larger than that pivot element. Save this second
half into a list called largeList.

d. In smallList, delete all the nodes at even position whereas in largeList, delete all nodes
at odd position.

e. Display smallList and LargeList that you formed in step (d).

Ans.
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
Question No. 2: [2 Marks]
Write a program that will take 10 integer values from the user.

a. If the entered value is Even then enqueue it into a queue at even index and if the entered
value is Odd then enqueue it into a queue at odd index. (Note: Even index will start from 0
& odd will start from 1 ).

b. Dequeue the values and display the sum of the even values and the sum of the odd values.
(Only write specific code, not system generated)

Answer:

_______
_________________________________________________________________________

Question No. 3: [4 Marks]

In Several applications, more than one stack may be required together, some stacks overflow
whereas others are nearly empty, Suppose an application requires two stack X and Y ,One can
define an array A with Nx elements for stack X and another array B with Ny elements for stack
Y. Now instead of defining two separates arrays A and B, We can define a single array ,Say
AB ,with N=Nx+Ny elements for X and Y together ,Let us define the starting locations of items
for stack X and Y as AB[0] and AB[N] respectively and X grows to the right whereas Y grows
to the left
Using this scheme, we need the modified versions of push_A and pop_A as push_X ,push_Y
,pop_X and pop_Y .Similarly ,the operation STATUS_AB has to be defined to test the state of
empty or full, count of space occupied by X and Y, etc

0 1 2 N-2 N-1
N
1 2 3 5 7 8
X grows ----->> <<------Y
grows

Write the basic operations for the implementation of such a multi-stack.

Ans________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
Question No. 4 [6 Marks]

Alexa has two stacks of non-negative integers, stack A=[a0,a1,….,an-1] and stack
B=[b0,b1,..bm-1] where index denotes the top of the stack. Alexa challenges Nick to play
the following game:

• In each move, Nick can remove one integer from the top of either stack A or
stack B
• Nick keeps a running sum of the integers he removes from the two stacks.
• Nick is disqualified from the game if, at any point, his running sum becomes
greater than some integer given at the beginning of the game.
• Nick's final score is the total number of integers he has removed from the two
stacks.

Given A, B and x for games g, find the maximum possible score Nick can achieve
(i.e., the maximum number of integers he can remove without being disqualified)
during each game and print it on a new line.

The image below depicts the integers Nick should choose to remove from the stacks. We print
as our answer, because that is the maximum number of integers that can be removed from the
two stacks without the sum exceeding x=10.

(Initial Look) (After Some moves)

Ans_______________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

Question No. 5 [marks 4]


Write a C++ program to implement a suitable data structures (using array) of marble plates.
Your program should cover the following scenario.
Suppose a waiter in a hotel is collecting plates from different tables and giving to dish washer.
Dish washer is washing plates and throwing them into rack of plates. Some plates hit others
very hard to break other (one above it) and/or itself.
You need to develop an application that will find how many plates are broken and plates from
which tables are broken.
You will identify plates with unique numeric identity number. Every 5th plate thrown into rack
will break itself and one plate below it while every 7th plate will only break itself.
Ans_______________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________

You might also like