![]() |
|
#1
|
|||
|
|||
Linked List help!Hopefully I can get a response so I can get this done, as I am tired...anyways I'll post my code up first
Here is my header file: CPP / C++ / C Code:
And here is my .cpp file just SCROLL PAST THIS for now (just in case you guys say to post all my code...ill tell the problem after this paste) CPP / C++ / C Code:
Now the problem lies within add_term CPP / C++ / C Code:
The problem is that, when I initialize a zero polynomial "poly1" and then go: poly1.add_term(-4,0).add_term(2,1).print(); It will show 2x - 4 instead of -4 + 2x Something in there is making it switch the order so when the new node is added to the linked list, it puts it before the existing one, rather than after... I am super tired and my brain cannot comprehend anything, so PLEASE be descriptive with how I can fix this. i'm really in a rut here guys... Thanks in advance, Kris EDIT: by the way, here is my test program: CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: Linked List help!krisopotamus,
First off, very nicely presented request. One thing though. When I tried to compile I got the error: Code:
Easy enough to take care of but I wanted to be sure you posted your correct h file. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#3
|
||||
|
||||
Re: Linked List help!Quote:
Yes. The "something" is you CPP / C++ / C Code:
Right? So you must think the other way around: instead of inserting the new element in front of the list, insert it at the end. In case this wasn't descriptive enough (meaning you can't change it for yourself), I'll post the solution in a few hours. But try it please Kind regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#4
|
|||
|
|||
Re: Linked List help!That's what I thought the problem was too, I spent some time trying to change it (before you posted), without any success.
I don't really know what I have to do to fix this |
|
#5
|
||||
|
||||
Re: Linked List help!Quote:
Hi. Sorry for replying so late. You can thank my ISP I'll write some code now or in the morning. First, be careful with a few things. First, your printing function isn't OK: CPP / C++ / C Code:
What's wrong here? Also, there is absolutely no need to make a static pointer! Hm, maybe a situation could rise when this would be needed, since it is possible!? Very unlikely. You dynamically allocate the memory, so it will be there. I think you misunderstood the use of static (or pointers maybe?). More about this some other time... The idea in adding values in a list at the end is to also store the last element of the list, not only the first (this also comes in handy when you wish to run through the list in both directions). I guess there are other ways to implement this solution too, but this seems the most straight forward. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#6
|
|||
|
|||
Re: Linked List help!I got it working. The reason why I used static was because the header file (with function prototypes) was already given to me, and i had to make my program corresponding to them. I got it to add it in the right spot.
Thanks for the help tho, but yay I'm done |
|
#7
|
||||
|
||||
Re: Linked List help!OK, from what I understood, you want to add the coefficients of the elements with the same exponent, right (logical). So, there shouldn't always be created a new T element. In order to make it resemble your original program, I left this part as it was (for now). You also delete the variable terms at some point - why? What was your reasoning there? (I really want to know)
I understand that you use terms as the pointer to the first element, right? I recommend you change the name to something more suggestive. Like I named the new member variable I added pLastTerm_ - p for Pointer, then a Camel notation for the words inside, and the last underscore to know it is a member variable (you can choose whatever convention you like). I also left the old name unchanged, so you can understand it easier. I will now present you the code I produced; it's not complete, in the sense I miss some logic in it - I didn't quite get what you were trying to do. I'll just let you read the comments, and we can talk tomorrow, I'm really sleepy So, add the new member variable: CPP / C++ / C Code:
Initialize it: CPP / C++ / C Code:
The code for add_term (with comments): CPP / C++ / C Code:
The new find_next_term : CPP / C++ / C Code:
Also, while you are at it, please take a look at your degree member function; I really don't understand what (or why) are you trying to do - why would you cycle through the list, only to return the last element (because you didn't have a last maybe? [edit] I see you got it working; great news! Maybe you could post your code, there were some parts I didn't get [/edit] HTH, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#8
|
||||
|
||||
Re: Linked List help!Quote:
You mean you couldn't add member functions, right? Yes, I thought that was the reason - but static isn't he solution. Your functions will work the same without static. I'll try to explain better tomorrow. Good night __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 14:13 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 01:34 |
| adding to linked list from external file | cghv | C Programming Language | 3 | 09-Mar-2005 13:36 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The