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

Dharmsinh Desai University, Nadiad

Faculty of Technology
Department of Computer Engineering
B. Tech – CE, Semester: III
Subject: Data Structure and Algorithm
General Instructions:

• First things first, we take plagiarism very seriously for this course. So please do not copy
other’s work. If anyone is found guilty of plagiarism, very strict action would be taken. As
you have to submit your work online, its easy to detect plagiarism on soft copy. So be
aware!
• Unindented code really looks ugly and is hard to follow program logic. So please make sure
that your program is properly indented before submission.
• Names of the variables should be meaningful and should reflect its usage. Do not use
variable names like a, b, c.
• Divide your logic into multiple small functions whenever appropriate, do not write entire
logic in single function.
• Assume all the inputs to your program will always be within given range.
• Test your program with different test cases (different inputs) and try to make it robust before
submission.
• Submit solution for problem statements on moodle before deadline for respective lab work.
No excuse would be entertained after deadline.
• Name of the program file should be RollNoOnlyThreeDigits_ProgramNoTwoDigits.c. E.g
001_02.c – it means that this is the solution from roll no. CE001 for second program. If you
do not follow this naming convention, your submission will not be considered.
• DO NOT use statements like printf(“Enter number: ”) or printf(“Output is: ”). Output file
should be clean as explained in problem statement, without any clutter.
• Ask questions, whenever in doubt. If its related to programming, do some work to resolve it
by yourself first, if you can not resolve it then ask for help.

Lab 5: Queue

Mandatory:

1. Write a program to implement insert and delete operations on queue.

Insert operation would take one element and insert it at the end of the queue.
Delete operation would remove one element from front of the queue and return it.

Insert and delete operations have code associated with them as shown below:
QINSERT // Code 1
QDELETE // Code 2

Input format:
• Input file will contain multiple lines, each line will contain code of the operation to be
performed.
• If code is 1 (QINSERT), then next to code (separated by tab) in the same line there
would be number that has to be inserted.
• If code is 2 (QDELETE), then no other information would be there in that line

Output format:
• Output should be one line containing all items present in the queue (separated by
space) after executing all the input operations. First item in output should start from
front of the queue and should be in the sequence.

Clarification:
• If overflow or underflow happens at any point then output should be one line
containing “INVALID SEQUENCE OF COMMANDS”

Range:
1 <= maximum data items in queue at any given time <= 100000
-100000 <= each data item in queue <= 100000

Example 1:
Input:
1 7
1 9
1 11
1 5
2
2
1 3
1 10
2

Output:
5 3 10

Information for debugging your code – content of queue after execution of each input
command:
Initially queue would be empty
7
79
7 9 11
7 9 11 5
9 11 5
11 5
11 5 3
11 5 3 10
5 3 10

2. Write a program to implement insert and delete operations on circular queue.

Insert operation would take one element and insert it at the end of the queue.
Delete operation would remove one element from front of the queue and return it.

Insert and delete operations have code associated with them as shown below:
CQINSERT // Code 1
CQDELETE // Code 2
Input format:
• Input file will contain multiple lines, each line will contain code of the operation to be
performed.
• If code is 1 (CQINSERT), then next to code (separated by tab) in the same line there
would be number that has to be inserted.
• If code is 2 (CQDELETE), then no other information would be there in that line

Output format:
• Output should be one line containing all items present in the circular queue (separated
by space) after executing all the input operations. First item in output should start from
front of the queue and should be in the sequence.

Clarification:
• If overflow or underflow happens at any point then output should be one line
containing “INVALID SEQUENCE OF COMMANDS”

Range:
1 <= maximum data items in queue at any given time <= 100000
-100000 <= each data item in queue <= 100000

Example 1:
Input:
1 7
1 9
1 11
1 5
2
2
1 3
1 10
2

Output:
5 3 10

Information for debugging your code – content of queue after execution of each input
command:
Initially queue would be empty
7
79
7 9 11
7 9 11 5
9 11 5
11 5
11 5 3
11 5 3 10
5 3 10

Practice Questions:
3. Write a program to implement insert and delete operations on priority queue.

Insert operation would take one element and priority as input and will insert that element in
the queue according to its priority.
Delete operation would remove one element from front of the queue and return it. Element
with priority p can only be deleted if all elements with higher priority (<p) have already been
deleted.

Insert and delete operations have code associated with them as shown below:
PQINSERT // Code 1
PQDELETE // Code 2

Input format:
• Input file will contain multiple lines, each line will contain code of the operation to be
performed.
• If code is 1 (PQINSERT), then next to code (separated by tab) in the same line there
would be number that has to be inserted and next to that (separated by tab) in the same
line there would be priority of element to be inserted.
• If code is 2 (PQDELETE), then no other information would be there in that line.

Output format:
• Output should be one line containing all items present in the queue (separated by
space) after executing all the input operations. First item in output should start from
front of the queue and should be in the sequence.

Clarification:
• If overflow or underflow happens at any point then output should be one line
containing “INVALID SEQUENCE OF COMMANDS”

Range:
1 <= maximum data items in queue at any given time <= 100000
-100000 <= each data item in queue <= 100000
1 <= p (priority) <= 1000

Example 1:
Input:
1 7 2
1 9 1
1 11 4
1 5 3
2
2
1 3 1
1 10 2
1 15 3
2

Output:
10 5 15 11

Information for debugging your code – content of priority queue after execution of each input
command:
Initially queue would be empty
7
97
9 7 11
9 7 5 11
7 5 11
5 11
3 5 11
3 10 5 11
3 10 5 15 11
10 5 15 11

You might also like