linked_list/linked_list1.cpp
// Function to delete a node at a given position
Function to delete a node at a given position
Function to delete a node at a given position
int delete_at_pos(int pos)
{
    struct Node * p, *q;
    int del_val = -1;
    if (pos == 1)
    {
        p = first;
        first = first->next;
        del_val = first->data;
        delete p;
    }
    else
    {
        p=first;
        q=NULL;
        for(int i=0; i<pos-1 && p; i++)
        {
            q=p;
            p=p->next;
        }
        if(p) 
        {
            q->next=p->next;
            del_val=p->data;
            delete p;
        }
    }
    return del_val;
}
int delete_at_pos ( int pos ) { struct Node * p , * q ; int del_val = -1 ; if ( pos == 1 ) { p = first ; first = first -> next ; del_val = first -> data ; delete p ; } else { p = first ; q = NULL ; for ( int i = 0 ; i < pos - 1 && p ; i ++ ) { q = p ; p = p -> next ; } if ( p ) { q -> next = p -> next ; del_val = p -> data ; delete p ; } } return del_val ; }
Function to delete a node at a given position
Function to delete a node at a given position
// take two pointers // init the value to be deleted // check if the position is first or not // if true // ppoints to first node // move first pointer point to next // store delete value in del_val // delete p because it's been allocated in // the heap memory // p points to the node whose data to be deleted // q points to the previous node which helps to // delete node p // if p is not null