Answer to Question #265262 in C for Rosie

Question #265262

Write a C program to remove and print every third number from a list of numbers until the list becomes empty. 

1
Expert's answer
2021-11-13T02:01:29-0500
#include <stdio.h>
#include <stdlib.h>
 
struct Node {
    int info;
    struct Node* next;
};


void push(struct Node** head_ref, int new_info)
{
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));
    new_node->info = new_info;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}


void deleteNode(struct Node** head_ref, int X)
{
   
    struct Node *temp = *head_ref, *prev;
 
    
    if (temp != NULL && temp->info == X) {
        *head_ref = temp->next; 
        free(temp); 
        return;
    }
 
    while (temp != NULL && temp->info != X) {
        prev = temp;
        temp = temp->next;
    }
 
    
    if (temp == NULL)
        return;
 
   
    prev->next = temp->next;
 
    free(temp); 
}


void printList(struct Node* node)
{
    while (node != NULL) {
        printf(" %d ", node->info);
        node = node->next;
    }
}
 
int main()
{
    struct Node* H = NULL;
    push(&H, 50);
    push(&H, 40);
    push(&H, 30);
    push(&H, 20);
    push(&H, 10);
 
    puts("Created Linked List: ");
    printList(H);
    deleteNode(&H, 10);
    printf("\n10 deleted\nRemaining linked list\n");
    printList(H);
    deleteNode(&H, 40);
    printf("\n40 deleted\nRemaining linked list\n");
    printList(H);
    deleteNode(&H, 20);
    printf("\n20 deleted\nRemaining linked list\n");
    printList(H);
    deleteNode(&H, 50);
    printf("\n50 deleted\nRemaining linked list\n");
    printList(H);
    deleteNode(&H, 30);
    printf("\n10 deleted\nLinked list empty\n");
    printList(H);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS