- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find Number of Occurrences of All Elements in a Linked List
When it is required to find the number of occurences of all the elements of a linked list, a method to add elements to the linked list, a method to print the elements and a method to find the occurrence of all elements in the linked list are defined.
Below is a demonstration for the same −
Example
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head = None self.last_node = None def add_vals(self, data): if self.last_node is None: self.head = Node(data) self.last_node = self.head else: self.last_node.next = Node(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_elem(self, key): curr = self.head count_val = 0 while curr: if curr.data == key: count_val = count_val + 1 curr = curr.next return count_val my_instance = LinkedList_structure() my_list = [56, 78, 98, 12, 34, 55, 0] for elem in my_list: my_instance.add_vals(elem) print('The linked list is : ') my_instance.print_it() key_val = int(input('Enter the data item ')) count_val = my_instance.count_elem(key_val) print('{0} occurs {1} time(s) in the list.'.format(key_val, count_val))
Output
The linked list is : 56 78 98 12 34 55 0 Enter the data item 0 0 occurs 1 time(s) in the list.
Explanation
The ‘Node’ class is created.
Another ‘LinkedList_structure’ 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’.
A method named ‘add_vals’ is defined, that helps add a value to the stack.
Another method named ‘print_it’ is defined, that helps display the values of the linked list on the console.
Another method named ‘count_elem’ is defined, that helps find the occurrence of every character in the linked list.
An instance of the ‘LinkedList_structure’ is created.
A list of elements is defined.
The list is iterated over, and these elements are added to the linked list.
The elements are displayed on the console.
The ‘count_elem’ method is called on this linked list.
The output is displayed on the console.
- Related Articles
- Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion
- Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion
- Python program – All occurrences of Substring from the list of strings
- Python Program to Reverse only First N Elements of a Linked List
- Python program to sort the elements of the Circular Linked List
- Program to find a list of product of all elements except the current index in Python
- Python Program to Create a Linked List & Display the Elements in the List
- Program to find linked list intersection from two linked list in Python
- Program to find the kth missing number from a list of elements in Python
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- Python program to remove duplicate elements from a Doubly Linked List
- Python program to remove duplicate elements from a Circular Linked List
- Program to find the middle node of a singly linked list in Python
- Python program to find sum of elements in list
- Program to find folded list from a given linked list in Python
