Not repeated elements in link list (help)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 17
Reputation: lmastex is an unknown quantity at this point 
Solved Threads: 0
lmastex lmastex is offline Offline
Newbie Poster

Re: Not repeated elements in link list (help)

 
0
  #11
Dec 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Not repeated elements in link list (help)

 
0
  #12
Dec 5th, 2008
Originally Posted by lmastex View Post
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 .
  1. template<class T>
  2. class Node {
  3. T _element;
  4. Node* _next;
  5. public:
  6. Node(const T& element, Node* next = 0) : _element(element), _next(next) {}
  7.  
  8. T Element() const { return _element; }
  9.  
  10. Node*& Next() { return _next; }
  11.  
  12. };
  13.  
  14.  
  15. template<class T>
  16. class LinkList {
  17. Node<T>* _head;
  18.  
  19. public:
  20. LinkList() : _head(0) {}
  21. bool Insert(const T& element);
  22. void PrintList() const;
  23. };
  24.  
  25. template<class T>
  26. bool LinkList<T>::Insert(const T& element)
  27. {
  28.  
  29. if (!_head) {
  30. _head = new Node<T>(element);
  31. return true;
  32. }
  33.  
  34. if (element == _head->Element ()) {
  35. return false;
  36. }
  37.  
  38. // Temporary pointer to head.
  39. Node<T>* _headIter = _head;
  40. bool bRepeated = false;
  41. while (_headIter->Next()) {
  42. if (element == _headIter->Element()) {
  43. bRepeated = true;
  44. }
  45. _headIter = _headIter->Next();
  46. }
  47.  
  48. if (bRepeated) {
  49. cout<<" Repeated: element is : "<< element <<endl;
  50. }
  51. else {
  52. _headIter->Next() = new Node<T>(element);
  53. }
  54.  
  55. return true;
  56. }
  57.  
  58. template<class T>
  59. void LinkList<T>::PrintList() const
  60. {
  61. Node<T>* _headIter = _head;
  62. while (_headIter) {
  63. cout<<"The element is : " << _headIter->Element()<<endl;
  64. _headIter = _headIter->Next();
  65. }
  66. }
try to acheive this task its interesting one
there is a link list with repeated elements remove all the repeated elements .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC