Delete a node from linked list specific position in c

C program for Delete a node from linked list specific position. Here problem description and explanation.

/*
    C program for
    Delete node at given index of linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Linked List LinkNode
struct LinkNode
{
    int data;
    struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
    struct LinkNode *head;
    struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
    // Create memory of head and tail Nodes
    struct SingleLL *sll = (struct SingleLL *)
    malloc(sizeof(struct SingleLL));
    if (sll == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        sll->head = NULL;
        sll->tail = NULL;
    }
    return sll;
}
// Handles the request of adding new node at end of linked list
void addNode(struct SingleLL *sll, int data)
{
    // Create dynamic node
    struct LinkNode *node = (struct LinkNode *)
    malloc(sizeof(struct LinkNode));
    if (node == NULL)
    {
        printf("Memory overflow to Create LinkNode\n");
        return;
    }
    else
    {
        // Set initial node value
        node->data = data;
        node->next = NULL;
    }
    if (sll->head == NULL)
    {
        sll->head = node;
    }
    else
    {
        sll->tail->next = node;
    }
    sll->tail = node;
}
// Display linked list element
void display(struct LinkNode *node)
{
    if (node == NULL)
    {
        return;
    }
    struct LinkNode *temp = node;
    // iterating linked list elements
    while (temp != NULL)
    {
        printf(" %d →", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" NULL\n");
}

// Delete node at given position
void deleteAtIndex(struct SingleLL *sll, int index)
{

    struct LinkNode *temp = sll->head;

    if (sll->head == NULL)
    {
        printf("\nEmpty linked list");
        return;
    }
    else if(index < 0)
    {
        printf("\nInvalid positions");
        return;
    }
    else if(index == 1)
    {
        // Means deleting a first node

        if(sll->tail == sll->head)
        {
            // Only one node
            sll->tail = NULL;
        }
        // make second node as head
        sll->head = sll->head->next;


        free(temp); 

    }
    else
    {

        int count = index - 1;

        // iterating linked list elements and find deleted node
        while (temp != NULL && count > 1)
        {
            // Visit to next node
            temp = temp->next;
            count --;
        }

        if(count == 1 && temp->next != NULL)
        {
            // Get deleted node
            struct LinkNode *n = temp->next;

            if(n == sll->tail)
            {
                // When delete last node
                sll->tail = temp;
            }
            // Unlink the deleting node
            temp->next = n->next;

            // Free Node
            free(n);
        }
        else
        {
            printf(" Index %d are not present\n",index);
            return;
        }
    }
  
}
int main(int argc, char const *argv[])
{
    // Create new linked list
    struct SingleLL *sll = newLinkedList();

    // Test with your index
    // Note index start with 1
    int index = 5; 

    // Linked list
    // 10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
    addNode(sll, 10);
    addNode(sll, 20);
    addNode(sll, 30);
    addNode(sll, 40);
    addNode(sll, 50);
    addNode(sll, 60);
    addNode(sll, 70);
    addNode(sll, 80);

    // Display linked list
    printf(" Before Delete Linked List \n");
    display(sll->head);

    // Delete node at 5-th position
    deleteAtIndex(sll,index);

    // Display linked list
    printf(" After Delete Linked List \n");
    display(sll->head);

    return 0;
}

Output

 Before Delete Linked List
 10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
 After Delete Linked List
 10 → 20 → 30 → 40 → 60 → 70 → 80 → NULL


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved