home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2986
Discussions: 188,378, Posts: 2,243,448, Members: 232,606
Old November 5th, 2002, 04:01 PM   Digg it!   #1 (permalink)
ILC
Senior Member
 
ILC's Avatar
 
Join Date: Oct 2001
Location: Eastern Shore
Posts: 701
Strange problem

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.
ILC is offline   Reply With Quote
Old November 5th, 2002, 10:50 PM     #2 (permalink)
ILC
Senior Member
 
ILC's Avatar
 
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

ILC is offline   Reply With Quote
Old 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...

vass0922 is online now   Reply With Quote
Old November 7th, 2002, 01:23 AM     #4 (permalink)
ILC
Senior Member
 
ILC's Avatar
 
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
ILC is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may 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
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2882)
The United States Debt (20)
Looks like Burris will get his Sena.. (7)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
dual monitors wont boot (0)
Folderchat Weekday thread (439)
New Build ( Finally ) (0)
MSN Hotmail Down??? (7)
Help with an Ati Radeon HD 4850.. (23)
Laptop waking up itself (0)
CPU wont boot (3)
Best digital camera for under 2.. (13)
Building first computer, will t.. (2)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:09 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28