184: Improve searching in a linked list

PHOTO EMBED

Tue May 23 2023 16:59:50 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

//move to front
//improve searching by moving the searched node to the front so that another time it is searched, it will be found in less time
//function for moving a node to the head (in search operation)

Search(Node *p, int key){
  
  Node *q= NULL;
  
  while(p!= NULL){
    if(key==p->data) {
      q-next= p->next;
      p->next= first;
      first = p;
    }
    q= p;
    p=p->next;
  }
}
content_copyCOPY