Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Created October 17, 2017 11:55
Show Gist options
  • Save mirsahib/774a65b03d17b196a6af98304a8d203f to your computer and use it in GitHub Desktop.
Save mirsahib/774a65b03d17b196a6af98304a8d203f to your computer and use it in GitHub Desktop.
void insertBeforeParticularItem(int searchVal,int val){
int counter = 0;
if(head == NULL){
cout<<"List is empty"<<endl;
}else{
curr = head;
node* prev = NULL;
while(curr!=NULL){
if(curr->data==searchVal){
node *newNode = new node;
newNode->data = val;
if(prev==NULL){
newNode->next = curr;
head = newNode;
}else{
newNode->next = curr;
prev->next = newNode;
}
counter++;
break;
}
prev = curr;
curr=curr->next;
}
if(counter==0)cout<<"search occurrence not found"<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment