Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Count occurrence of given node in linked list

Suppose given linked list contain following nodes.

Input : 1 2 3 4 5 6 4 1 1

Find occurrence one.

Output: Occurrence of [1] is 3

Given example:

Count Occurrence

View insertion process.

Try it yourself

Algorithm:

use one integer count variable and assign the value is 0. And use one temp pointer that is point to first node of linked list. help of temp pointer visit all other linked list node. if node value are equal to occurrence value then modifying count variable and increment by one.

Code execution:

Stack Areamaincounter (int) ? root(pointer)occurrencecount (int) =3temp(pointer)value (int) =1 Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 1next (pointer)= NULL

Note that not given all step of execution process here.View How to insert linked list element and so on.

Try it yourself

C program to count occurrence of a given node in linked list.

OutPut:

Spread the post