Sunday, July 26, 2009

Ininsert a node at specified location in liked list using c++?

Not sure if this is fully correct, but i'm sure this would help :


insertat(Node *head,int position,int data)


{


Node *temp = head,*temp1;


//move to that location


while(position--)


temp = temp-%26gt;next;


//create a node


temp1 = (Node *)malloc(sizeof(Node));


//insert your data into the newly created node


temp1 -%26gt; num = data;


//connect the new node in its place


temp1-%26gt;next = temp-%26gt;next;


temp-%26gt;next = temp1;


}


P.S: I assume a node has a link to next node and an integer member called "num".

Ininsert a node at specified location in liked list using c++?
your gay
Reply:its easy to insert a node at specified location..





just goto the node before the position where u wana insert a new node.


example new node name is n1 node before the position is o1 and the node after position is f1 and ptr means pointer part of node.





now put


n1.ptr = o1.ptr (so that n1 points to f1)


then put


o1.ptr = o1 ( so that o1 points to n1 )





ok hope this can help u


No comments:

Post a Comment