1 R Doubly linked lists
class Node:
def __init__(self, value = None):
self.value = value self.next_node = None self.previous_node = None
2 Using linked lists to represent polynomials
Write a program that implements a class Polynomial. An object of this class is built from a string that represents a polynomial, that is, a sum or difference of monomials.
β’ The leading monomial can be either an integer, or an integer followed by x, or an integer followed by xΛ followed by a nonnegative integer.
β’ The other monomials can be either a nonnegative integer, or an integer followed by x, or an integer followed by xΛ followed by a nonnegative integer.
Spaces can be inserted anywhere in the string.
A monomial is defined by the following class:
class Monomial:
def __init__(self, coefficient = 0, degree = 0):
self.coefficient = coefficient self.degree = degree self.next_monomial = None
A polynomial is a linked list of monomials, ordered from those of higher degree to those of lower degree. An implementation of the __str__() method allows one to print out a polynomial.
Next is a possible interaction.
1
$ python …
>>> from polynomial import *
>>> Polynomial(β-0β) Incorrect input
>>> Polynomial(β+0β)
Incorrect input
>>> Polynomial(β0x^-1β)
Incorrect input
>>> Polynomial(β2x + +2β) Incorrect input
>>> Polynomial(β2x + -2β) Incorrect input
>>> Polynomial(β2x – +2β)
Incorrect input
>>> poly_0 = Polynomial(β0β)
>>> print(poly_0)
0
>>> poly_0 = Polynomial(β0xβ)
>>> print(poly_0)
0
>>> poly_0 = Polynomial(β0x^0β)
>>> print(poly_0)
0
>>> poly_0 = Polynomial(β0x^5β)
>>> print(poly_0)
0
>>> poly_1 = Polynomial(βxβ)
>>> print(poly_1) x
>>> poly_1 = Polynomial(β1xβ)
>>> print(poly_1) x
>>> poly_1 = Polynomial(β1x^1β)
>>> print(poly_1) x
>>> poly_2 = Polynomial(β2β)
>>> print(poly_2)
2
>>> poly_2 = Polynomial(β2x^0β)
>>> print(poly_2)
2
>>> poly_3 = Polynomial(β1 + 2-3 +10β)
>>> print(poly_3)
10
>>> poly_4 = Polynomial(βx + x – 2x -3x^1 + 3xβ)
>>> print(poly_4)
0
>>> poly_5 = Polynomial(βx + 2 + x – x -3x^1 + 3x + 5x^0β)
>>> print(poly_5)
x + 7
>>> poly_6 = Polynomial(β-2x + 7x^3 +x – 0 + 2 -x^3 + x^23 – 12x^8 + 45 x ^ 6 -x^47β)
>>> print(poly_6)
-x^47 + x^23 – 12x^8 + 45x^6 + 6x^3 – x + 2
2
[SOLVED] COMP9021 Lab 8-Doubly linked lists
30.00 $
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
Securely Powered by:
- Lab_8-5okzq3.zip





