You are on page 1of 30

DATA STRUCTURES LABORATORY July 2015

Question 1 (a)

The following interface is given to you

interface TimeInterface
{
void display();
int diffInsec(TimeInterface other);
void addTime(int hr, int min);
int timeInsec();
}

Develop two classes implementing the above interface

a) A class that stores time in sec.


b) A class that stores time in hr and min.

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)

In the Client file

Create four instances of MyTime


Create four instances of MyHourTime
Store the references in an array of TimeInterface
Print the original array using the Arrays.toString(.) method.
Note: You must override the toString() method of the MyTime and MyHourTime classes

Step 2:

Implement Map Function:

Write a class called Util


In the class write a function called mymap
o TimeInterface[] mymap(TimeInterface[] a, UnaryOp unaryop)
The mymap function should take an array of TimeInterfaces and return a new array of TimeInterfaces

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:

1. Append an element at the end of list.

2. Insert an element at the beginning.

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).

5. Exit the application on an invalid choice of operation.

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.

Sample Test case:

Input: Output:

4 123 Empty List

1 123 123

2 234 234 123

3 4 345 234 123 345

3 1 456 234 456 123 345

4 123 234 456 345

4 123 234 456 345

1 567 234 456 345 567

2 789 789 234 456 345 567

3 0 890 890 789 234 456 345 567

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.

1. Append an element at the end of list (do nothing in case of overflow).

2. Insert an element at the beginning (do nothing in case of overflow).

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).

5. Exit the application on 6 or any invalid choice of operation.

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.

Sample Test case:

Input: Output:

3 4 123 Empty List

1 123 123

2 234 234 123

3 4 345 234 123 345

3 1 456 234 123 345

4 123 234 345

4 123 234 345

1 567 234 345 567

2 789 234 345 567

3 0 890 234 345 567

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.

Eg: String "abcdef"

Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input Format: A string of alphanumeric characters.

Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.

Sample Test case:

Input: Output:

abcdef Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input: Output:

pesuniversity Preorder: peuernsisityv

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.

Write a program to perform the following operations on your Priority Queue:

A. Add an integer into the Priority Queue.

R. Remove least-valued integer from the Priority Queue and Print it in a new line. Do nothing in case of underflow.

X. Exit at option 'X' or an invalid choice of operation.

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 removed items in a new line.

Sample Test case:

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.

Eg: String "abcdef"

Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input Format: A string of alphanumeric characters.

Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.

Sample Test case:

Input: Output:

abcdef Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input: Output:

pesuniversity Preorder: peuernsisityv

Inorder: euresniptiysv

Postorder: erusinetyivsp
DATA STRUCTURES LABORATORY July 2015

Question 4 (b)

Construct a Binary Search Tree (BST) with a sequence of n integers.

Print the integers in their in-order traversal.

Search whether a given integer exists in the BST or not.

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.

Sample Test case:

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).

X. Exit at option 'X' or an invalid choice of operation.

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.

Sample Test case:

Input: Output:

I 0 123 123

I 0 234 234 123

I 10 345 234 123 345

D 112 234 123 345

D 123 234 345

D 243 345

D 345 Empty List

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.

Write a program to perform the following operations on your Priority Queue:

A. Add an integer into the Priority Queue.

R. Remove least-valued integer from the Priority Queue and Print it in a new line. Do nothing in case of underflow.

X. Exit at option 'X' or an invalid choice of operation.

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 removed items in a new line.

Sample Test case:

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.

Required output is Boolean: True or False.

The kinds of brackets: () [] {}

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.

Eg: List of 5 processes with their burst time.

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.

Sample Test case:

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.

Sample Test case:

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.

Sample Test case:

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.

Eg: String "abcdef"

Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input Format: A string of alphanumeric characters.

Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.

Sample Test case:

Input: Output:

abcdef Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input: Output:

pesuniversity Preorder: peuernsisityv

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.

Eg: List of 5 processes with their burst time.

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.

Sample Test case:

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).

X. Exit at option 'X' or an invalid choice of operation.

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.

Sample Test case:

Input: Output:

I 0 123 123

I 0 234 234 123

I 10 345 234 123 345

D 112 234 123 345

D 123 234 345

D 243 345

D 345 Empty List

X
DATA STRUCTURES LABORATORY July 2015

Question 9 (b)

Construct a Binary Search Tree (BST) with a sequence of n integers.

Print the integers in their in-order traversal.

Search whether a given integer exists in the BST or not.

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.

Sample Test case:

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.

Eg: String "abcdef"

Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input Format: A string of alphanumeric characters.

Output Format: Strings generated out of preorder, inorder and postorder traversals of the binary tree constructed.

Sample Test case:

Input: Output:

abcdef Preorder: abdecf

Inorder: dbeafc

Postorder: debfca

Input: Output:

pesuniversity Preorder: peuernsisityv

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.

Write a program to perform the following operations on your Priority Queue:

A. Add an integer into the Priority Queue.

R. Remove least-valued integer from the Priority Queue and Print it in a new line. Do nothing in case of underflow.

X. Exit at option 'X' or an invalid choice of operation.

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 removed items in a new line.

Sample Test case:

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:

1. Append an element at the end of list.

2. Insert an element at the beginning.

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).

5. Exit the application on an invalid choice of operation.

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.

Sample Test case:

Input: Output:

4 123 Empty List

1 123 123

2 234 234 123

3 4 345 234 123 345

3 1 456 234 456 123 345

4 123 234 456 345

4 123 234 456 345

1 567 234 456 345 567

2 789 789 234 456 345 567

3 0 890 890 789 234 456 345 567

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.

Sample Test case:

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.

Required output is Boolean: True or False.

The kinds of brackets: () [] {}

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)

Construct a Binary Search Tree (BST) with a sequence of n integers.

Print the integers in their in-order traversal.

Search whether a given integer exists in the BST or not.

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.

Sample Test case:

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.

Sample Test case:

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.

Sample Test case:

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).

X. Exit at option 'X' or an invalid choice of operation.

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.

Sample Test case:

Input: Output:

I 0 123 123

I 0 234 234 123

I 10 345 234 123 345

D 112 234 123 345

D 123 234 345

D 243 345

D 345 Empty List

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.

Eg: List of 5 processes with their burst time.

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.

Sample Test case:

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

Output Format: The count of odd integers and prime number

Sample Test case:

Input: Output:

5 Number of odd integers : 3

56789 Number of prime numbers : 2


DATA STRUCTURES LABORATORY July 2015

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:

text file containing text(preferably with words repeated)

Output Format:

Word Position(s)

Sample Test case:

Input: (from text file)

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

You might also like