Not repeated elements in link list (help)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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 Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Not repeated elements in link list (help)

 
0
  #2
Dec 4th, 2008
Use code tags please [code=cplusplus][/code]
Also what exactly is the problem?

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
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

Re: Not repeated elements in link list (help)

 
0
  #3
Dec 4th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Not repeated elements in link list (help)

 
0
  #4
Dec 4th, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Not repeated elements in link list (help)

 
0
  #5
Dec 4th, 2008
Just call this..
  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. )
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

 
0
  #6
Dec 4th, 2008
Originally Posted by lmastex View Post
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.

  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
  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
Reply With Quote Quick reply to this message  
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

Re: Not repeated elements in link list (help)

 
0
  #7
Dec 4th, 2008
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)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

 
0
  #8
Dec 4th, 2008
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.
Reply With Quote Quick reply to this message  
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

Re: Not repeated elements in link list (help)

 
0
  #9
Dec 4th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

 
0
  #10
Dec 4th, 2008
Originally Posted by lmastex View Post
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.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC