Skip to main content

Delete middle node from linked list in c

C program for Delete middle node from linked list. Here problem description and explanation.

/*
    C program for
    Delete middle node 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;
};
// 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;
    }
    return sll;
}
// Handles the request of adding new node at beginning 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 = sll->head;
    }
    sll->head = 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)
    {
        // Display node value
        printf(" %d →", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" NULL\n");
}
// Delete the middle node of linked list
void deleteMid(struct LinkNode *head)
{
    if (head == NULL)
    {
        // When linked list are no elements
        printf("\nEmpty Linked List\n");
    }
    else if (head->next == NULL && 
             head->next->next == NULL)
    {
        // When linked list are less than  of 3 nodes
        printf("\nLess then 3 node of linked list\n");
    }
    else
    {
        struct LinkNode *temp = head;
        struct LinkNode *midPrevious = NULL;
        // Find the middle and its previous node
        while (temp != NULL && 
               temp->next != NULL && 
               temp->next->next != NULL)
        {
            if (midPrevious == NULL)
            {
                // When first node
                midPrevious = temp;
            }
            else
            {
                // Visit to next node
                midPrevious = midPrevious->next;
            }
            // Visit to next second next node
            temp = temp->next->next;
        }
        // Get the node which is deleting 
        temp = midPrevious->next;
        // Show deleted node
        printf("Delete node : %d\n", temp->data);
        // Change link
        midPrevious->next = temp->next;
        free(temp);
    }
}
int main(int argc, char
    const *argv[])
{
    struct SingleLL *sll = newLinkedList();
    // Linked list
    // 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL
    addNode(sll,1);
    addNode(sll,2);
    addNode(sll,3);
    addNode(sll,4);
    addNode(sll,5);
    addNode(sll,6);
    addNode(sll,7);
    printf(" Linked List \n");
    display(sll->head);
    printf("Before Delete middle node\n");
    display(sll->head);
  
    deleteMid(sll->head);
    printf("After Delete middle node\n");
    display(sll->head);
    return 0;
}

Output

 Linked List
 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL
Before Delete middle node
 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL
Delete node : 4
After Delete middle node
 7 → 6 → 5 → 3 → 2 → 1 → NULL




Comment

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