Professional Documents
Culture Documents
Question 1 (a)
interface TimeInterface
{
void display();
int diffInsec(TimeInterface other);
void addTime(int hr, int min);
int timeInsec();
}
Client Class
Create instances of the two classes.
Validate that sec, hr and min are greater than zero.
Compute the difference in time between the two instances.
Call the addTime function and add change the time by any value.
All display should be in the format (h:m:s). Adjust the values such that h should not be more than 24, value of m
and s should not be more than 60.
DATA STRUCTURES LABORATORY July 2015
Question 1 (b)
Step 1:
Note: Use the TimeInterface, and the classes you created in Question 1(a)
Step 2:
Step 3
In the Client class
Using callbacks (through an anonymous class of the interface given below) write a method which will add
a given number of hours and minutes on each item in the array and return the new array
interface UnaryOp1
{
TimeInterface process(TimeInterface timeInterface);
}
Display the resulting array
DATA STRUCTURES LABORATORY July 2015
Question 2 (a)
Write a program to perform the following operations using Singly Linked List:
3. Insert an element at a specified position (position 0 is the beginning position, and anything greater than or equal
to the size of the list would be taken as the last position).
4. Delete the specified element from the list (the key would be the value of the node, and do nothing if the node
does not exists).
Input Format:
An input begins with the code of the operation as coded in the question followed by the necessary inputs in the
same line separated by spaces.
Output Format:
Display the linked list after every operation except the Exit operation ("Empty List" should be printed in case of
empty list) where the items are separated by spaces.
Input: Output:
1 123 123
5
DATA STRUCTURES LABORATORY July 2015
Question 2 (b)
Array implementation would allocate an array, which can hold up to n keys of the list. Reference variables should
not be used. You could have an array of integers of size n for n keys and an array of n integers for holding the next
array index. Using yet another array of array-indices for maintaining free nodes is optional. Write a class (say,
ListInArray), which allocates integer arrays of size n in the beginning. The class would also have an integer variable
Head (and Tail?) to refer to the beginning (and end of the linked list).
Write a program, which takes n, the max size of the list and then implements the following operations.
3. Insert an element at a specified position (position 0 is the beginning position, and anything greater than or equal
to the size of the list would be taken as the last position. Do nothing in case of overflow).
4. Delete the specified element from the list (the key would be the value of the node, and do nothing if the node
does not exist).
Input Format: An input begins with the code of the operation as coded in the question followed by the necessary
inputs in the same line separated by spaces.
Output Format: Display the linked list after every operation except the Exit operation ("Empty List" should be
printed in case of empty list) where the items are separated by spaces.
Input: Output:
1 123 123
6
DATA STRUCTURES LABORATORY July 2015
Question 3 (a)
Given a string of characters, construct a binary tree of the string where each character is the key of a node and the
tree would be in the following order. First character would be the root of the tree. Next 2 characters are the left
and right children of the tree, which form the depth level 1. The following four characters form the depth level 2
such that 4th and 5th characters are children of 2nd character, and 6th and 7th characters are children of 3rd
character. Similarly, the following 8 characters (8th to 15th in the string) form the depth level 3 in that order and
so on until all the characters in the string are consumed. In general, left and right children of character at i would
be characters at 2*i and 2*i+1. Parent of a character at j would be floor(j/2). Print the strings generated out of
preorder, inorder and postorder traversals of the constructed binary tree.
Preorder: abdecf
Inorder: dbeafc
Postorder: debfca
Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.
Input: Output:
Inorder: dbeafc
Postorder: debfca
Input: Output:
Inorder: euresniptiysv
Postorder: erusinetyivsp
DATA STRUCTURES LABORATORY July 2015
Question 3 (b)
Implement Priority Queue using min-heap so that the Remove method of the queue always removes the least
valued item in the queue.
R. Remove least-valued integer from the Priority Queue and Print it in a new line. Do nothing in case of underflow.
Input Format: An input begins with the code of the operation as coded in the question followed by the necessary
inputs in the same line separated by spaces.
Input: Output:
A 21 -23
A -23 -100
A 123 0
A 1000 21
R 123
A -100 1
A0 1000
A1
X
DATA STRUCTURES LABORATORY July 2015
Question 4 (a)
Given a string of characters, construct a binary tree of the string where each character is the key of a node and the
tree would be in the following order. First character would be the root of the tree. Next 2 characters are the left
and right children of the tree, which form the depth level 1. The following four characters form the depth level 2
such that 4th and 5th characters are children of 2nd character, and 6th and 7th characters are children of 3rd
character. Similarly, the following 8 characters (8th to 15th in the string) form the depth level 3 in that order and
so on until all the characters in the string are consumed. In general, left and right children of character at i would
be characters at 2*i and 2*i+1. Parent of a character at j would be floor(j/2). Print the strings generated out of
preorder, inorder and postorder traversals of the constructed binary tree.
Preorder: abdecf
Inorder: dbeafc
Postorder: debfca
Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.
Input: Output:
Inorder: dbeafc
Postorder: debfca
Input: Output:
Inorder: euresniptiysv
Postorder: erusinetyivsp
DATA STRUCTURES LABORATORY July 2015
Question 4 (b)
Input Format: Input begins with a positive integer n, indicating the number of integers to be inserted into the BST.
The following line to have n integers separated by spaces, which are to be inserted into the BST in the given order.
The third line will have an integer m, which needs to be searched in the BST for its existence.
Output Format: In the first line, n integers found out of in-order traversal of the BST to be printed in a single line
separated by spaces. True/False to be printed in the second line indicating whether the integer m exists in the BST
or not.
Input: Output:
9 12 23 34 45 56 67 78 89 90
34 90 12 89 78 45 56 23 67 True
89
Input: Output:
2 12 90
90 12 False
89
DATA STRUCTURES LABORATORY July 2015
Question 5 (a)
Write a program to perform the following operations using Doubly Linked List:
I. Insert an element at a specified position (position 0 is the beginning position, and anything greater than or equal
to the size of the list would be taken as the last position).
D. Delete the specified element from the list (the key would be the value of the node, and do nothing if the node
does not exist).
Input Format:
An input begins with the code of the operation as coded in the question followed by the necessary inputs in the
same line separated by spaces.
Output Format:
Print the linked list ("Empty List" should be printed in case of empty list) where the items are separated by spaces.
Input: Output:
I 0 123 123
D 243 345
X
DATA STRUCTURES LABORATORY July 2015
Question 5 (b)
Implement Priority Queue using min-heap so that the Remove method of the queue always removes the least
valued item in the queue.
R. Remove least-valued integer from the Priority Queue and Print it in a new line. Do nothing in case of underflow.
Input Format: An input begins with the code of the operation as coded in the question followed by the necessary
inputs in the same line separated by spaces.
Input: Output:
A 21 -23
A -23 -100
A 123 0
A 1000 21
R 123
A -100 1
A0 1000
A1
X
DATA STRUCTURES LABORATORY July 2015
Question 6 (a)
Implement a Stack class with Push and Pop methods (and any other methods required for your implementation).
Given an algebraic expression with multiple kinds of brackets, check whether the brackets are matching using the
Stack class.
You can safely ignore all the symbols other than brackets. You are free to use multiple instances of the class Stack,
if it is needed.
Sample Testcase:
Input:
{a+b[c-d]+[(e*f(g^2))]}
{a+b[c-d]+[(e*f(g^2)])}
{a+b[c-d]+[(e*f(g^2))]
Output:
True
False
False
DATA STRUCTURES LABORATORY July 2015
Question 6 (b)
Given n number of processes (P1, P2, ..., Pn) with their total required execution time units (aka burst time),
simulate the Round-Robin scheduling for a given time quantum by printing the Gantt Chart. Round-Robin
scheduling requires a queue/circular-list (it's called as Ready Queue) of processes to be scheduled. The processor
can execute one process at a time. The scheduling allows a process to execute for one time quantum and pre-
empts to allow the next process to execute for a time quantum and so on. Once a process is pre-empted after its
time quantum, it'll go the back of the Ready Queue. Whenever a process executes for its burst time, it should be
removed from the Ready Queue. Gantt Chart is a timeline listing of the processes executed (in order) for the given
list of processes. Just print the process name (Pi) in the Gantt Chart, which is essentially the order in which
processes were scheduled to execute. Implement the Ready Queue using a Circular List.
P1 6
P2 5
P3 2
P4 3
P5 7
Time Quantum: 2
Gantt Chart: P1, P2, P3, P4, P5, P1, P2, P4, P5, P1, P2, P5, P5
Input Format: Begins with n number of processes, and following line to have burst time of n processes separated
by spaces. Last line to have time quantum. All the numbers are positive integers not exceeding 100.
Output Format: Processes Pi scheduled (in order) separated by spaces, where i is the index of the process among
processes P1, P2, ..., Pn.
Input: Output:
5 P1 P2 P3 P4 P5 P1 P2 P4 P5 P1 P2 P5 P5
65237
2
DATA STRUCTURES LABORATORY July 2015
Question 7 (a)
Implement a Queue class with Add and Remove methods (and any other methods required for your
implementation).
Suppose you are receiving a sequence of items, which has two kinds of items. You are asked to make pairs of items
where a pair has both kinds of items. Implement it using the Queue class where the sequence of items is a string of
characters and the kinds are identified by whether the character is a digit or an English letter. You are free to use
multiple instances of the class Queue, if it needs. Assume there is equal number of either kind of characters in the
string. Output the pairs, one pair per line.
Input:
ab1cd2345efg67
Output:
a1
b2
c3
d4
e5
f6
g7
DATA STRUCTURES LABORATORY July 2015
Question 7 (b)
Given an undirected graph in the form of an adjacency matrix, find the number of components the given graph has
using DFS (or BFS) graph traversal technique.
Input Format: Input begins with n indicating the number of vertices the undirected graph has (1 <= n <= 10). The
following n lines to have the adjacency matrix of the graph where a '1' means there is an edge and '0' means there
is no edge between the vertices in question. Because it is an undirected graph, it is assured that the adjacency
matrix will be a symmetric matrix.
Output Format: Print the number of components the given graph has.
Input: Output:
3 3
000
000
000
3 2
010
100
000
10 5
0110000000
1010000000
1101000000
0010100000
0001000000
0000000000
0000000000
0000000000
0000000001
0000000010
DATA STRUCTURES LABORATORY July 2015
Question 8 (a)
Given a string of characters, construct a binary tree of the string where each character is the key of a node and the
tree would be in the following order. First character would be the root of the tree. Next 2 characters are the left
and right children of the tree, which form the depth level 1. The following four characters form the depth level 2
such that 4th and 5th characters are children of 2nd character, and 6th and 7th characters are children of 3rd
character. Similarly, the following 8 characters (8th to 15th in the string) form the depth level 3 in that order and
so on until all the characters in the string are consumed. In general, left and right children of character at i would
be characters at 2*i and 2*i+1. Parent of a character at j would be floor(j/2). Print the strings generated out of
preorder, inorder and postorder traversals of the constructed binary tree.
Preorder: abdecf
Inorder: dbeafc
Postorder: debfca
Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.
Input: Output:
Inorder: dbeafc
Postorder: debfca
Input: Output:
Inorder: euresniptiysv
Postorder: erusinetyivsp
DATA STRUCTURES LABORATORY July 2015
Question 8 (b)
Given n number of processes (P1, P2, ..., Pn) with their total required execution time units (aka burst time),
simulate the Round-Robin scheduling for a given time quantum by printing the Gantt Chart. Round-Robin
scheduling requires a queue/circular-list (it's called as Ready Queue) of processes to be scheduled. The processor
can execute one process at a time. The scheduling allows a process to execute for one time quantum and pre-
empts to allow the next process to execute for a time quantum and so on. Once a process is pre-empted after its
time quantum, it'll go the back of the Ready Queue. Whenever a process executes for its burst time, it should be
removed from the Ready Queue. Gantt Chart is a timeline listing of the processes executed (in order) for the given
list of processes. Just print the process name (Pi) in the Gantt Chart, which is essentially the order in which
processes were scheduled to execute. Implement the Ready Queue using a Circular List.
P1 6
P2 5
P3 2
P4 3
P5 7
Time Quantum: 2
Gantt Chart: P1, P2, P3, P4, P5, P1, P2, P4, P5, P1, P2, P5, P5
Input Format: Begins with n number of processes, and following line to have burst time of n processes separated
by spaces. Last line to have time quantum. All the numbers are positive integers not exceeding 100.
Output Format: Processes Pi scheduled (in order) separated by spaces, where i is the index of the process among
processes P1, P2, ..., Pn.
Input: Output:
5 P1 P2 P3 P4 P5 P1 P2 P4 P5 P1 P2 P5 P5
65237
2
DATA STRUCTURES LABORATORY July 2015
Question 9 (a)
Write a program to perform the following operations using Doubly Linked List:
I. Insert an element at a specified position (position 0 is the beginning position, and anything greater than or equal
to the size of the list would be taken as the last position).
D. Delete the specified element from the list (the key would be the value of the node, and do nothing if the node
does not exist).
Input Format:
An input begins with the code of the operation as coded in the question followed by the necessary inputs in the
same line separated by spaces.
Output Format:
Print the linked list ("Empty List" should be printed in case of empty list) where the items are separated by spaces.
Input: Output:
I 0 123 123
D 243 345
X
DATA STRUCTURES LABORATORY July 2015
Question 9 (b)
Input Format: Input begins with a positive integer n, indicating the number of integers to be inserted into the BST.
The following line to have n integers separated by spaces, which are to be inserted into the BST in the given order.
The third line will have an integer m, which needs to be searched in the BST for its existence.
Output Format: In the first line, n integers found out of in-order traversal of the BST to be printed in a single line
separated by spaces. True/False to be printed in the second line indicating whether the integer m exists in the BST
or not.
Input: Output:
9 12 23 34 45 56 67 78 89 90
34 90 12 89 78 45 56 23 67 True
89
Input: Output:
2 12 90
90 12 False
89
DATA STRUCTURES LABORATORY July 2015
Question 10 (a)
Given a string of characters, construct a binary tree of the string where each character is the key of a node and the
tree would be in the following order. First character would be the root of the tree. Next 2 characters are the left
and right children of the tree, which form the depth level 1. The following four characters form the depth level 2
such that 4th and 5th characters are children of 2nd character, and 6th and 7th characters are children of 3rd
character. Similarly, the following 8 characters (8th to 15th in the string) form the depth level 3 in that order and
so on until all the characters in the string are consumed. In general, left and right children of character at i would
be characters at 2*i and 2*i+1. Parent of a character at j would be floor(j/2). Print the strings generated out of
preorder, inorder and postorder traversals of the constructed binary tree.
Preorder: abdecf
Inorder: dbeafc
Postorder: debfca
Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.
Input: Output:
Inorder: dbeafc
Postorder: debfca
Input: Output:
Inorder: euresniptiysv
Postorder: erusinetyivsp
DATA STRUCTURES LABORATORY July 2015
Question 10 (b)
Implement Priority Queue using min-heap so that the Remove method of the queue always removes the least
valued item in the queue.
R. Remove least-valued integer from the Priority Queue and Print it in a new line. Do nothing in case of underflow.
Input Format: An input begins with the code of the operation as coded in the question followed by the necessary
inputs in the same line separated by spaces.
Input: Output:
A 21 -23
A -23 -100
A 123 0
A 1000 21
R 123
A -100 1
A0 1000
A1
X
DATA STRUCTURES LABORATORY July 2015
Question 11 (a)
Write a program to perform the following operations using Singly Linked List:
3. Insert an element at a specified position (position 0 is the beginning position, and anything greater than or equal
to the size of the list would be taken as the last position).
4. Delete the specified element from the list (the key would be the value of the node, and do nothing if the node
does not exists).
Input Format:
An input begins with the code of the operation as coded in the question followed by the necessary inputs in the
same line separated by spaces.
Output Format:
Display the linked list after every operation except the Exit operation ("Empty List" should be printed in case of
empty list) where the items are separated by spaces.
Input: Output:
1 123 123
5
DATA STRUCTURES LABORATORY July 2015
Question 11 (b)
Given an undirected graph in the form of an adjacency matrix, find the number of components the given graph has
using DFS (or BFS) graph traversal technique.
Input Format: Input begins with n indicating the number of vertices the undirected graph has (1 <= n <= 10). The
following n lines to have the adjacency matrix of the graph where a '1' means there is an edge and '0' means there
is no edge between the vertices in question. Because it is an undirected graph, it is assured that the adjacency
matrix will be a symmetric matrix.
Output Format: Print the number of components the given graph has.
Input: Output:
3 3
000
000
000
3 2
010
100
000
10 5
0110000000
1010000000
1101000000
0010100000
0001000000
0000000000
0000000000
0000000000
0000000001
0000000010
DATA STRUCTURES LABORATORY July 2015
Question 12 (a)
Implement a Stack class with Push and Pop methods (and any other methods required for your implementation).
Given an algebraic expression with multiple kinds of brackets, check whether the brackets are matching using the
Stack class.
You can safely ignore all the symbols other than brackets. You are free to use multiple instances of the class Stack,
if it is needed.
Sample Testcase:
Input:
{a+b[c-d]+[(e*f(g^2))]}
{a+b[c-d]+[(e*f(g^2)])}
{a+b[c-d]+[(e*f(g^2))]
Output:
True
False
False
DATA STRUCTURES LABORATORY July 2015
Question 12 (b)
Input Format: Input begins with a positive integer n, indicating the number of integers to be inserted into the BST.
The following line to have n integers separated by spaces, which are to be inserted into the BST in the given order.
The third line will have an integer m, which needs to be searched in the BST for its existence.
Output Format: In the first line, n integers found out of in-order traversal of the BST to be printed in a single line
separated by spaces. True/False to be printed in the second line indicating whether the integer m exists in the BST
or not.
Input: Output:
9 12 23 34 45 56 67 78 89 90
34 90 12 89 78 45 56 23 67 True
89
Input: Output:
2 12 90
90 12 False
89
DATA STRUCTURES LABORATORY July 2015
Question 13 (a)
Implement a Queue class with Add and Remove methods (and any other methods required for your
implementation).
Suppose you are receiving a sequence of items, which has two kinds of items. You are asked to make pairs of items
where a pair has both kinds of items. Implement it using the Queue class where the sequence of items is a string of
characters and the kinds are identified by whether the character is a digit or an English letter. You are free to use
multiple instances of the class Queue, if it needs. Assume there is equal number of either kind of characters in the
string. Output the pairs, one pair per line.
Input:
ab1cd2345efg67
Output:
a1
b2
c3
d4
e5
f6
g7
DATA STRUCTURES LABORATORY July 2015
Question 13 (b)
Given an undirected graph in the form of an adjacency matrix, find the number of components the given graph has
using DFS (or BFS) graph traversal technique.
Input Format: Input begins with n indicating the number of vertices the undirected graph has (1 <= n <= 10). The
following n lines to have the adjacency matrix of the graph where a '1' means there is an edge and '0' means there
is no edge between the vertices in question. Because it is an undirected graph, it is assured that the adjacency
matrix will be a symmetric matrix.
Output Format: Print the number of components the given graph has.
Input: Output:
3 3
000
000
000
3 2
010
100
000
10 5
0110000000
1010000000
1101000000
0010100000
0001000000
0000000000
0000000000
0000000000
0000000001
0000000010
DATA STRUCTURES LABORATORY July 2015
Question 14 (a)
Write a program to perform the following operations using Doubly Linked List:
I. Insert an element at a specified position (position 0 is the beginning position, and anything greater than or equal
to the size of the list would be taken as the last position).
D. Delete the specified element from the list (the key would be the value of the node, and do nothing if the node
does not exist).
Input Format:
An input begins with the code of the operation as coded in the question followed by the necessary inputs in the
same line separated by spaces.
Output Format:
Print the linked list ("Empty List" should be printed in case of empty list) where the items are separated by spaces.
Input: Output:
I 0 123 123
D 243 345
X
DATA STRUCTURES LABORATORY July 2015
Question 14 (b)
Given n number of processes (P1, P2, ..., Pn) with their total required execution time units (aka burst time),
simulate the Round-Robin scheduling for a given time quantum by printing the Gantt Chart. Round-Robin
scheduling requires a queue/circular-list (it's called as Ready Queue) of processes to be scheduled. The processor
can execute one process at a time. The scheduling allows a process to execute for one time quantum and pre-
empts to allow the next process to execute for a time quantum and so on. Once a process is pre-empted after its
time quantum, it'll go the back of the Ready Queue. Whenever a process executes for its burst time, it should be
removed from the Ready Queue. Gantt Chart is a timeline listing of the processes executed (in order) for the given
list of processes. Just print the process name (Pi) in the Gantt Chart, which is essentially the order in which
processes were scheduled to execute. Implement the Ready Queue using a Circular List.
P1 6
P2 5
P3 2
P4 3
P5 7
Time Quantum: 2
Gantt Chart: P1, P2, P3, P4, P5, P1, P2, P4, P5, P1, P2, P5, P5
Input Format: Begins with n number of processes, and following line to have burst time of n processes separated
by spaces. Last line to have time quantum. All the numbers are positive integers not exceeding 100.
Output Format: Processes Pi scheduled (in order) separated by spaces, where i is the index of the process among
processes P1, P2, ..., Pn.
Input: Output:
5 P1 P2 P3 P4 P5 P1 P2 P4 P5 P1 P2 P5 P5
65237
2
DATA STRUCTURES LABORATORY July 2015
Question 15 (a)
Write a generic method to count the number of elements in a collection (you may use ArrayLists) that
are
1. Odd integers
2. Prime numbers
Input Format: Begins with n number signifying the number of Integers in the collection
Input: Output:
Question 15 (b)
Given a text file containing some text construct a multi list such that each node of the main list is a word from the
text. This list should be in alphabetical order. Each node of the sublist should have the positions at which the
particular word occurs in the list. Implement using a generic list.
Input Format:
Output Format:
Word Position(s)
betty bought some butter but the butter was bitter betty bought some better butter to make the bitter butter
better
Output:
better 13 20
betty 1 10
bitter 9 18
bought 2 11
but 5
butter 4 7 14 19
make 16
some 3 12
the 6 17
to 15
was 8