
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion
When it is required to count the number of occurrence of a specific element of a linked list with the help of recursion, a method to add elements to the linked list, a method to print the elements of the linked list, and a method to count the occurrence of an element in the linked list is defined. A helper function is defined, since recursion is being used. This helper function calls the previously defined occurrence counting function.
Below is a demonstration for the same −
Example
class Node: def __init__(self, data): self.data = data self.next = None class my_linked_list: def __init__(self): self.head = None self.last_node = None def add_value(self, my_data): if self.last_node is None: self.head = Node(my_data) self.last_node = self.head else: self.last_node.next = Node(my_data) self.last_node = self.last_node.next def print_it(self): curr = self.head while curr: print(curr.data) curr = curr.next def count_val(self, key): return self.count_helper_fun(self.head, key) def count_helper_fun(self, curr, key): if curr is None: return 0 if curr.data == key: return 1 + self.count_helper_fun(curr.next, key) else: return self.count_helper_fun(curr.next, key) my_instance = my_linked_list() my_list = [56, 43, 70, 67, 89, 91, 70, 23, 46, 70] for elem in my_list: my_instance.add_value(elem) print("The linked list contains the below elements:") my_instance.print_it() key_val = int(input('Enter the data item: ')) count_val = my_instance.count_val(key_val) print('{0} occurs {1} time(s) in the list.'.format(key_val, count_val))
Output
The linked list contains the below elements: 56 43 70 67 89 91 70 23 46 70 Enter the data item: 70 70 occurs 3 time(s) in the list.
Explanation
The ‘Node’ class is created.
Another ‘my_linked_list’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’ and last node to ‘None’.
Another method named ‘add_value’ is defined, that is used to add data to the linked list.
Another method named ‘print_it’ is defined, that iterates over the list, and prints the elements.
Another method named ‘count_val’ is defined that is used to call the helper function.
Another helper function, named ‘count_helper_fun’ is defined, that helps determine the frequency of occurrence of a specific element in the linked list.
An object of the ‘my_linked_list’ class is created.
The count_val method is called, to find the frequency of a specific element.
This output is displayed on the console.
- Related Articles
- Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion
- Python Program to Search for an Element in the Linked List without using Recursion
- Python Program to Find the Length of the Linked List using Recursion
- Count occurrences of an element in a list in Python
- Python Program to Find the Length of the Linked List without using Recursion
- Python program to count occurrences of an element in a tuple
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Program to Display all the Nodes in a Linked List using Recursion
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- How to count total number of occurrences of an object in a Python list?
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Golang Program to count the number of nodes in a linked list.
- Golang program to count the number of nodes in a doubly linked list.
