Skip to main content

Delete all nodes whose node digit sum is odd from circular linked list in c

C program for Delete all nodes whose node digit sum is odd from circular linked list. Here problem description and other solutions.

// Include header file
#include <stdio.h>
#include <stdlib.h>

// C Program
// Remove all the Odd digit sum nodes from a circular linked list
typedef struct LinkNode
{
    // Define useful field of LinkNode
    int data;
    struct LinkNode * next;
}LinkNode;

LinkNode * getLinkNode(int data)
{
    // Create dynamic memory of LinkNode
    LinkNode * ref = (LinkNode * ) malloc(sizeof(LinkNode));
    if (ref == NULL)
    {
        // Failed to create memory 
        return NULL;
    }
    ref->data = data;
    ref->next = NULL;
    return ref;
}
typedef struct CircularLinkedList
{
    // Define useful field of CircularLinkedList
    struct LinkNode * head;
    struct LinkNode * tail;
}CircularLinkedList;

CircularLinkedList * getCircularLinkedList()
{
    // Create dynamic memory of CircularLinkedList
    CircularLinkedList * ref = 
      (CircularLinkedList * ) malloc(sizeof(CircularLinkedList));
    if (ref == NULL)
    {
        // Failed to create memory 
        return NULL;
    }
    // Set initial value
    ref->head = NULL;
    ref->tail = NULL;
    return ref;
}
//Add node at the end position efficiently
void insert(CircularLinkedList * ref, int value)
{
    // Create a new node
    LinkNode * node = getLinkNode(value);
    if ((ref->head == NULL))
    {
        // When first node
        ref->head = node;
    }
    else
    {
        // Connect new node at the last
        ref->tail->next = node;
    }
    // Connect last node to first node
    node->next = ref->head;
    // Set new last node
    ref->tail = node;
}
// Display node element of circular linked list
void display(CircularLinkedList * ref)
{
    if ((ref->head == NULL))
    {
        printf("\n Empty Linked List");
    }
    else
    {
        // First node of linked list
        printf(" %d ", ref->head->data);
        LinkNode * temp = ref->head->next;
        // Display linked list node
        while (temp != NULL && temp != ref->head)
        {
            // Display node value
            printf(" %d ", temp->data);
            // Visit to next node
            temp = temp->next;
        }
    }
}
// Sum of digits
int digitSum( int num)
{
    int n = num;
    int sum = 0;
    if ((n < 0))
    {
        // When node value is negative
        n = -n;
    }
    while (n > 0)
    {
        sum += (n % 10);
        n = n / 10;
    }
    return sum;
}
void removeOddDigitSumNode(CircularLinkedList * ref)
{
    if ((ref->head == NULL))
    {
        return;
    }
    LinkNode * temp = ref->head;
    LinkNode * auxiliary = ref->head;
    LinkNode * point = NULL;
    while (temp != NULL)
    {
        if (((digitSum(temp->data) % 2) != 0))
        {
            auxiliary = temp;
            temp = temp->next;
            if ((auxiliary == ref->head))
            {
                // Enter to remove first node
                if ((auxiliary == ref->tail))
                {
                    // Enter to removing single node
                    ref->tail = NULL;
                    ref->head = NULL;
                    temp = NULL;
                    point = NULL;
                }
                else
                {
                    ref->head = temp;
                    ref->tail->next = temp;
                }
            }
            else if ((auxiliary == ref->tail))
            {
                // When Remove last node
                if ((point != NULL))
                {
                    point->next = ref->head;
                }
                ref->tail = point;
            }
            else
            {
                // When removing intermediate node
                if ((point != NULL))
                {
                    point->next = temp;
                }
            }
            free(auxiliary);
            auxiliary = NULL;
        }
        else
        {
            point = temp;
            // Visit to next node
            temp = temp->next;
            if ((temp == ref->head))
            {
                // Stop the process
                temp = NULL;
            }
        }
    }
}
int main()
{
    CircularLinkedList * cll = getCircularLinkedList();
    // Insert node
    insert(cll, 131);
    insert(cll, 12);
    insert(cll, 33);
    insert(cll, 10);
    insert(cll, 143);
    insert(cll, 91);
    insert(cll, 27);
    insert(cll, 125);
    insert(cll, 89);
    printf("\n Before remove odd digit sum nodes\n");
    display(cll);
    /*
        Node   Digit Sum    Odd
        ----- -----------  --------
        131   [1+3+1] 5     Yes
        12    [1+2]   3     Yes
        33    [3+3]   6     No
        10    [10]    1     Yes
        143   [1+4+3] 8     No
        91    [9+1]   10    No
        27    [2+7]   9     Yes        
        125   [1+2+5] 8     No
        89    [8+9]   17    Yes
        --------------------------
        Result 
        ------
        33 → 143 → 91 → 125 ┐ 
         └-----------------⤶
            
    */
    removeOddDigitSumNode(cll);
    printf("\n After remove odd digit sum nodes\n");
    display(cll);
}

Output

 Before remove odd digit sum nodes
 131  12  33  10  143  91  27  125  89
 After remove odd digit sum nodes
 33  143  91  125




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