Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct listNode {
- int value;
- struct listNode * next;
- };
- struct listNode* listGenerator(){
- /*
- ammount: How many number a user will give
- node: Current node in itteration
- head: The first node of the list
- previous_node: Stores the previous node of the list in the iteration
- */
- int ammount;
- struct listNode* node, *head, *previous_node;
- printf("How many numbers will you give?");
- scanf("%d", &ammount);
- if(ammount <= 0 ){ //If the user requests <=0 numbers
- return NULL; //we return a NULL pointer
- }
- for (int i = 0; i < ammount; i++){
- node = (struct listNode *) malloc(sizeof(struct listNode)); //Allocating memory for a new node
- printf("Give number for [%d]", i); //Asking the user for a value
- scanf("%d", &(node->value));
- (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
- previous_node = node; //This node is stored in the previous node for the next iteration
- }
- node->next = NULL; //The last node of the list points now to NULL
- return head; //We return the head
- }
- void printList(struct listNode * head)
- {
- if (head != NULL){ //If we haven't reached beyond the list
- printf("%d ", head->value); //We print this node's value
- printList(head->next); //And call the function again for the next node
- } else{
- printf("Nothing to print"); //Otherwise we terminate
- exit(1);
- }
- }
- //The requested function
- int checkSumOfOdds(struct listNode * node){
- int sum = 0; //This is the sum of the next odd numbers of the list
- if(node == NULL){ //If we have reached beyond the list we return 0
- return 0;
- }
- sum = checkSumOfOdds(node->next); //If we are still inside the list we fetch the sum of the next odd nodes
- if( node->value * node->value > sum){ //If the square of the number is bigger than the sum
- printf("%d[%d] YES ",node->value, sum); //We print accordingly
- }else{
- printf("%d[%d] NO ",node->value, sum); //The opposite
- }
- if (node->value % 2 == 1){ //If this node is an odd number
- return (sum + node->value); //We return it's value plus the next odd nodes sum
- }else{ return (sum); //Else we return just the next odd nodes sum
- }
- }
- int main()
- {
- struct list *head = listGenerator();
- //printList(head);
- checkSumOfOdds(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement