adding two polynomials
I've already written some code to add two polynomials(one variable) stored in a linked list.
The Basic Node structure looks like this:
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.
unsigned int add_flag;
struct node *next;
struct node *prev;
typedef struct node * nodeptr;
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:-
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.
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.
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.