Dsa Lab - Assignment-Queue Submission Deadline (On or Before) : 15th September 2021, 5 PM Policies For Submission and Evaluation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DSA LAB – ASSIGNMENT- QUEUE

Submission deadline (on or before): 15th September 2021, 5 pm

Policies for Submission and Evaluation


You must submit your assignment in the moodle (eduserver) course page, on or before the
submission deadline. Your submission will also be tested for plagiarism, by automated tools. In
case your code fails to pass the test, you will be straightaway awarded F grade in the course.
Detection of ANY malpractice regarding the lab course will also lead to awarding an F grade.

Naming Conventions for submission:


Submit a single ZIP (.zip) file (do not submit in any other archived formats like .rar or .tar.gz).
The name of this file must be ASSG<Number>_<ROLLNO>_<FIRST-NAME>.zip (For
example: ASSG1_118cs0006_Abhishek.zip). DO NOT add any other files except your source
code (like temporary files, input files, etc.), into the zip archive.
The source codes must be named as
ASSG<Number>_<ROLLNO>_<FIRST-NAME>_<PROGRAM-NUMBER>.<extension> (For
example: ASSG1_118cs0006_Abhishek_1.c). If there is a part a and a part b for a particular
question, then, name the source files for each part separately as in
ASSG1_118cs0006_Abhishek_1b.cpp

If you do not conform to the above naming conventions, your submission might not be
recognized by some automated tools, and hence will lead to a score of 0 for the submission. So,
make sure that you follow the naming conventions.

Standard of Conduct
Violations of academic integrity will be severely penalized.
Each student is expected to adhere to high standards of ethical conduct, especially those related
to cheating and plagiarism. Any submitted work MUST BE an individual effort. Any academic
dishonesty will result in zero marks in the corresponding exam or evaluation and will be reported
to the department council for record keeping and for permission to assign F grade in the course.
Questions

1. QUEUE
a) Implement a queue using an array.
b) Implement a queue using a linked list.
Your program must support the following functions:
- enqueue(q, element) – puts the data specified by element at the rear end of the queue
specified by q.
- dequeue(q) – removes and returns the element at the front of the queue specified by q.
Return null (or some special value), if the queue is empty.
- peek(q) – returns the element at the front of the queue specified by q, without actually
removing the element from the queue. Return null (or some special value), if the queue is
empty.
- show(q) – displays all the data present in the queue.

Input-Output Format
The input consists of multiple lines, each one containing either one or two integers. The first
integer in the line can be 0, 1, 2, 3 or 4, and each one has its own meaning:
-The integer 0 means stop the program.
-The integer 1 means enqueue the next integer from the input into the queue. In this case,
the next integer (>= 0) is given on the same line as the 1, separated by a space.
-The integer 2 means dequeue and output the element from the front of the queue. Output
“EMPTY”, if the queue was originally empty.
-The integer 3 means peek and output the element from the front of the queue. Output
“EMPTY”, if the queue was originally empty.
-The integer 4 means show all elements in the queue. In this case, output all elements of
the stack on a single line, separated by space, starting with the element at the front. Output
“EMPTY”, if the queue was originally empty.
Sample Input Sample Output
1 45
1 65
1 74
1 25
1 98
3 45
3 45
2 45
3 65
1 17
4 65 74 25 98 17
2 65
2 74
2 25
2 98
2 17
2 EMPTY
3 EMPTY
4 EMPTY
0
Note:
The above input and output is for the linked list implementation of the queue. For the array
implementation, the very first line of input contains an integer c, 0 < c < 100, which is the
capacity of the queue. In this case, the enqueue operation must output “OVERFLOW” when
an element is being tried to be enqueued into an already full queue. Other input and output
formats remain the same.
2. PRIORITYQUEUE
Implement a priority queue using a heap.
Your program must support the following functions:
- insert(pq, element) – adds the data specified by element into the priority queue
specified by pq. The priority of the element will have been already set.
- remove(pq) – removes and returns the element with the highest priority from the priority
queue specified by pq. Return null (or some special value), if the priority queue is empty.
- peek(pq) – returns the element with the highest priority from the priority queue specified
by pq, without actually removing the element from the priority queue. Return null (or
some special value), if the priority queue is empty.
- increase_priority(pq, element, newpr) – change the priority of the data specified by
element, in the priority queue specified by pq, by assigning it the new priority, newpr. It
is guaranteed that newpr will be higher (in the sense of priority) than the original priority
of the data specified by element.

Input - Output Format


The input consists of multiple lines, each one containing either one or three integers.
The first integer in the line can be 0, 1, 2, 3 or 4, and each one has its own meaning:
- The integer 0 means stop the program.
- The integer 1 means insert the next integer from the input into the priority queue. In this
case, two more integers follow the 1, each separated by space. The first integer (>= 0) is
the data to be inserted. The second integer (>= 1) is the priority of this data item (1 being
the highest priority). Assign the priority to the data, and then insert this data into the
priority queue.
- The integer 2 means remove and output the element with the highest priority from the
priority queue (Output the priority of the element in parenthesis, separated by a space).
Output “EMPTY”, if the priority queue was originally empty.
- The integer 3 means peek and output the element with the highest priority from the
priority queue (Output the priority of the element in parenthesis, separated by a space).
Output “EMPTY”, if the priority queue was originally empty.
- The integer 4 means increase the priority of a datum in the priority queue. In this case,
two more integers follow the 4, each separated by space. The first integer (>= 0) is the
data (which will be present in the priority queue). The second integer (>= 1) is the new
increased priority. After this operation, the priority of this data should be the one
specified in this input.
Sample Input Sample Output
1 45 5
1 65 9
1 74 7
1 25 2
1 98 3
3 25 (2)
4 74 1
3 74 (1)
2 74 (1)
3 25 (2)
1 17 6
2 25 (2)
2 98 (3)
3 45 (5)
4 65 1
3 65 (1)
2 65 (1)
2 45 (5)
2 17 (6)
2 EMPTY
3 EMPTY
0
3. You are given an integer array A of size n and an index k (consider 0 based indexing).

Now all the indexes of the elements of the array i.e. 0 to n - 1 numbers are stored in the queue.

In one iteration:

1) Remove one element from front of the queue. Let us denote it by variable ele.

2) The number at index ele of the given array A i.e. A[ele] is decremented by one.

3) If A[ele] > 0, then add ele to the back of the queue.

You have to repeat this process until the array value at index k becomes zero. i.e. A[k] = 0

Output the number of iterations it takes until A[k] becomes 0.

Input format:

First line contains integer T, number of test cases.

Each test case consists of 2 lines:

First line consists of two integers n and k, where n is the size of an array and k is the given
index.

Second line contains n space separated integers denoting array A

Output format:

For each test case, print required the answer on a new line.

Constraint:

1 <= T <= 10

1 <= n <= 105

1 <= A[i] <= 109

0 <= k < n
Sample Input
1
52
26345
Sample output
12

You might also like