c code to replace linked list with product of data

  • clinked list
  • write a c code to replace linked list with product of data excluding itself

    Write a program to display a linked list of n integers (n>1), where every index of the linked list should contain the product of all elements in the linked list except the element at the given index. Solve this problem by taking a single loop without an additional linked list. Input linked list : 3 4 5 1 2 Output linked list :40 30 24 120 60

    Code

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data; struct node *next; }; void display(struct node *r) { printf("elements of list are : "); while (r) { printf("%d ---> ", r->data); r = r->next; } printf("NULL\n"); } int init_count(struct node *r) { int count = 0; while (r) { r = r->next; count++; } return count; } int tmul(struct node *r) { int i = 1; while (r) { i = r->data * i; r = r->next; } return i; } struct node *create(struct node *head) { struct node *t, *p; int n; if (head == NULL) { t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", &t->data); t->next = NULL; head = t; } if (head != NULL) { do { t = (struct node *)malloc(sizeof(struct node)); printf("enter the node: \n"); scanf("%d", &t->data); t->next = NULL; p = head; while (p->next != NULL) { p = p->next; } p->next = t; printf("do you want to add more node press 1 for yes, 0 for no. "); scanf("%d", &n); } while (n); } return head; } struct node *replaceprod(struct node *head) { int mul = tmul(head); struct node *t = head; while (t) { t->data = mul / t->data; t = t->next; } return head; } int main() { struct node *head = NULL; head = create(head); display(head); head = replaceprod(head); display(head); return 0; }