gur111 icon

Print Fibo Recur and linked list looped

gur111 | PRO | 05/26/19 08:19:43 PM UTC | 0 ⭐ | 565 👁️ | Never ⏰ | []
C |

970 B

|

None

|

0 👍

/

0 👎

include <stdio.h>
#include <stdlib.h>
 
 
typedef struct node{
        int n;
        struct node* _next;
} Node;
 
 
void fibo_o(Node *prev, Node *first, int n){
        Node *new_node = malloc(sizeof(Node));
 
        if(n==0){
                prev->_next->_next=first;
                return;
        }
        new_node->n = prev->n+prev->_next->n;
        prev->_next->_next=new_node;
 
        fibo_o(prev->_next, first, n-1);
        printf("%d,", new_node->n);
 
}
 
void fibo(float n){
        Node *second = malloc(sizeof(Node));
        Node *first = malloc(sizeof(Node));
        if(((float) n) != ((int) n)  || n<0){
                printf("Invalid number\n");
                return;
        }
        if(n==0){
                first->_next=first;
                printf("1\n");
                return;
        }
        first->n=second->n=1;
        first->_next = second;
        fibo_o(first, first, (int)(n-1));
        printf("1,1\n");
 
}

Comments