DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Not repeated elements in link list (help) (http://www.daniweb.com/forums/thread160733.html)

lmastex Dec 4th, 2008 1:39 pm
Not repeated elements in link list (help)
 
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:

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

Freaky_Chris Dec 4th, 2008 2:05 pm
Re: Not repeated elements in link list (help)
 
Use code tags please [code=cplusplus][/code]
Also what exactly is the problem?

Chris

lmastex Dec 4th, 2008 2:18 pm
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.

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



}

Freaky_Chris Dec 4th, 2008 2:21 pm
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

cikara21 Dec 4th, 2008 2:26 pm
Re: Not repeated elements in link list (help)
 
Just call this..
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;
)

Laiq Ahmed Dec 4th, 2008 3:00 pm
Re: Not repeated elements in link list (help)
 
Quote:

Originally Posted by lmastex (Post 750311)
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
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

lmastex Dec 4th, 2008 3:40 pm
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)

Laiq Ahmed Dec 4th, 2008 3:42 pm
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.

lmastex Dec 4th, 2008 3:51 pm
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?

Laiq Ahmed Dec 4th, 2008 4:05 pm
Re: Not repeated elements in link list (help)
 
Quote:

Originally Posted by lmastex (Post 750372)
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.


All times are GMT -4. The time now is 9:48 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC