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

Use code tags please [code=cplusplus] [/code]
Also what exactly is the problem?

Chris

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



}

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

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

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

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)

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.

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?

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.

I replaced it to: while( Ptr != 0) But I still have the same problem.
I think I have to replace if(Ptr->Info.Age == Ptr->Next) but I don't find the right way.

I replaced it to: while( Ptr != 0) But I still have the same problem.
I think I have to replace if(Ptr->Info.Age == Ptr->Next) but I don't find the right way.

Here is a sample code, thanks for your efforts :).

template<class T>
class Node { 
	T _element;
	Node* _next;
public: 
	Node(const T& element, Node* next = 0) : _element(element), _next(next) {}

	T Element() const { return _element; }

	Node*& Next() {	return _next; }

};


template<class T>
class LinkList { 
	Node<T>* _head;

public:
	LinkList() : _head(0) {}
	bool Insert(const T& element);
	void PrintList() const;
};

template<class T>
bool LinkList<T>::Insert(const T& element) 
{
	
	if (!_head) {
		_head = new Node<T>(element);
		return true;
	}

	if (element == _head->Element ()) {
		return false;
	}

	// Temporary pointer to head.
	Node<T>* _headIter = _head;
	bool bRepeated = false;
	while (_headIter->Next()) {
		if (element == _headIter->Element()) {
			bRepeated = true;
		}
		_headIter = _headIter->Next();
	}
	
	if (bRepeated) {
		cout<<" Repeated: element is : "<< element <<endl;
	}
	else {
		_headIter->Next() = new Node<T>(element);
	}

	return true;
}

template<class T> 
void LinkList<T>::PrintList() const
{
	Node<T>* _headIter = _head;
	while (_headIter) {
		cout<<"The element is : " << _headIter->Element()<<endl;
		_headIter = _headIter->Next();
	}
}

try to acheive this task its interesting one
there is a link list with repeated elements remove all the repeated elements :).