![]() |
|
#1
|
|||
|
|||
Linked list questionHello all,
I am trying to add two objects of integers. Object A: 92999 Object B: 22 My add method works when A is being added by B, but not the other way around because the amount of nodes is less. I should know how to add a node but this time I am already at the end of the list, which I thought would be easy. Can anyone see where I am going wrong? Many thanks. CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: linked list questionQuote:
The first thing that I noticed is that you didn't really offer enough code for us to accurately discern the implementation. I can't understand why you've named your class Number and not Integer, especially if it only deals with integers. Aside from that, why would an Integer class contain a list of integers? Wouldn't we think that it should be an object-oriented representation of a single integer and therefore offer attributes and operations consistent with the manifestation of that representation? If we accept that premise, then your "list" is not a member of your misnamed Number class. But, of course, it is a member, because you access "Num.head" in your Add operation. So, instead of being a representation of a single Number (or a single Integer), your class is really a collection of some count of integers, which makes both names, Number or Integer, inappropriate. Therefore, I'd recommend renaming your class as an IntList or something along those lines. I'd separate the "collection mechanics" from the representation of the integer. It also occurs to me that you're intending Number to be a "big number" (that would exceed the capabilities of the built-in types) where each "digit" is represented by the contents of the list. However, would we expect the contents of the list (the digits) to be organized such that the first node in the "big number" is the least significant digit or the most significant? It would be nice if posters would reduce the level of "detective story" they present in their posts by making more things clear to readers. However, your list "management" is "whack." Basically, you should have something that runs through the list, starting with assigning the head pointer to a "node" and traversing while the "node" pointer is not NULL. You seem to be attempting to do things with regard to the length of the "big numbers." I'd ask, why does length matter? I'm guessing that the answer is because you were taught "how to carry" by always seeing the larger number above the smaller number. If this is the case, and you feel compelled to apply your logic using the "bigger number on top," then internally assign a pointer to the larger and another to the smaller and work on them in that fashion rather than worrying about the "order" presented by the caller. CPP / C++ / C Code:
BTW, I didn't notice how you were handling negative "Number"s in your code. MxB |
|
#3
|
|||
|
|||
Re: Linked list questionThanks for the reply. Yea, the post was not complete. Next time when I need help I will be sure to include the complete program. After looking at what the program is doing and what you said about the naming, I agree that IntList or ListOfInts is much better. I did come up with a near solution as you did, just yours was much cleaners. I did this...
CPP / C++ / C Code:
I have to learn to use "this" better. I hope after using C++ more and more that my coding gets cleaner and more efficient as you have shown. Again thanks. |
|
#4
|
|||
|
|||
Re: Linked list questionQuote:
Depending upon how long you stay with programming & programming in C++ in particular, you will run into two schools of thought:
|
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Str_Misaligned in Double Link List | Peter_APIIT | C Programming Language | 1 | 29-Feb-2008 20:50 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Linked list memory question | dabigmooish | C++ Forum | 3 | 31-Oct-2006 00:05 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 01:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The