Count nodes in a linked list with given value
“Count nodes in a linked list with given value” is a basic programming problem based on linked list data structures for beginners. Here, we are given a linked list and our task is to count nodes in a linked list with given value.
To do so, we need to traverse the complete linked list and increment the counter every time we encounter a node with value ‘x’.
Example:
Suppose, we need to count the occurrence of the nodes with value ‘2’ in the following linked list. Linked List: 1 -> 2 -> 3 -> 2 -> 2 -> 7 -> 2 The following list has 4 occurrences of Nodes with value as ‘2’.
The steps to count nodes in a linked list with given value are as follows:
- Initialize a counter with 0.
- Traverse the complete linked list. While traversing, for every node, check if the node value is ‘x’ or not. If the node value is ‘x’, then increment the counter.
- Print the counter as the number of occurrences of the nodes with value ‘x’.
C++ Program to count nodes in a linked list with given value is as follows:
/* C++ Program to Count nodes in a linked list with given value */
#include<bits/stdc++.h>
using namespace std;
/* Structure of the node of the linked list */
struct node
{
int data;
struct node *next;
};
/* Function to Insert new node at the beginning of the linked list */
void insert_end(struct node **head, int ele)
{
/* Creating a new node */
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;
/* check whether the linked list is empty or not */
/* If the linked list is empty */
if(*head == NULL)
{
*head = new_node;
return;
}
/* if the linked list is not empty, then traverse the complete linked list and
points the pointer of the last node of the linked list to the new node */
struct node *temp = NULL;
temp = (*head);
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}
/* Function to count all Occurrences of particular node in a linked list */
int count(struct node *head,int x)
{
/* Traverse the complete linked list and for every node encountered,
check whether node value is 'x' or not. If Node value is 'x', then increment
counter by 1. */
struct node *temp = head;
int counter = 0;
while(temp!=NULL)
{
if(temp->data == x)
counter++;
temp=temp->next;
}
return counter;
}
/* Function to print the linked list */
void print(struct node *head)
{
struct node *p = head;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
struct node *head;
head = NULL;
/* Insert Some Nodes of the Linked List */
insert_end(&head,1);
insert_end(&head,2);
insert_end(&head,3);
insert_end(&head,2);
insert_end(&head,2);
insert_end(&head,7);
insert_end(&head,2);
cout<<"The elements of the linked list are:\n";
print(head);
int x = 2;
int res = count(head,x);
cout<<"\nThe total number of nodes in a linked list with value as "<<x<<" is "<<res;
}
OUTPUT: The elements of the linked list are: 1 2 3 2 2 7 2 The total number of nodes in a linked list with value as 2 is 4
Related Posts:
- Bubble Sort on Linked List.
- Detect a Loop in a Linked List.
- Find the Length of the Loop present in the Linked List.
- Detect and Remove Loop from a Linked List.
- Segregate Even and Odd Nodes of the Linked List.
- Delete Complete Linked List.
- Delete Nth Node of the Linked List.
- Delete without head pointer of the Linked List.
- Delete All Occurrences of particular node of the Linked List.
- Delete Alternate Nodes of the Linked List.
- Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
- Remove Duplicate Nodes from an Unsorted Linked List.
- Remove Duplicate Nodes from a Sorted Linked List.
- Find Union and Intersection of Two Linked List.
- Merge Two Sorted Linked List.
- Insert a New Node at the Middle of the Linked List.
- Insert a New Node at the Sorted Linked List.
- Reverse a Linked List.
- Reverse a Linked List Using Stack.
- Printing Linked List in Reverse Order without actually reversing the Linked List.