Advertisement
jimangel2001

Untitled

Mar 8th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct listNode {
  5.     int value;
  6.     struct listNode * next;
  7. };
  8.  
  9.  
  10. struct listNode* listGenerator(){
  11.     /*
  12.     ammount:        How many number a user will give
  13.     node:           Current node in itteration
  14.     head:           The first node of the list
  15.     previous_node:  Stores the previous node of the list in the iteration
  16.     */
  17.     int ammount;
  18.     struct listNode* node, *head, *previous_node;
  19.  
  20.     printf("How many numbers will you give?");
  21.     scanf("%d", &ammount);
  22.  
  23.     if(ammount <= 0 ){                                                  //If the user requests <=0 numbers
  24.         return NULL;                                                    //we return a NULL pointer
  25.     }
  26.  
  27.     for (int i = 0; i < ammount; i++){
  28.         node = (struct listNode *) malloc(sizeof(struct listNode));     //Allocating memory for a new node
  29.         printf("Give number for [%d]", i);                              //Asking the user for a value
  30.         scanf("%d", &(node->value));
  31.         (i == 0) ? (head = node) : (previous_node->next=node);          //If this is the first node, head becomes this node, otherwise, the previous node will point to this node
  32.         previous_node = node;                                           //This node is stored in the previous node for the next iteration
  33.     }
  34.     node->next = NULL;                                                  //The last node of the list points now to NULL
  35.     return head;                                                        //We return the head
  36. }
  37.  
  38.  
  39. void printList(struct listNode * head)
  40. {
  41.     if (head != NULL){                              //If we haven't reached beyond the list
  42.         printf("%d ", head->value);                 //We print this node's value
  43.         printList(head->next);                      //And call the function again for the next node
  44.     } else{
  45.         printf("Nothing to print");                 //Otherwise we terminate
  46.         exit(1);
  47.     }
  48. }
  49.  
  50. //The requested function
  51. int checkSumOfOdds(struct listNode * node){
  52.     int sum = 0;                                    //This is the sum of the next odd numbers of the list
  53.  
  54.     if(node == NULL){                               //If we have reached beyond the list we return 0
  55.         return 0;
  56.     }
  57.  
  58.     sum = checkSumOfOdds(node->next);               //If we are still inside the list we fetch the sum of the next odd nodes
  59.  
  60.     if( node->value * node->value > sum){           //If the square of the number is bigger than the sum
  61.         printf("%d[%d] YES ",node->value, sum);    //We print accordingly
  62.     }else{
  63.         printf("%d[%d] NO ",node->value, sum);     //The opposite
  64.     }
  65.  
  66.     if (node->value % 2 == 1){                      //If this node is an odd number
  67.         return (sum + node->value);                 //We return it's value plus the next odd nodes sum
  68.     }else{ return (sum);                            //Else we return just the next odd nodes sum
  69.     }
  70.  
  71.  
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77.     struct list *head = listGenerator();
  78.     //printList(head);
  79.     checkSumOfOdds(head);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement