Advertisement
gur111

Print Fibo Recur and linked list looped

May 26th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct node{
  6.         int n;
  7.         struct node* _next;
  8. } Node;
  9.  
  10.  
  11. void fibo_o(Node *prev, Node *first, int n){
  12.         Node *new_node = malloc(sizeof(Node));
  13.  
  14.         if(n==0){
  15.                 prev->_next->_next=first;
  16.                 return;
  17.         }
  18.         new_node->n = prev->n+prev->_next->n;
  19.         prev->_next->_next=new_node;
  20.  
  21.         fibo_o(prev->_next, first, n-1);
  22.         printf("%d,", new_node->n);
  23.  
  24. }
  25.  
  26. void fibo(float n){
  27.         Node *second = malloc(sizeof(Node));
  28.         Node *first = malloc(sizeof(Node));
  29.         if(((float) n) != ((int) n)  || n<0){
  30.                 printf("Invalid number\n");
  31.                 return;
  32.         }
  33.         if(n==0){
  34.                 first->_next=first;
  35.                 printf("1\n");
  36.                 return;
  37.         }
  38.         first->n=second->n=1;
  39.         first->_next = second;
  40.         fibo_o(first, first, (int)(n-1));
  41.         printf("1,1\n");
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement