Dsa Lab - Assignment-Queue Submission Deadline (On or Before) : 15th September 2021, 5 PM Policies For Submission and Evaluation
Dsa Lab - Assignment-Queue Submission Deadline (On or Before) : 15th September 2021, 5 PM Policies For Submission and Evaluation
Dsa Lab - Assignment-Queue Submission Deadline (On or Before) : 15th September 2021, 5 PM Policies For Submission and Evaluation
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.
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.
You have to repeat this process until the array value at index k becomes zero. i.e. A[k] = 0
Input format:
First line consists of two integers n and k, where n is the size of an array and k is the given
index.
Output format:
For each test case, print required the answer on a new line.
Constraint:
1 <= T <= 10
0 <= k < n
Sample Input
1
52
26345
Sample output
12