Pointers and recursive functions in C -
i have created list numbers. example:
-3 4 -2 -1 4 5
the products of 2 continuous nodes -14 -8 2 -4 21
. want check if product of 2 nodes bigger product of following ones. used recursive function , in example treaty unsuccessful because 2>-4
.
i want print message showing first node causes problem. in case -1 4th node. how return pointer showing wrong node? see questionmarks :)
struct node* findnode(struct node *junc) { //success if(junc->link->link==null){ printf("success"); return null; } //failure if (((junc->content)*(junc->link->content))>=((junc->link->content)*(junc->link->link->content))) { printf("fail."); printf("\nfail because of numbers:%d %d.",junc->link->content,junc->link->link->content); return junc; } return(findnode(junc->link)); }
i want check if product of 2 nodes bigger product of following ones.
i think had code wrong way around, i.e. had >= should <= according description.
the code below return pointer wrong node (i.e. first of 2 numbers product less or equal product of next 2 numbers)
#include <stdio.h> struct node { int content; struct node* next; }; struct node* findnode(struct node* node) { if(node->next->next == 0){ printf("success"); return 0; } if ((node->content * node->next->content) <= (node->next->content * node->next->next->content)) { printf("fail because of numbers: %d , %d", node->next->content, node->next->next->content); return node; } return(findnode(node->next)); } int main() { struct node node1 = {5, 0}; struct node node2 = {4, &node1}; struct node node3 = {-1, &node2}; struct node node4 = {-2, &node3}; struct node node5 = {4, &node4}; struct node node6 = {-3, &node5}; struct node* res = findnode(&node6); if (res != 0) { // res pointer wrong node } return 0; }