Set Class - Insert Function

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 11
Reputation: boydale1 is an unknown quantity at this point 
Solved Threads: 0
boydale1 boydale1 is offline Offline
Newbie Poster

Set Class - Insert Function

 
0
  #1
Apr 13th, 2009
I am working on a set class and I have successfully written an insert function, but I need the function to insert in an ordered fashion.

  1. bool Set::insert( const EType & A )
  2. {
  3. bool Flag = false;
  4. unsigned Position = 0;
  5. Node * Prev = Head;
  6. Node * Curr = Head->Succ;
  7. Node * Temp;
  8.  
  9. Temp = new Node;
  10. if (Temp != NULL )
  11. {
  12. Temp->Item = A;
  13. Temp->Succ = Curr;
  14. Prev->Succ = Temp;
  15. Num++;
  16. Flag = true;
  17. }
  18. return Flag;
  19. }

  1. Set::Set()
  2. {
  3. Num = 0;
  4. Head = new (nothrow) Node;
  5. Head->Succ = NULL;
  6. }

  1. class Set
  2. {
  3. private:
  4.  
  5. struct Node
  6. {
  7. EType Item; // User data item
  8. Node * Succ; // Link to the node's successor
  9. };
  10.  
  11. unsigned Num; // Number of user data items in the set
  12. Node * Head; // Link to the head of the chain

Tell me if anything else is needed
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Set Class - Insert Function

 
0
  #2
Apr 13th, 2009
Scan the set until you find a value higher or lower (depends on how you sort) then the current value, insert it there?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: boydale1 is an unknown quantity at this point 
Solved Threads: 0
boydale1 boydale1 is offline Offline
Newbie Poster

Re: Set Class - Insert Function

 
0
  #3
Apr 13th, 2009
I tried doing that several separate times, but i cannot avoid a segmentation fault.

  1. bool Set::insert( const EType & A )
  2. {
  3. bool Flag = false;
  4. unsigned Position = 0;
  5. Node * Prev = Head;
  6. Node * Curr = Head->Succ;
  7.  
  8. while ( (Curr->Item < A) and (Curr->Succ != NULL) )
  9. {
  10. Prev = Curr;
  11. Curr = Curr->Succ;
  12. cout << Curr->Item;
  13. }
  14.  
  15. if ( (Curr->Item != A) or (Curr == NULL) )
  16. {
  17. Node * Temp;
  18. Temp = new Node;
  19.  
  20. Temp->Item = A;
  21. Temp->Succ = Curr;
  22. Prev->Succ = Temp;
  23. Num++;
  24. Flag = true;
  25. }
  26. return Flag;
  27. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Set Class - Insert Function

 
0
  #4
Apr 13th, 2009
  1. while ( (Curr->Item < A) and (Curr->Succ != NULL) )
  2. {
  3. Prev = Curr;
  4. Curr = Curr->Succ;
  5. cout << Curr->Item;
  6. }

That code sets Curr to the last item in the set that's smaller than A.
And then you compare the last item (curr->item) to A (to guarantee uniqueness) or NULL for the last item. And there's your problem I think, hehe.

What if Curr is NULL and you try to dereference Curr->item? Segfault. I hope that's the error.

So, correct code would be something like..

  1. if(Curr == NULL){
  2. //create new item, append it to the set
  3. } else if (Curr->Item != A) {
  4. //create new item, insert it between the two items.
  5. }
Last edited by Clockowl; Apr 13th, 2009 at 2:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 11
Reputation: boydale1 is an unknown quantity at this point 
Solved Threads: 0
boydale1 boydale1 is offline Offline
Newbie Poster

Re: Set Class - Insert Function

 
0
  #5
Apr 13th, 2009
I don't really understand the last post. I do understand the second part, but I'm confused about the while loop still.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Set Class - Insert Function

 
0
  #6
Apr 13th, 2009
While the current node's item is smaller than A and the current item isn't NULL (and thus we are at the end of the list), go to the next node.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 612 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC