Tuesday, August 17, 2010

Adding a node at the begining of a linked list (vc++).?

I have this method of adding a node at the starting point of a linked list but its not not working, instead its adding at the end.


header files


:#include %26lt;iostream.h%26gt;


#include %26lt;string.h%26gt;





method:


void put_node_at_start(int scode, char sname[20], char sdesig[20], int syexp, int sage){


node *temp;//,*temp2;


char nameholder[20]; //holds the value of passed name value


char desigholder[20]; //holds the value of passed desig value


// Reserve space for new node and fill it with data


temp = new node;


strcpy(nameholder,sname);


strcpy(desigholder,sdesig);


strcpy((*temp).name,nameholder);


strcpy((*temp).desig,desigholder);


temp-%26gt;code= scode;


temp-%26gt;age= sage;


temp-%26gt;yexp = syexp;





temp-%26gt;nxt=start_ptr-%26gt;nxt;


start_ptr=temp;





delete temp;


}

Adding a node at the begining of a linked list (vc++).?
it should be


temp-%26gt;next = start_ptr





not start_ptr-%26gt;next, assuming start_ptr points to the first node.


then start_ptr has to be initialised to temp.

hooks

No comments:

Post a Comment