Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic

2 2 Queues as Linked Lists entry 1entry 2entry 3 front class Queue { public: Queue(); bool empty() const; Error_code append (const Queue_entry &item); Error_code serve( ); Error_code retrieve(Queue_entry &item) const; ~Queue(); Queue(const Queue &original); void operator =(const Queue &original); protected: Node *front, *rear; }; rear

3 3 append( ) implementation Error_code Queue :: append(const Queue_entry &item) { }

4 4 Implementing serve( ) Error_code Queue :: serve( ) { }

5 5 Polynomial Arithmetic p = 3x 2 + 4x -1 q = x 3 - 2x 2 - 4x + 7 r = p + q = x 3 + x 2 + 6 3 24 1-1 0 p coeffexponent Rules: 1)Order elements from highest exponent to lowest. 2)Terms with coefficient of zero are not included. 3)No two terms have the same exponent.

6 6 The Term struct struct Term { int degree; double coefficient; Term (int exponent = 0, double scalar = 0); }; Term :: Term(int exponent, double scalar) /* Post: The Term is initialized with the given coefficient and exponent, or with default parameter values of 0. */ { degree = exponent; coefficient = scalar; }

7 7 The Polynomial class typedef Term Queue_entry; class Polynomial: private Extended_queue { // Use private inheritance. public: void read( ); void print( ) const; void equals_sum(Polynomial p, Polynomial q); void equals_difference(Polynomial p, Polynomial q); void equals_product(Polynomial p, Polynomial q); Error_code equals_quotient(Polynomial p, Polynomial q); int degree( ) const; private: void mult_term(Polynomial p, Term t); };

8 Polynomial addition void Polynomial :: equals_sum(Polynomial p, Polynomial q) { clear( ); while (!p.empty( ) || !q.empty( )) { Term p_term, q_term; if (p.degree( ) > q.degree( )) { p.serve_and_retrieve(p_term); append(p_term); } else if (q.degree( ) > p.degree( )) { q.serve_and_retrieve(q_term); append(q_term); } else { p.serve_and_retrieve(p_term); q.serve_and_retrieve(q_term); if (p_term.coefficient + q_term.coefficient != 0) { Term answer_term(p_term.degree, p_term.coefficient + q_term.coefficient); append(answer_term); } } } }

9 9 The degree( ) function int Polynomial :: degree( ) const { if (empty( )) { return -1; } Term lead; retrieve(lead); return lead.degree; }


Download ppt "1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic."

Similar presentations


Ads by Google