Home » C programs

Find the length of a linked list without using recursion

In this article, we are going to see how to find a linked list length without using recursion?
Submitted by Piyas Mukherjee, on January 24, 2019

Solution:

Algorithm to find length

Input:

A singly linked list whose address of the first node is stored in a pointer, say head.

Output:

The no of nodes in the list, c

Data structure used:

Singly linked list where each node contains a data element say data, and the address of the immediate next node say next, with head holding the address of the first node.

Pseudo code:

Begin
    temp=head
    c=0 //counter to store count of nodes
    While(temp!=NULL) //upto end of linked list
	    begin
	    c=c+1
	    temp=temp->next
    End while
End

C code:

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

//node sructure
typedef struct list 
{
	int data;
	struct list *next;
}node;

int length(node *temp);

int main()
{
	node *head=NULL,*temp,*temp1;
	int choice,count;
	//building linked list
	do
	{
		temp=(node *)malloc(sizeof(node));
		if(temp!=NULL)
		{
			printf("\nEnter the element in the list : ");
			scanf("%d",&temp->data);
			temp->next=NULL;
			if(head==NULL)
			{	
				head=temp;
			}
			else
			{
				temp1=head;
				while(temp1->next!=NULL)
				{
					temp1=temp1->next;
				}
				temp1->next=temp;
			}
		}
		else
		{
			printf("\nMemory not avilable...node allocation is not possible");
		}
		printf("\nIf you wish to add more data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	//Now counting the length of the list using a user defined function
	count=length(head);
	printf("\nThe length of the list is : %d",count);
	
	return 0;
}

//function to find length iteratively
int length(node *temp) 
{
	int c=0;
	//travarsing to the end of the list and count the number of nodes
	while(temp!=NULL)	
	{
		c=c+1;
		temp=temp->next;
	}
	return c;
}

Output

Enter the element in the list : 1

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 2

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 3

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 4

If you wish to add more data on the list enter 1 : 1

Enter the element in the list : 5

If you wish to add more data on the list enter 1 : 0

The length of the list is : 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.