-
C++ (
http://www.daniweb.com/forums/forum8.html)
| lmastex | Dec 4th, 2008 7:44 pm | |
| Re: Not repeated elements in link list (help) 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. |
| Laiq Ahmed | Dec 5th, 2008 5:48 am | |
| Re: Not repeated elements in link list (help) Quote: Originally Posted by lmastex (Post 750506) 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 :). |
| All times are GMT -4. The time now is 9:06 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC