Thursday, July 9, 2009

Using Linked Lists and STL in C++ to write a code , Who can Help ?

I want to write the following code in C++ , who can help me or give me some example or pre-written code about this topic ???





you will implement a class template called polynomial using linked lists and instantiate it by int. Each node in the linked list represents a term (such as -7x5) in the polynomial and contains at least the following member variables: the degree(the degree of the term) and coeff (the nonzero coefficient of the term). Note that you should only store nonzero coefficients. This means that if a coefficient goes to zero as a result of any operation, you must make sure that you delete that node.





In addition, you must make sure that the terms in the linked list are arranged in increasing order of their degree. Thus, the linked list corresponding to polynomial q(x) defined above must have nodes in the following order: node 1 representing term -5, node 2 representing the term 6x3, and node 3 representing the term -7x5.

Using Linked Lists and STL in C++ to write a code , Who can Help ?
I won't write the whole thing for you, but here is reasonable starting methodology for this type of problem :





// Begin Code





#include %26lt;list.h%26gt;








// Polynomial term definition ... you can use a class or typedef it


// instead if you'd prefer.





struct Polynomial_Term


{


int coefficient;


int exponent;


};





// Polynomial type definition using the list template of the STL.





typedef list%26lt;struct Polynomial_Term%26gt; Polynomial;





// End Code








So you'll declare a local variable of type Polynomial ... loop through adding your nodes using the list template methods (possibly tossing 0 coefficient terms at this stage before adding them to the Polynomial) ... then sort the Polynomial using the list template method and a sorting routine supplied by you (compare the exponents of both terms).





You can see the list template's header file in your compiler's includes ... but the STL headers are a bit messy and cumbersome. If you need resources to help you understand what methods are available and how to properly use them, do a Google search for "C++ STL list template".


No comments:

Post a Comment