Thread: adding two polynomials

  1. #1
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40

    adding two polynomials

    Hi,

    I've already written some code to add two polynomials(one variable) stored in a linked list.

    The Basic Node structure looks like this:
    Code:
    struct node
    {
    	int coeff;
    	int deg;
            unsigned int add_flag;
    
    	struct node *next;
    	struct node *prev;
    };
    
    typedef struct node * nodeptr;
    Now what I do is that i check the degree of every node in both polynomials and if I find matching degrees, add them and store them in a third list, concurrently I set the add_flag of both nodes to 1.

    Then I run through both lists, and if i find a node who's add flag hasnt been set, I include that node in my resultant list as well. Then I sort the resultant list in decreasing order of degrees.

    This means that I traverse both lists several times, which in my mind makes my program pretty time complex.

    Is there any other simpler way to achieve the above addition?


    Also I have a personal problem, I know this sounds stupid, off-topic etc etc, also the moderators out here are free to do anything with me if they want.

    I have 20 days, in which I have to compulsarily clear 3 exams, for people who are interested they're:-

    1.Mathematics
    2.Mechanics
    3.Engineering Drawing/Graphics

    I love programming and I want to continue my engineering course, but i just cannot get myself to study these subjects, also I live in India, where the concept of optional subjects just isnt there..I only have one more chance after this to clear these subjects, otherwise I will fail one more year(I already have failed once) and I wont be able to bear the shame.

    Any words of advice(or any flames for my lame posting) are welcome.

    I am again very sorry, but I'm deeply depressed.
    Thanks,
    Sahil

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Wouldn't it be easier to do what, for instance, Matlab does and simply store the polynomials as an array? The length of the array is the degree of the polynomial plus one, and each element of the array is the coefficient.
    e.g.

    x^3 + 2x + 4 ==> [1, 0, 2, 4]

    Then you can just add the corresponding elements of the arrays. You could also use a linked list the same way, if you want to allocate space at runtime and you don't know how much you'll need, however, I'd recommend an array, it's really the most appropriate way to represent a polynomial and lends itself nicely to polynomial operations - addition/subtraction, multiplication (by convolution of vectors) etc.

    Regarding exams, don't get depressed over them. Stress does not help performance. Just do your best, that's all anyone can ask of you, and don't worry about what if this, what if that etc until you actually know your results. Just concentrate on learning what you need to know and don't worry about anything else.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    thanks to both of you.
    Thanks,
    Sahil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  2. Multiplying Two Polynomials
    By CaptainMorgan in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 02:34 PM
  3. Java: Sorting and Adding a Row in Table
    By alphaoide in forum Tech Board
    Replies: 4
    Last Post: 08-12-2005, 08:22 PM
  4. Polynomials with linked Lists
    By shaheedpak in forum C++ Programming
    Replies: 0
    Last Post: 09-26-2001, 11:10 AM
  5. Generating Polynomials using Linked Lists
    By shaheedpak in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2001, 01:22 PM