192: deleting a node from a linked list

PHOTO EMBED

Tue May 23 2023 18:55:29 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

int Delete(struct Node *p, int index){
  Node *q;
  int x=-1 , i;
  
  if(index < 1 || index >count(p))
    return -1;
  
  if(index==1){
    x=first->data;
    q=first;
    first=first->next;
    delete q;
    return x;
  } 
  else{
    for(i=0; i<index-1 && p; i++){
      q=p;
      p=p->next;
    }
    q->next=p->next;
    x=p->data;
    delete p;
    return x;
    }
}
content_copyCOPY