
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Write a function that counts the number of times a given int occurs in a Linked List in C++
In this problem, we are given a linked list. Our task is to create a function that will be able to count the number of times a given number occurs in the linked list.
Let’s take an example to understand the problem,
Input
Linked list = 10-> 50 -> 10 -> 20 -> 100 -> 10, int = 10
Output
3
Explaination − the number 10 occurs 3 times in the linked list.
The solution to this problem is simple, just traverse the linked list and increment a counter the current node value is equal to the given number.
The looping over the nodes of the linked list can be done by using iteration as well as recursion and we are illustrating both methods to solve the problem
Program to illustrate the solution using iteration,
Example
#include <iostream> using namespace std; class Node { public: int data; Node* next; }; void push(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int countInt(Node* head, int search_for) { Node* current = head; int intCount = 0; while (current != NULL) { if (current->data == search_for) intCount++; current = current->next; } return intCount; } int main() { Node* head = NULL; push(&head, 10); push(&head, 40); push(&head, 10); push(&head, 50); push(&head, 20); push(&head, 90); push(&head, 10); cout<<"The count of 10 in the linked list is "<<countInt(head, 10); return 0; }
Output
The count of 10 in the linked list is 3
Program to illustrate the solution using recurssion,
Example
#include <iostream> using namespace std; int intCount = 0; class Node { public: int data; Node* next; }; void push(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int countInt(struct Node* head, int key){ if (head == NULL) return intCount; if (head->data == key) intCount++; return countInt(head->next, key); } int main() { Node* head = NULL; push(&head, 10); push(&head, 40); push(&head, 10); push(&head, 50); push(&head, 20); push(&head, 90); push(&head, 10); cout<<"The count of 10 in the linked list is "<<countInt(head, 10); return 0; }
Output
The count of 10 in the linked list is 3
- Related Articles
- Write a function to delete a Linked List in C++ Programming
- Python Program to Search the Number of Times a Particular Number Occurs in a List
- Write a function to get Nth node in a Linked List in C++
- Write a program in C++ to insert a Node at the beginning of the given Singly linked list
- Reverse a Linked List in groups of a Given Size using C++
- How to Segregate a given Linked List in C++
- Delete a Linked List node at a given position in C++
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- C++ Pairwise Swap Elements of a Given Linked List
- Maximum and Minimum element of a linked list which is divisible by a given number k in C++
- How can I write a MySQL stored function that calculates the factorial of a given number?
- Delete all the nodes from a doubly linked list that are smaller than a given value in C++
- Create linked list from a given array in C++ Program
- Delete a Doubly Linked List node at a given position in C++
- Move first element to end of a given Linked List in C++

Advertisements