Insert Node into the end of list

struct node{
    int data;
    struct node *next;
};

struct node *getLastNode(struct node *head);

void insertNode(struct node **head, int data)
{

    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->next = NULL;
    newNode->data = data;
    if(*head == NULL)
    {
        *head = newNode;
    }
    else
    {
        getLastNode(*head)->next = newNode;
    }
}

struct node *getLastNode(struct node *head)
{
    struct node *lastNode = head;
    if(head == NULL)
        return NULL;
    else{
        while(lastNode->next != NULL)
            lastNode = lastNode->next;
    }
    return lastNode;
}

Remove Node with Key

struct node{
    int data;
    struct node *next;
};

void removeNodeWithData(struct node **head, int removeData)
{
    struct node *current = *head;
    struct node *prev=*head;
    while(current->data == removeData) //head
    {
        *head = (*head)->next;
        free(current);
        current = *head;
    }

    prev = *head;
    current = (*head)->next;
    while(current != NULL)
    {
        if(current->data == removeData)
        {
            prev->next = current->next;
            free(current);
        }
        else{
            prev = current;

        }
        current = prev->next;
    }

}
Related Posts Plugin for WordPress, Blogger...