943,542 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3292
  • C++ RSS
Nov 22nd, 2008
0

Genreic Double Linked List Using Templates

Expand Post »
Generic Double Linked List
C++ Syntax (Toggle Plain Text)
  1. /* These are generic Functions for a Generic List
  2.  * Note:-Only Functions not including a Menu and an Interface
  3.  * You could use these Functions for your own Benefit*/
  4. #include <iostream>
  5. using namespace std;
  6. template <class X> struct node{ //Template Structure
  7. X data;
  8. node* next;
  9. node* prev;
  10. };
  11.  
  12. template <class Y> void insert_first(node<Y>** l,Y elem)
  13. {
  14. node<Y>* Q=new node<Y>();
  15. Q->data=elem;
  16. Q->prev=NULL;
  17. Q->next=*l;
  18. *l=Q;
  19. }
  20. template <class Y> void print_l(node<Y>* l)//Print the List
  21. {
  22. while(l!=NULL)
  23. {
  24. cout<<l->data<<" ";
  25. l=l->next;
  26. }
  27. }
  28. template <class type> int count(node<type>* l)//Count the List
  29. {
  30. int counter=0;
  31. while(l!=NULL)
  32. {
  33. counter++;
  34. l=l->next;
  35. }
  36. return counter;
  37. }
  38.  
  39. template <class type> void append(node<type>** l,type elem)
  40. {
  41. if(*l==NULL)
  42. insert_first(&*l,elem);
  43. else
  44. {
  45. node<type>*temp=*l;
  46. node<type> *Q=new node<type>;
  47. Q->data=elem;
  48. while(temp->next!=NULL)
  49. {
  50. temp=temp->next;
  51. }
  52. temp->next=Q;
  53. Q->prev=temp;
  54. }
  55. }
  56. template <class type>
  57. void insert_at_pos(node<type> **l,int pos,type elem)
  58. {
  59. if(pos<=0||pos>count(*l))
  60. {
  61. cout<<"Invalid Position"<<endl;
  62. exit(1);
  63. }
  64. node<type>* Q=new node<type>;
  65. node<type>* temp=*l;
  66. int i=2;
  67. if(pos==1)
  68. {
  69. insert_first(&*l,elem);
  70. }
  71. else
  72. {
  73. while(temp!=NULL)
  74. {
  75. if(pos==i)
  76. {
  77. Q->data=elem;
  78. Q->next=temp->next;
  79. Q->prev=temp;
  80. temp->next=Q;
  81. }
  82. i++;
  83. temp=temp->next;
  84. }
  85. }
  86. }
  87.  
  88. int main(int argc, char** argv)
  89. {
  90. node<int>* q=NULL;//Create Empty Pointer that Will point to the Head of the List
  91. insert_first(&q,2);
  92. insert_at_pos(&q,2,2);
  93. return 0;
  94. }

If there is any critique about this code plz tell me about it to be careful in the next time i write any code .
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

You wrote a bunch of C-style unsafe functions instead of a good C++ class List with comfortable and safe interface. Look at the class std::list as a model. Now these templates look like a steam-engine with microcontrolled whistle...
Last edited by ArkM; Nov 22nd, 2008 at 11:00 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

Quote ...
You wrote a bunch of C-style unsafe functions instead of a good C++ class List with comfortable and safe interface. Look at the class std::list as a model.
Mr ArkM I didn't intend using classes but i was intending to try to use the (Generic Functions ) Definition not a Generic Class or something like that and in addition to that i wrote this code as an extension to the course that i am taking at college which is (Data Structures and Algorithm Analysis in C (not C++).

BTW thanx for your advice.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

I see. I think it's not the best problem area to try generic functions. You are trying to declare a family of related functions implemented some generic data structure. But it's exactly a class notion. Now your efforts demonstrated a disparity of a goal and tools.

When we need generic functions? We want a generic unit of functionality - for example, generic sort array operation...

Apropos, don't use by value parameters in program cases as in following code:
C++ Syntax (Toggle Plain Text)
  1. ... append(node<type>** l,type elem) {
  2. ... Q->data=elem;
No need in argument copying, use references:
C++ Syntax (Toggle Plain Text)
  1. ... append(node<type>** l,const type& elem) {
Yet another remark(s): print_l function requires operator << for template argument class. Why? There are lots of data classes without such operator but now you can't use them with your functions package. Avoid such names as print_l: simply look at print_l and print_1 (different ) names...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

Mr ArkM i'm very thankful for your interaction with me .
Could u plz explain your purpose cuz i tried it and it went to infinite loop while displaying random numbers (or maybe the addresses) and why we pass the reference of the element while u know in the structure up there
contains a variable of type X not a pointer of type X to receive the address ?.
Plz Explain your point Mr.ArkM .
Last edited by Ahmed_I; Nov 22nd, 2008 at 12:14 pm.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

Try to insert val at pos 4 without insert val at pos 2..Is it work?..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

Click to Expand / Collapse  Quote originally posted by Ahmed_I ...
Mr ArkM i'm very thankful for your interaction with me .
Could u plz explain your purpose cuz i tried it and it went to infinite loop while displaying random numbers (or maybe the addresses) and why we pass the reference of the element while u know in the structure up there
contains a variable of type X not a pointer of type X to receive the address ?.
Plz Explain your point Mr.ArkM .
Please, present your modified code (with test main, of course).
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

This is the modified function
C++ Syntax (Toggle Plain Text)
  1. template <class type> void append(node<type>** l,const type& elem)
  2. {
  3. if(*l==NULL)
  4. insert_first(&*l,elem);
  5. else
  6. {
  7. node<type>*temp=*l;
  8. node<type> *Q=new node<type>;
  9. Q->data=elem;
  10. while(temp->next!=NULL)
  11. {
  12. temp=temp->next;
  13. }
  14. temp->next=Q;
  15. Q->prev=temp;
  16. }
  17. }


and this is the test in main
C++ Syntax (Toggle Plain Text)
  1. int main(int argc, char** argv)
  2. {
  3. node<int>* q=NULL;//Create Empty Pointer that Will point to the Head of the List
  4. insert_first(&q,2);
  5. append(&q,5); //Here is the test
  6. print_l(q);
  7. system("PAUSE");
  8. return 0;
  9. }
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Genreic Double Linked List Using Templates

Well, this code prints "Invalid position" then exit (it's a very bad solution: after exit() automatic class variables destructors are not called). You don't use modified append function. Moreover, passed by reference data argument can't raise new problems at all...

I have no time to debug your code from the beginning. Please, make a proper test plan, prepare test cases then ask help with the serious problems brief and clear description.

It's so simply to destruct your list via improper calls of these low-level functions. May this package debugging troubles help you to understand that all these low level operations must be incapsulated in the well-designed class ...
Last edited by ArkM; Nov 22nd, 2008 at 2:50 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

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: Square Root of -1
Next Thread in C++ Forum Timeline: CMSFlexGrid : MFC :





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


Follow us on Twitter


© 2011 DaniWeb® LLC