Not repeated elements in link list (help)
Please support our C++ advertiser: Programming Forums
![]() |
•
•
Posts: 17
Reputation:
Solved Threads: 0
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:
What do you think it is? Thanks
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:
cplusplus Syntax (Toggle Plain Text)
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 }
What do you think it is? Thanks
Last edited by Narue : Dec 4th, 2008 at 2:41 pm. Reason: fixed code tags
•
•
Posts: 17
Reputation:
Solved Threads: 0
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.
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
} Just call this..
c++ Syntax (Toggle Plain Text)
bool sample::exist(NodePtr *p,int age) { bool isexist=false; NodePtr *n_ptr = p; for(;n_ptr!=0;n_ptr=n_ptr->Next) if(n_ptr->Info.Age==age) isexist=true; return isexist; )
•
•
Posts: 113
Reputation:
Solved Threads: 13
•
•
•
•
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
cpp Syntax (Toggle Plain Text)
template <typename ListElement> void List <ListElement>::Insert() { NodePtr Ptr; Ptr = new Node; bool repeated = false; 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 = true; break; } // This break is for not to add the element Ptr = Ptr->Next; } if(repeated = false) { Ptr->Next = Head; Head = Ptr; } else { delete Ptr; cout << "repetition just ocurred" << endl; // This is just 4 me to verified if the function works }
Hope Above idea helps
•
•
Posts: 113
Reputation:
Solved Threads: 13
•
•
•
•
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- Amazing Website Designs - How do they do this? (Site Layout and Usability)
Other Threads in the C++ Forum
- Previous Thread: Can someone please correct my pseudo code assignment? I'm new to this language.
- Next Thread: Urgent solution needed.....
•
•
•
•
Views: 575 | Replies: 11 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode