November 5th, 2002, 04:01 PM
|
#1 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Eastern Shore
Posts: 701
|
Needed to create a pointer based linked list, overloaded the += operator in my class to add a value to the list. The operator inserts the value in order.
It works fine until the first value entered is either 0 or 1. Notice, this is only a problem for the FIRST value entered. These numbers work fine at any other insertion point. Whats up, what am i obviously missing? Oh yea, I get a core dump when I try that. Code: bool Set::operator +=(const unsigned int value)
{
bool retValue = false;
//if value is not in the Set
if(!(*this%value)) //% checks to see whether or not the value is already in the list
{
if(head == NULL) //add to empty list
{
head = new Node;
assert(head != NULL);
head->next = NULL;
head->dataValue = value;
retValue = true;
}//end of if - adding to empty list
else //else - multiple nodes
{
Node *current = head;
//value is less then the head
if(value < current->dataValue)
{
cout<<"else if"<<endl;
current = new Node;
assert(current!=NULL);
current->dataValue = value;
current->next = head;
head = current;
retValue = true;
}//end if - value should be head
else
{
bool placed = false;
Node *prev = current;
current = current->next;
if(placed == true)
cout<<"true"<<endl;
while(!placed)
{
//value should be in center
if((value > prev->dataValue)
&&(value < current->dataValue))
{
cout<<"while if"<<endl;
current = new Node;
assert(current!=NULL);
current->next = prev->next;
current->dataValue = value;
prev->next = current;
placed=true;
retValue = true;
}//end if - value in center
//value is at the end
else if(current->next == NULL)
{
cout<<"while else if"<<endl;
prev = current;
current = new Node;
assert(current!=NULL);
current->dataValue = value;
current->next = NULL;
prev->next = current;
placed = true;
retValue = true;
}//end else if - add at end of Set
//value should not be placed
else
{
cout<<"while else"<<endl;
//increment pointers
prev=current;
current = current->next;
}//end of else - not placed;
}//end of while - placing value
}//end of else - multiple nodes, not head
}//end of else - multiple Nodes
}//end of if - value is in the set
return retValue;
}//end of operator += Thanks.
ILC
P.S. I know it isn't the prettiest code, but I despise pointers. Any cout statements were for trying to debug.
Last edited by ILC : November 5th, 2002 at 04:51 PM.
|
| |
November 5th, 2002, 10:50 PM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Eastern Shore
Posts: 701
|
Well, several hours and aspirin later, I have fixed it. No one ask what exactly caused it becuase I myself am not too sure.......
ILC |
| |
November 7th, 2002, 12:57 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,017
|
so did you change anything in the code?
maybe we can see a difference... |
| |
November 7th, 2002, 01:23 AM
|
#4 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Eastern Shore
Posts: 701
|
Totally re-wrote the function. Code: bool Set::operator += (const unsigned int value)
{
bool retValue = false;
//if value is not in the list
if(!(*this%value))
{
if(head == NULL)
{
head = addNewNode(value,NULL);
retValue = true;
}//end if - adding to empty list
else if(value < head->dataValue) //value should become the first node
{
head = addNewNode(value,head);
retValue = true;
}//end else if - value should be first node
else //value in middle or end
{
Node *current;
Node *prev;
current = head;
bool run = true;
//find location for pointer
while(run)
{
if((current->next == NULL)||(current->dataValue > value))
run = false;
else
{
prev = current;
current = current->next;
}//end else
}//end of while
//add if needs to be last node
if((current->next == NULL)&&(value > current->dataValue))
{
current->next = addNewNode(value,NULL);
retValue = true;
}//end if - adding last node
else
{
prev->next = addNewNode(value,current);
retValue=true;
}//end of else
}//end else - value not first
}//end if - value in set
return retValue;
}//end operator +=={ ILC |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |