| | |
Genreic Double Linked List Using Templates
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
Generic Double Linked List
If there is any critique about this code plz tell me about it to be careful in the next time i write any code .
C++ Syntax (Toggle Plain Text)
/* These are generic Functions for a Generic List * Note:-Only Functions not including a Menu and an Interface * You could use these Functions for your own Benefit*/ #include <iostream> using namespace std; template <class X> struct node{ //Template Structure X data; node* next; node* prev; }; template <class Y> void insert_first(node<Y>** l,Y elem) { node<Y>* Q=new node<Y>(); Q->data=elem; Q->prev=NULL; Q->next=*l; *l=Q; } template <class Y> void print_l(node<Y>* l)//Print the List { while(l!=NULL) { cout<<l->data<<" "; l=l->next; } } template <class type> int count(node<type>* l)//Count the List { int counter=0; while(l!=NULL) { counter++; l=l->next; } return counter; } template <class type> void append(node<type>** l,type elem) { if(*l==NULL) insert_first(&*l,elem); else { node<type>*temp=*l; node<type> *Q=new node<type>; Q->data=elem; while(temp->next!=NULL) { temp=temp->next; } temp->next=Q; Q->prev=temp; } } template <class type> void insert_at_pos(node<type> **l,int pos,type elem) { if(pos<=0||pos>count(*l)) { cout<<"Invalid Position"<<endl; exit(1); } node<type>* Q=new node<type>; node<type>* temp=*l; int i=2; if(pos==1) { insert_first(&*l,elem); } else { while(temp!=NULL) { if(pos==i) { Q->data=elem; Q->next=temp->next; Q->prev=temp; temp->next=Q; } i++; temp=temp->next; } } } int main(int argc, char** argv) { node<int>* q=NULL;//Create Empty Pointer that Will point to the Head of the List insert_first(&q,2); insert_at_pos(&q,2,2); return 0; }
If there is any critique about this code plz tell me about it to be careful in the next time i write any code .
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
•
•
•
•
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.
BTW thanx for your advice.
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:
No need in argument copying, use references:
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...
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)
... append(node<type>** l,type elem) { ... Q->data=elem;
C++ Syntax (Toggle Plain Text)
... append(node<type>** l,const type& elem) {
) names... •
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
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 .
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.
•
•
•
•
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 .
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
This is the modified function
and this is the test in main
C++ Syntax (Toggle Plain Text)
template <class type> void append(node<type>** l,const type& elem) { if(*l==NULL) insert_first(&*l,elem); else { node<type>*temp=*l; node<type> *Q=new node<type>; Q->data=elem; while(temp->next!=NULL) { temp=temp->next; } temp->next=Q; Q->prev=temp; } }
and this is the test in main
C++ Syntax (Toggle Plain Text)
int main(int argc, char** argv) { node<int>* q=NULL;//Create Empty Pointer that Will point to the Head of the List insert_first(&q,2); append(&q,5); //Here is the test print_l(q); system("PAUSE"); return 0; }
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
...
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Square Root of -1
- Next Thread: CMSFlexGrid : MFC :
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






