samedi 13 juillet 2019

Linked List insertion At specific node rather than zero;

When am trying to insert a node at some other position rather than first am getting no result, where am i having the error? while inserting the second insert command nothing happens?***************************************************************************

class Node
{
public :
int data;
class Node *next;
}*first;
int count(class Node *c)
{
int l = 0;
while(c)
{
    l++;
    c=c->next;
}
return l;
}
void Insert(int pos,int x)
{
class Node *t,*p;
if(pos==0)
{
     t=new Node;
    t->data=x;
    t->next=first;
    first=t;
}
else
{
    p=first;
    for(int i=0;i<pos&&p;i++)
        p=p->next;
    if(p)
     {
          t=new Node;
          t->data=x;
          t->next=p->next;
          p->next=t;
     }
 }
 }
 void display(class Node *p)
 {
 while(p!=NULL)
{
    cout<<p->data;
    p=p->next;
}
}
int main()
{
first = NULL;
Insert(0,1);
Insert(1,2);    // NOT WORKING
display(first);

return 0;
}

Aucun commentaire:

Enregistrer un commentaire