You are on page 1of 4

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 : Linked list

Mandatory:

1. Write a program to implement addition of two – two variable polynomials - using linked list.

Input format:
• First line would contain two numbers (m and n) (separated by tab), first number (m)
represents total number of terms in first polynomial and second number (n) represents
total number of terms in second polynomial.
• Rest of the m + n lines would contain three numbers each (separated by tab). Each line
would represent one term. First number represents coefficient of the term, second
number represents the power of first variable and third number represents power of the
second variable.

For example,
7x2y3 + 3xy2 + 3y + 2x + 5
4x2y3 + 8x2y + 6y + x
Above two polynomials will be represented in input as follow:
5 4
7 2 3
3 1 2
3 0 1
2 1 0
5 0 0
4 2 3
8 2 1
6 0 1
1 1 0

Output format:
• First line in output should contain number (n) representing number of terms in the sum
of two input polynomials.
• Next n lines would contain three numbers each (separated by tab). Each line should
represent one term. First number represents coefficient of the term, second number
represents the power of first variable and third number represents power of the second
variable.
• Terms in the output should be in order according to following rule:
◦ If output containts two terms xayb and xpyq then xayb should appear before xpyq in
the output if (a>p) or if (a==p and b>q)

Range:
0 <= m, n <= 10000
-10000 <= coefficient of each term <= 10000
-10000 <= power of each term <= 10000

Example 1:

7x2y3 + 3xy2 + 3y + 2x + 5
4x2y3 + 8x2y + 6y + x

Input:
5 4
7 2 3
3 1 2
3 0 1
2 1 0
5 0 0
4 2 3
8 2 1
6 0 1
1 1 0

Output:
6
11 2 3
8 2 1
3 1 2
3 1 0
9 0 1
5 0 0

Practice Questions:

2. Write a program to implement addition of two – three variable polynomials - using linked list.

Input format:
• First line would contain two numbers (m and n) (separated by tab), first number (m)
represents total number of terms in first polynomial and second number (n) represents
total number of terms in second polynomial.
• Rest of the m + n lines would contain four numbers each (separated by tab). Each line
would represent one term. First number represents coefficient of the term, second
number represents the power of first variable, third number represents power of the
second variable and fourth number represents power of the third variable..

For example,
7x2y3z4 + 3xy2z + 3y + 2x + 5
4x2y3z4 + 8x2yz2 + 6y + x

Above two polynomials will be represented in input as follow:


5 4
7 2 3 4
3 1 2 1
3 0 1 0
2 1 0 0
5 0 0 0
4 2 3 4
8 2 1 2
6 0 1 0
1 1 0 0

Output format:
• First line in output should contain number (n) representing number of terms in the sum
of two input polynomials.
• Next n lines would contain four numbers each (separated by tab). Each line should
represent one term. First number represents coefficient of the term, second number
represents the power of first variable, third number represents power of the second
variable and fourth number represents power of the third variable.
• Terms in the output should be in order according to following rule:
◦ If output containts two terms xaybzc and xpyqzr then xaybzc should appear before
xpyqzr in the output if (a>p) or if (a==p and b>q) or if (a==p and b==q and c>r)

Range:
0 <= m, n <= 10000
-10000 <= coefficient of each term <= 10000
-10000 <= power of each term <= 10000

Example 1:
7x2y3z4 + 3xy2z + 3y + 2x + 5
4x2y3z4 + 8x2yz2 + 6y + x

Input:
5 4
7 2 3 4
3 1 2 1
3 0 1 0
2 1 0 0
5 0 0 0
4 2 3 4
8 2 1 2
6 0 1 0
1 1 0 0

Output:
6
11 2 3 4
8 2 1 2
3 1 2 1
3 1 0 0
9 0 1 0
5 0 0 0

3. Identify whether given linked list has loop or not. Remove loop from linked list if present.

A linked list is said to have loop if last node of a linked list points to one of the node in the
linked list instead of terminating the linked list. (If linked list is created in C language, this
means last node point to some other node in the list instead of having null pointer).

Before you rush to uncle google to find the solution to this problem, wait for a second and try
to figure this out by yourself. :)

No input/output format is specified for this problem. Feel free to implement it your way.

You might also like