GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Aug-2009, 17:01
ziorus ziorus is offline
Awaiting Email Confirmation
 
Join Date: Aug 2009
Posts: 11
ziorus is on a distinguished road

Linked list question


Hello 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:
void Number::Add(const Number& Num)
{
	digit *temp = head;
	int newLength = GetLength();
	digit *temp2 = Num.head;
	int checkLength = Num.GetLength();
	int carry = 0;
	int num1;
	int num2;

	if (IsEmpty() == 1)
	{
		cout << "One of the list contains no Numbers" << endl;
	}
	else
	{
		if (checkLength > newLength)
		{
			newLength = checkLength;
		}
		
		for(int i = 0; i < newLength; i++)
		{
			if (temp == NULL)
			{
				num1 = 0;
				digit *newPtr = new digit;
				newPtr->prev = head;
				newPtr->next = temp;
				newPtr->Value = carry;
				temp = newPtr;
				
				size = size + 1;
			}

			else
				num1 = temp->Value;

			if(temp2 == NULL)
				num2 = 0;
			else
				num2 = temp2->Value;

			
				if (num1 + num2 + carry <= 9)
				{
					temp->Value = num1 + num2 + carry;
					temp = temp->next;
					if(temp2 != NULL)
						temp2 = temp2->next;
					carry = 0;
				
				}
				else
				{
					temp->Value = num1 + num2 + carry - 10;
					temp = temp->next;
					if (temp2 != NULL)
						temp2 = temp2->next;
					carry = 1;
				}
			
		}


		if (temp == NULL  && carry == 1)
		{
			temp = head;
			temp = temp->next;
			newLength = newLength + 1;
			digit *newPtr = new digit;
			size = newLength;
			
			if (size > 2 )
			{
				newPtr->Value = carry;
				head->next = temp;
				newPtr->next = NULL;
				if (head == NULL)
					head->next = temp;
				else
				{
					while (temp->next !=NULL)
					{
					temp = temp->next;
					}
					temp->next = newPtr;
				}
				newPtr->prev = temp;
			}
			else
			{
				newPtr->Value = carry;
				newPtr->prev = head;
				newPtr->next = temp;
				if (head != NULL)
					head->next = newPtr;
			}
		}
	}
}
  #2  
Old 27-Aug-2009, 06:01
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: linked list question


Quote:
Originally Posted by ziorus
Can anyone see where I am going wrong?

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:
Add(Number const& N) {
  Number *pBigger, *pSmaller;
  if(this->size() < N.size())
  {
    pSmaller = this;
    pBigger = &N;
  }
  else //...the other way around.
  ...
}

BTW, I didn't notice how you were handling negative "Number"s in your code.


MxB
  #3  
Old 03-Sep-2009, 03:23
ziorus ziorus is offline
Awaiting Email Confirmation
 
Join Date: Aug 2009
Posts: 11
ziorus is on a distinguished road

Re: Linked list question


Thanks 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:
if (checkLength > newLength)
		{
			newLength = checkLength;
			temp = Num.head;
			temp2 = head;
			this->size = newLength;
			this->head = Num.head;
		}

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  
Old 03-Sep-2009, 18:51
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Linked list question


Quote:
Originally Posted by ziorus
I did this...

CPP / C++ / C Code:
if (checkLength > newLength)
		{
			newLength = checkLength;
			temp = Num.head;
			temp2 = head;
			this->size = newLength;
			this->head = Num.head;
		}

I have to learn to use "this" better.
In the code provided, this isn't required at all. The compiler's front-end will check increasingly broader scopes to find where size & head are declared. Since neither is defined locally, the next scope considered is class scope, & since these members are defined there, searching ends.

Depending upon how long you stay with programming & programming in C++ in particular, you will run into two schools of thought:
  • Use this as sparingly as possible. There are some situations where it is required; everywhere else is optional.
  • Some think that copious of this helps provide context.
Each of these points can be considered opposite ends of a continuum, & most programmers (especially if they have experience with the language...) will err towards brevity.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 11:32.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.