186: Insertion in a linked list (function)

PHOTO EMBED

Tue May 23 2023 17:33:39 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

void Insert(int pos, int x){
  Node *t, *p;
  if(pos==0){
    t= new Node;
    t->data= x;
    t->next= first;
    first= t;
  }
  else if(pos>0){
    p = first;
    for(i=0; i<pos-1; i++)
      p=p->next;
    if(p){
      t= new Node;
      t->data= x;
      t->next = p->next;
      p->next= t;
    }
  }
}
content_copyCOPY