Home » C programs

Count the number of occurrences of an element in a linked list without using recursion

In this article, we are going to see how to find no of occurrences of an element in a linked list without using recursion?
Submitted by Piyas Mukherjee, on January 11, 2019

Solution:

Input:

A singly linked list whose address of the first node is stored in a pointer, say head and key is the data of which we have to count the number of occurrences.

Output:

The number of times key occurred in the list, (Count)

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
        Count = 0
        while(temp != NULL)
            begin
                if(temp->data = key)
                    count=count+1
                endif
                temp=temp->link
        End while
    End

C program to Count the number of occurrences of an element in a linked list without using recursion

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

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

int main()
{
	node *head=NULL,*temp,*temp1;
	int choice,count=0,key;

	//building the 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 m ore data on the list enter 1 : ");
		scanf("%d",&choice);
	}while(choice==1);
	
	//finding occurence of key
	printf("\nEnter the data to find it's occurrence : ");
	scanf("%d",&key);
	
	temp=head;
	while(temp!=NULL)
	{
		if(temp->data==key)
		{
			count=count+1;
		}
		temp=temp->next;
	}
	printf("\n %d occurred %d times in the list",key,count);

	return 0;
}

Output

Enter the element in the list : 1

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

Enter the element in the list : 2

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

Enter the element in the list : 3

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

Enter the element in the list : 4

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

Enter the element in the list : 1

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

Enter the data to find it's occurrence : 1

1 occurred 2 times in the list




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



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.