Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //c program to find the length of a linked list using recirsion
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- typedef struct node
- {
- int data;
- struct node *link;
- }NODE;
- NODE *getnode()
- {
- NODE *New;
- New=(NODE *)malloc(sizeof(NODE));
- New->link=NULL;
- return New;
- }
- NODE *insert(NODE *Start, int x)
- {
- NODE *New=getnode(),*Last;
- if(New==NULL)
- {
- printf("\nNo space");
- return Start;
- }
- New->data = x;
- if(Start==NULL)
- {
- Start=New;
- return Start;
- }
- Last = Start;
- while(Last->link != NULL)
- {
- Last = Last->link;
- }
- Last->link = New;
- return Start;
- }
- void display(NODE *Start)
- {
- if(Start==NULL)return;
- printf("%d ",Start->data);
- display(Start=Start->link);
- //printf("%d ",Start->Data);
- }
- NODE *CreateList()
- {
- char Resp;
- int x;
- NODE *Start=NULL;
- while(1)
- {
- printf("\nAny more data to insert(Y/N)?");
- Resp=getche();
- if(Resp=='N' || Resp=='n')
- {
- return Start ;
- }
- printf("\nEnter Data:");
- scanf("%d",&x);
- Start=insert(Start,x);
- display(Start);
- }
- }
- int l=0;
- //recursive function to find length
- int length(NODE *temp)
- {
- if(temp==NULL)
- return l;
- else
- {
- l=l+1;
- length(temp->link);
- }
- }
- main()
- {
- NODE *start;
- int len;
- start=CreateList();
- //printf("The number of nodes are: %d\n", length(start));
- len=length(start);
- printf("The list has a total of %d no of nodes",l);
- }
Add Comment
Please, Sign In to add comment