Professional Documents
Culture Documents
UNIVERSITY OF SARGODHA
Lab 5 Manual
Polynomial Evaluation using Linked Lists
Date Performed
Marks obtained
Instructor Signature
CLO-1 Implement in Python programming language different
categories of data structures
OBJECTIVE:
Objective of this experiment is to evaluate the result of polynomial expression using circular linked list.
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 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()
if not added:
result.insert_term(term1.coefficient, term1.exponent)
term1 = term1.next
if term1 == self.head:
break
term2 = other.head
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()
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
Outcomes Assessed:
Total