943,867 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 985
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 4th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lmastex is offline Offline
17 posts
since Jan 2008
Dec 5th, 2008
0

Re: Not repeated elements in link list (help)

Click to Expand / Collapse  Quote originally posted by lmastex ...
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 .
cpp Syntax (Toggle Plain Text)
  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 .
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error C2664: 'setStartupDirectory' : cannot convert parameter
Next Thread in C++ Forum Timeline: Urgent solution needed.....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC