RSS Forums RSS

Not repeated elements in link list (help)

Please support our C++ advertiser: Programming Forums
Reply
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)

  #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 2:41 pm. Reason: fixed code tags
AddThis Social Bookmark Button
Reply With Quote  
Posts: 665
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: 107
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Not repeated elements in link list (help)

  #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  
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)

  #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.

template <typename ListElement>
void List <ListElement>::Insert()
{
NodePtr Ptr;
Ptr = new Node;
bool repeated = true;

if (Ptr == NULL)
{
cout << "Error: Insuficient Storage " << endl;
exit(1);
}

cout << " Enter The Social Security Number of New Student: ";
cin >> Ptr->Info.SSN;
cout << "Enter The Age of This Student: ";
cin >> Ptr->Info.Age;

while( Ptr->Next != NULL)
{
if(Ptr->Info.Age == Ptr->Next->Info.Age)
repeated = false;
break; // This break is for not to add the element

Ptr = Ptr->Next;


}

Ptr->Next = Head;
Head = Ptr;

if(repeated = false)
cout << "repetition just ocurred" << endl; // This is just 4 me to verified if the function works



}
Reply With Quote  
Posts: 665
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: 107
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Not repeated elements in link list (help)

  #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  
Posts: 314
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 61
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Not repeated elements in link list (help)

  #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  
Posts: 113
Reputation: Laiq Ahmed is on a distinguished road 
Solved Threads: 13
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

  #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.

template <typename ListElement>
void List <ListElement>::Insert()
{
NodePtr Ptr;
Ptr = new Node;
bool repeated = true;

if (Ptr == NULL)
{
cout << "Error: Insuficient Storage " << endl;
exit(1);
}

cout << " Enter The Social Security Number of New Student: ";
cin >> Ptr->Info.SSN;
cout << "Enter The Age of This Student: ";
cin >> Ptr->Info.Age;

while( Ptr->Next != NULL)
{
if(Ptr->Info.Age == Ptr->Next->Info.Age)
repeated = false;
break; // This break is for not to add the element

Ptr = Ptr->Next;


}

Ptr->Next = Head;
Head = Ptr;

if(repeated = false)
cout << "repetition just ocurred" << endl; // This is just 4 me to verified if the function works



}


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  
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)

  #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  
Posts: 113
Reputation: Laiq Ahmed is on a distinguished road 
Solved Threads: 13
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

  #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  
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)

  #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  
Posts: 113
Reputation: Laiq Ahmed is on a distinguished road 
Solved Threads: 13
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

  #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 3:06 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Views: 575 | Replies: 11 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:23 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC