| | |
Not repeated elements in link list (help)
![]() |
•
•
Join Date: Jan 2008
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:
C++ 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 3:41 pm. Reason: fixed code tags
•
•
Join Date: Jan 2008
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.
C++ 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 }
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; )
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
•
•
•
•
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)
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 }
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
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
•
•
•
•
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?
.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.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- 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.....
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets





