Genreic Double Linked List Using Templates

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

Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Genreic Double Linked List Using Templates

 
0
  #1
Nov 22nd, 2008
Generic Double Linked List
  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 .
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Genreic Double Linked List Using Templates

 
0
  #2
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Genreic Double Linked List Using Templates

 
0
  #3
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Genreic Double Linked List Using Templates

 
0
  #4
Nov 22nd, 2008
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:
  1. ... append(node<type>** l,type elem) {
  2. ... Q->data=elem;
No need in argument copying, use references:
  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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Genreic Double Linked List Using Templates

 
0
  #5
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Genreic Double Linked List Using Templates

 
0
  #6
Nov 22nd, 2008
Try to insert val at pos 4 without insert val at pos 2..Is it work?..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Genreic Double Linked List Using Templates

 
0
  #7
Nov 22nd, 2008
Originally Posted by Ahmed_I View Post
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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Genreic Double Linked List Using Templates

 
0
  #8
Nov 22nd, 2008
This is the modified function
  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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Genreic Double Linked List Using Templates

 
0
  #9
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC