Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

How is inserting fibonacci series in singly linked list done?

  Asked By: Niti    Date: Oct 22    Category: C Programming    Views: 1986
  

Code of creating a singly linked list and inserting a fibonacci series and deleting singly linked list using C language??

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Jeeya Kay     Answered On: Apr 13

//Program for fibonacci series using singly linked list

#include<stdio.h>
#include<stdlib.h>

typedef struct ls
{
int data;
struct ls *next;
}LIST;

LIST *fibo(LIST *head,int n)
{
LIST *newnode=(LIST *)malloc(sizeof(LIST)), *temp=head;
newnode->data=n;
newnode->next=NULL;


while(temp->next!=NULL)
temp=temp->next;

temp->next=newnode;

return head;
}

void display(LIST *head)
{
while(head!=NULL)
{
printf("%d",head->data);
if(head->next != NULL)
printf(" --> ");

head=head->next;
}

printf("\n");
}

int main()
{
LIST *head=(LIST *)malloc(sizeof(LIST));
int a,b,c,i,terms;

head->data=0;
head->next=NULL;

printf("The no. of terms to be built for fibonacci series : ");
scanf("%d",&terms);

a = 0; b = 1; c = 0;

for(i=1; i<=terms; i++)
{
a = b;
b = c;
c = a + b;
head=fibo(head,c);
}

display(head);

return 0;
}

 




Tagged: