View Single Post
Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Not repeated elements in link list (help)

 
0
  #1
Dec 4th, 2008
Hi.

I want my Insertion method in my link list class not to insert repeated elements. I have tried several ways but I have seriously ugly bugs in my code:

  1. template <typename ListElement>
  2. void List <ListElement>::Insert()
  3. {
  4. NodePtr Ptr;
  5. Ptr = new Node;
  6. bool repeated = true;
  7.  
  8. if (Ptr == NULL)
  9. {
  10. cout << "Error: Insuficient Storage " << endl;
  11. exit(1);
  12. }
  13.  
  14. cout << " Enter The Social Security Number of New Student: ";
  15. cin >> Ptr->Info.SSN;
  16. cout << "Enter The Age of This Student: ";
  17. cin >> Ptr->Info.Age;
  18.  
  19. while( Ptr->Next != NULL)
  20. {
  21. if(Ptr->Info.Age == Ptr->Next->Info.Age)
  22. repeated = false;
  23. break; // This break is for not to add the element
  24.  
  25. Ptr = Ptr->Next;
  26.  
  27.  
  28. }
  29.  
  30. Ptr->Next = Head;
  31. Head = Ptr;
  32.  
  33. if(repeated = false)
  34. cout << "repetition just ocurred" << endl; // This is just 4 me to verified if the function works
  35.  
  36.  
  37.  
  38. }

What do you think it is? Thanks
Last edited by Narue; Dec 4th, 2008 at 3:41 pm. Reason: fixed code tags
Reply With Quote