polynomials

15

Click here to load reader

Upload: jonghoon-park

Post on 17-Jun-2015

109 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Polynomials

Polynomials

Steve Paks

Page 2: Polynomials

Polynomials

• Representing Polynomials as Singly Linked Lists

A(x) = am-1xem-1+…+a0xe0

ai = nonzero coefficientei = nonnegative integer exponents, such that em-1 > em-2 > … > e0 ≥ 0

class PolyNode{int doef;int expon;

}

PolyNode a, b, d;

coef expon link

Page 3: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Singly Linked Lists(Cont’d)a = 3x14 + 2x8 + 1b = 8x14 – 3x10 + 10x6

3 14 2 8 1 0 null

8 14 -3 10 10 6 nulla

b

Page 4: Polynomials

Polynomials(Cont’d)

• Adding Polynomials

3 14 2 8 1 0 null

8 14 -3 10 10 6 nulla

b11 14

d(a) a.expon == b.expon

Page 5: Polynomials

Polynomials(Cont’d)

• Adding Polynomials(Cont’d)

3 14 2 8 1 0 null

8 14 -3 10 10 6 nulla

b11 14

d(b) a.expon < b.expon

-3 10

Page 6: Polynomials

Polynomials(Cont’d)

• Adding Polynomials(Cont’d)

3 14 2 8 1 0 null

8 14 -3 10 10 6 nulla

b11 14

d(c) a.expon > b.expon

-3 10 2 8

Page 7: Polynomials

Polynomials(Cont’d)• Erasing Polynomials

void erase(Polynomials ptr){Polynomials temp;while(ptr != null){

temp = ptr; ∙∙∙ (a)ptr = ptr.link; ∙∙∙ (b)

}}

nullptr

temp

(a)

nullptr

temp

(b)

ptr

Page 8: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists

– Reusing the nodes that they are no longer in use– Maintaining these nodes that have been “freed”– These nodes are reused if they are not emptied.– Using getNode(), retNode()

• getNode()– getting a node

• retNode()– adding a node to the node lists

– The avail list• collection of Polynomials

3 14 2 8 1 0

a a = 3x14 + 2x8 + 1

Page 9: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– getNode()

null

avail

(a)

null

avail

(b)

avail

node

node

Page 10: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– retNode()

ptr

avail

(a)

ptr

avail

Page 11: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– retNode()

(b)

ptr

avail

avail

Page 12: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– Erasing a circular list

null…ptr

avail

null…

ptr

avail

temp

Page 13: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– Erasing a circular list(Cont’d)

null…

ptr

avail

temp

null…

ptravailtemp

Page 14: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– Erasing a circular list(Cont’d)

null null …

null…

ptravailtemp

Page 15: Polynomials

Polynomials(Cont’d)

• Representing Polynomials as Circular Linked Lists(Cont’d)– Erasing a circular list(Cont’d)

temp

null

ptr avail

null

null null

tempptr avail