Re: Is there a more elegant way to populate a linked list ?

From: Leor Zolman (leor_at_bdsoft.com)
Date: 05/21/04


Date: Fri, 21 May 2004 09:06:40 -0400

On Fri, 21 May 2004 17:22:38 +0700, "Wayne McDermott"
<elhmbre@ozemail.com.au> wrote:

>Evening All,
>
>I have a class that represents a sequence of polynomial expressions. Each
>polynomial consists of a coefficient and a power and the expression is
>represented as a linked list of structures. One of my class constructors
>accepts polynomials as pairs of values from a text file and is included
>below. It works but I cannot help but feel there is a more elegant and
>generic way of populating structures without resorting to hardwired values
>such as "i" which identifies an integer as a coefficient or power depending
>on its place in the file. Any ideas ?
>
>/*
>Constructor that accepts a file.
>Pre : A correctly formatted file is passed to the constructor. The file
>consists of number pairs where the first number represents a polynomial
>coefficient and the second number represents the polynomial power.
>Post : An object of type Polynomial consisting of a linked list of nodes
>is created.
>*/
>Polynomial::Polynomial(ifstream& InputFile)
>{
>Polynomial p; //Create an object of type Polynomial
>node_t node;
>int value;
>int i = 1; //A counter used to determine if the value read from file is a
>coefficient or a power
>cout << "File Constructor has been called" << endl; //Just a test
>if (!InputFile)
>{
>cerr << "Unable to open file.\n";
>}
>while (InputFile >> value)
>{
>if (i == 1)
>{
>node.coefficient = value;
>cout << "coefficient is " << node.coefficient << endl;
>}
>else // i = 2 so value must be a power
>{
>node.power = value;
>cout << "power is " << node.power << endl;
>}
>if (i > 1)
>p.Insert(node);
>if (i == 1)
>i++;
>else
>i = 1; //We have inserted a node so reset the counter to 1
>}
>p.ToFirst();
>if (p.CurIsEmpty())
>cout << "File is empty or does not contain valid values" << endl;
>}
>

Off-hand, I don't see the reason for "i" at all.

If it were me, I'd format the input in some human-readable pair format,
such as:
        (n,n) (n,n) (n,n)
or
        n,n n,n n,n
or
        (n n)(n n)
or anything at all that sets pairs apart from each other to avoid errors
such as
        1 2 4 6 9 11 12 4 6 3 18
where you'd be hard pressed to know where the missing value was supposed to
go.

Then, I'd read the input /a pair at a time/, validate that a pair was read,
and repeat until EOF. Nowhere would I need a counter; I'd simply be reading
two values per loop iteration, into separate variables. Or, to make the
loop code even simpler, define an extractor for your node_t type and read
directly into node in the loop. That would collapse your loop down to:

        while (InputFile >> node)
                p.insert(node);

(I'm not sure what that business at the end of your loop is about, but you
can adjust accordingly if really necessary.)

HTH,
        -leor
 

-- 
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix  
C++ users: download BD Software's free STL Error Message Decryptor at:
   www.bdsoft.com/tools/stlfilt.html


Relevant Pages

  • Re: Speed Up symbolic code
    ... and then i use these symbols as polynomials ... and then i use solve to find some coefficient in somewhere. ... Anyway since i use symbolic toolbox and i also use for loop and in each loop i call function which includes symbolic functions, more than 20 times it makes my code running very very slow. ...
    (comp.soft-sys.matlab)
  • Is there a more elegant way to populate a linked list ?
    ... polynomial consists of a coefficient and a power and the expression is ... accepts polynomials as pairs of values from a text file and is included ... such as "i" which identifies an integer as a coefficient or power depending ...
    (alt.comp.lang.learn.c-cpp)
  • Eigenvalues to find roots of polynomials
    ... Since the eigenvalues of this matrix coincide with the roots of the ... for i in c'rangeloop ... -- This method is for monic polynomials, ...
    (comp.lang.ada)
  • Re: ambiguities
    ... >2) polynomial can only have terms with powers that are positive ... Nobody thinks polynomials can only have positive powers: ... The constant term is both a term and a coefficient. ...
    (sci.math)
  • Re: Fundimentals of Elliptic Curves for a newbe
    ... > polynomials. ... Now the power of p in F_can simply be considered as a dimension. ... Not so, as it's a mathematical fact, and quite a deep result, that all ... and use analogs of the discrete log problem. ...
    (sci.crypt)