
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to find the length of linked list
Linked lists use dynamic memory allocation i.e. they grow and shrink accordingly. They are defined as a collection of nodes. Here, nodes have two parts, which are data and link. The representation of data, link and linked lists is given below −
Types of Linked Lists
Linked lists have four types, which are as follows −
- Single / Singly linked lists
- Double / Doubly linked lists
- Circular single linked list
- Circular double linked list
The logic we used to find the length of linked list by using recursion method is −
int length(node *temp){ if(temp==NULL) return l; else{ l=l+1; length(temp->next); } }
Program
Following is the C program to find the length of linked list −
#include <stdio.h> #include <stdlib.h> typedef struct linklist{ int data; struct linklist *next; }node; int l=0; int main(){ node *head=NULL,*temp,*temp1; int len,choice,count=0,key; do{ temp=(node *)malloc(sizeof(node)); if(temp!=NULL){ printf("
enter the elements in a 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("
Memory is full"); } printf("
press 1 to enter data into list: "); scanf("%d",&choice); }while(choice==1); len=length(head); printf("The list has %d no of nodes",l); return 0; } //recursive function to find length int length(node *temp){ if(temp==NULL) return l; else{ l=l+1; length(temp->next); } }
Output
When the above program is executed, it produces the following result −
Run 1: enter the elements in a list: 3 press 1 to enter data into list: 1 enter the elements in a list: 56 press 1 to enter data into list: 1 enter the elements in a list: 56 press 1 to enter data into list: 0 The list has 3 no of nodes Run 2: enter the elements in a list: 12 press 1 to enter data into list: 1 enter the elements in a list: 45 press 1 to enter data into list: 0 The list has 2 no of nodes
- Related Articles
- Python Program to Find the Length of the Linked List using Recursion
- Python Program to Find the Length of the Linked List without using Recursion
- Find length of loop in linked list in C++
- C# program to find node in Linked List
- Program to find size of Doubly Linked List in C++
- Find Length of a Linked List (Iterative and Recursive) in C++
- JavaScript Program for Finding Length of a Linked List
- JavaScript Program for Finding the Length of Loop in Linked List
- Program to find linked list intersection from two linked list in Python
- C++ Program to Implement Singly Linked List
- C++ Program to Implement Doubly Linked List
- C++ program to find Second Smallest Element in a Linked List
- Length of a Linked List in Python
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Circular Singly Linked List

Advertisements