You are on page 1of 8

COLLEGE OF ENGINEERING & TECHNOLOGY

UNIVERSITY OF SARGODHA

CE 315: Data Structure and Algorithms

Lab 5 Manual
Polynomial Evaluation using Linked Lists

Instructor & Demonstrator: Engr. Nauman Ahmad Tariq

Student Name OBAID UR REHMAN

Roll No. ELEN51F20R010

Date Performed

Marks obtained

Instructor Signature
CLO-1 Implement in Python programming language different
categories of data structures

CLO-3 Write lab notes, effective communication and analysis of the


given problem to perform in the laboratory environment.

OBJECTIVE:
Objective of this experiment is to evaluate the result of polynomial expression using circular linked list.

ABOUT THE EXPERIMENT:

Circular Linked List:


In the circular linked list the last node of the list contains the address of the first node and forms a circular
chain

Polynomial:
A polynomial equation is an equation that can be written in the form. axn + bxn-1 + . . . + rx + s = 0, where
a, b, . . . , r and s are constants. We call the largest exponent of x appearing in a nonzero term of a polynomial
the degree of that polynomial.
As with polynomials with one variable, you must pay attention to the rules of exponents and the order of
operations so that you correctly evaluate an expression with two or more variables. Evaluate x2 + 3y3for x
= 7 and y = −2. Substitute the given values for x and y. Evaluate4x2y – 2xy2 + x – 7 for x = 3 and y = −1.
When a term contains both a number and a variable part, the number part is called the "coefficient". The
coefficient on the leading term is called the "leading" coefficient.

In the above example, the coefficient of the leading term is 4; the coefficient of the second term is 3; the
constant term doesn't have a coefficient.
ALGORITHM:
Step 1: Start.
Step 2: Read a polynomial.
Step 3: Represent the polynomial using singly circular linked list.
Step 3: Evaluate the given polynomial
Step 4: Read two polynomials and find the sum of the polynomials.
Step 5: Stop
Python Program Code:
class Node:
def __init__(self, coefficient, exponent):
self.coefficient = coefficient
self.exponent = exponent
self.next = None

class PolynomialLinkedList:
def __init__(self):
self.head = None
self.tail = None

def insert_term(self, coefficient, exponent):


new_node = Node(coefficient, exponent)
if self.head is None:
self.head = self.tail = new_node
new_node.next = self.head
else:
self.tail.next = new_node
self.tail = new_node
self.tail.next = self.head

def display(self):
current = self.head
if current is None:
print("Polynomial is empty.")
else:
while True:
print(f"{current.coefficient}x^{current.exponent}", end=" + ")
current = current.next
if current == self.head:
break
print()

def evaluate(self, x):


result = 0
current = self.head
if current is not None:
while True:
result += current.coefficient * (x ** current.exponent)
current = current.next
if current == self.head:
break
return result

def add(self, other):


result = PolynomialLinkedList()
term1 = self.head
term2 = other.head

while term1 is not None:


added = False
while term2 is not None:
if term1.exponent == term2.exponent:
result.insert_term(term1.coefficient + term2.coefficient, term1.exponent)
added = True
break
term2 = term2.next
if term2 == other.head:
break

if not added:
result.insert_term(term1.coefficient, term1.exponent)

term1 = term1.next
if term1 == self.head:
break

term2 = other.head

while term2 is not None:


added = False
term1 = self.head

while term1 is not None:


if term1.exponent == term2.exponent:
added = True
break
term1 = term1.next
if term1 == self.head:
break

if not added:
result.insert_term(term2.coefficient, term2.exponent)

term2 = term2.next
if term2 == other.head:
break

return result

def read_polynomial():
poly = PolynomialLinkedList()
num_terms = int(input("Enter the number of terms in the polynomial: "))
for i in range(num_terms):
coefficient = int(input(f"Enter the coefficient for term {i + 1}: "))
exponent = int(input(f"Enter the exponent for term {i + 1}: "))
poly.insert_term(coefficient, exponent)
return poly

if __name__ == "__main__":
print("Polynomial 1:")
poly1 = read_polynomial()
print("\nPolynomial 2:")
poly2 = read_polynomial()

print("\nPolynomial 1:")
poly1.display()
print("\nPolynomial 2:")
poly2.display()

x = float(input("Enter the value of x to evaluate the polynomials: "))


result1 = poly1.evaluate(x)
result2 = poly2.evaluate(x)

print(f"\nEvaluation of Polynomial 1 at x = {x}: {result1}")


print(f"Evaluation of Polynomial 2 at x = {x}: {result2}")

sum_poly = poly1.add(poly2)
print("\nSum of Polynomial 1 and Polynomial 2:")
sum_poly.display()

OUTPUT:

Polynomial 1:
Enter the number of terms in the polynomial: 1
Enter the coefficient for term 1: 4
Enter the exponent for term 1: 3

Polynomial 2:
Enter the number of terms in the polynomial: 2
Enter the coefficient for term 1: 3
Enter the exponent for term 1: 5
Enter the coefficient for term 2: 1
Enter the exponent for term 2: 2

Polynomial 1:
4.0x^3 ->

Polynomial 2:
3.0x^5 -> 1.0x^2 ->
Enter the value of x to evaluate the polynomials: 2

Evaluation of Polynomial 1 at x = 2.0: 32.0


Evaluation of Polynomial 2 at x = 2.0: 100.0

Sum of Polynomial 1 and Polynomial 2:


4.0x^3 -> 3.0x^5 -> 1.0x^2 ->

Process finished with exit code 0


Assessment Rubric for Lab 5
Method of Evaluation: Lab report.

Outcomes Assessed:

CLO-1 Implement in Python programming language different categories of data structures


CLO-3 Write lab notes, effective communication and analysis of the given problem to perform
in the laboratory environment

Performance 5 Excellent 4 Good 3 Satisfactory 2-1 Needs Marks


Improvement
Realization of Fully understand & Close to fully Partially Unable to
Experiment able to illustrate understand & able understands & able understand & able
CLO-1 Polynomial to illustrate to illustrate to illustrate
Evaluation using Polynomial Polynomial Polynomial
Linked Lists Evaluation using Evaluation using Evaluation using
Linked Lists Linked Lists Linked Lists
Conducting Completely Close to Partially Unable to
Experiment understand & able completely understand & able understand & able
CLO-1 to demonstrate understand & able to demonstrate to demonstrate
syntax of to demonstrate syntax of syntax of
Polynomial syntax of Polynomial Polynomial
Evaluation using Polynomial Evaluation using Evaluation using
Linked Lists Evaluation using Linked Lists Linked Lists
Linked Lists
Data Collection Completely able to Close to Partially able to Unable to
and Data Analysis successfully completely able to successfully successfully
CLO-3 design and compile successfully design and compile design and compile
C++ programs for design and compile C++ programs for C++ programs for
Polynomial C++ programs for Polynomial Polynomial
Evaluation using Polynomial Evaluation using Evaluation using
Linked Lists Evaluation using Linked Lists Linked Lists
Linked Lists
Individual Work Completely able to Close to Partially able to Unable to execute
CLO-1 execute different completely able to execute different different programs
programs for execute different programs for for Polynomial
Polynomial programs for Polynomial Evaluation using
Evaluation using Polynomial Evaluation using Linked Lists
Linked Lists Evaluation using Linked Lists
Linked Lists

Total

You might also like