943,654 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 985
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 4th, 2008
0

Not repeated elements in link list (help)

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Use code tags please [code=cplusplus][/code]
Also what exactly is the problem?

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Sorry I forgot ( I remember I used them though) Anyways.
The problem is that when I make an insertion of an element, I don't want to insert a repeated element, so if I inserted (Age = 20) and then If I insert (Age = 20) my list should have only one person who Age is 20, i don't want two ages of 20 in my list. Insertion without repeating elements.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Here is one method of doing it, get the Age from the user, loop through your linked list. If you find it don't add it to the list if you don't then when add it to the list

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Just call this..
c++ Syntax (Toggle Plain Text)
  1. bool sample::exist(NodePtr *p,int age)
  2. {
  3. bool isexist=false;
  4. NodePtr *n_ptr = p;
  5. for(;n_ptr!=0;n_ptr=n_ptr->Next)
  6. if(n_ptr->Info.Age==age)
  7. isexist=true;
  8. return isexist;
  9. )
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Click to Expand / Collapse  Quote originally posted by lmastex ...
Sorry I forgot ( I remember I used them though) Anyways.
The problem is that when I make an insertion of an element, I don't want to insert a repeated element, so if I inserted (Age = 20) and then If I insert (Age = 20) my list should have only one person who Age is 20, i don't want two ages of 20 in my list. Insertion without repeating elements.

C++ Syntax (Toggle Plain Text)
  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. }
I've changed your code
cpp Syntax (Toggle Plain Text)
  1. template <typename ListElement>
  2. void List <ListElement>::Insert()
  3. {
  4. NodePtr Ptr;
  5. Ptr = new Node;
  6. bool repeated = false;
  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 = true;
  23. break;
  24. }
  25. // This break is for not to add the element
  26. Ptr = Ptr->Next;
  27. }
  28.  
  29. if(repeated = false) {
  30. Ptr->Next = Head;
  31. Head = Ptr;
  32. }
  33. else {
  34. delete Ptr;
  35. cout << "repetition just ocurred" << endl; // This is just 4 me to verified if the function works
  36. }


Hope Above idea helps
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

If there is a break in line 23 to stop the element to be added, then why is line 34 is a deletion?

(Im having problems with the code "cpp Syntax" changed)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

because You allocated the element on Heap in the beginning and you should release its memory if its a repeated element otherwise you'd have a memory leak.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Oh ok I see. I don't know why but when I add an element (using this code) and press Enter, a windows error occur and the terminal closes. Any ideas?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 4th, 2008
0

Re: Not repeated elements in link list (help)

Click to Expand / Collapse  Quote originally posted by lmastex ...
Oh ok I see. I don't know why but when I add an element (using this code) and press Enter, a windows error occur and the terminal closes. Any ideas?
Yes! there are other mistakes in your code which I didn't touch .

while( Ptr->Next != NULL) should be replaced.

other misakes are there as well.
Last edited by Laiq Ahmed; Dec 4th, 2008 at 4:06 pm.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error C2664: 'setStartupDirectory' : cannot convert parameter
Next Thread in C++ Forum Timeline: Urgent solution needed.....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC