Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Find Number of Occurrences of All Elements in a Linked List
When working with linked lists, it's often useful to find the number of occurrences of elements. This implementation demonstrates how to create a linked list and count occurrences of specific elements using a custom count_elem method.
Below is a demonstration for the same ?
Linked List Implementation
First, we create the basic structure with Node and LinkedList classes:
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
Example: Counting Element Occurrences
Here's how to use the linked list to count occurrences of a specific element ?
# Create linked list instance
my_instance = LinkedList_structure()
elements = [56, 78, 98, 12, 34, 55, 0, 12, 56]
# Add elements to the linked list
for elem in elements:
my_instance.add_vals(elem)
print('The linked list is:')
my_instance.print_it()
# Count occurrences of specific elements
search_element = 12
count_val = my_instance.count_elem(search_element)
print(f'{search_element} occurs {count_val} time(s) in the list.')
# Count another element
search_element = 56
count_val = my_instance.count_elem(search_element)
print(f'{search_element} occurs {count_val} time(s) in the list.')
The linked list is: 56 78 98 12 34 55 0 12 56 12 occurs 2 time(s) in the list. 56 occurs 2 time(s) in the list.
Counting All Elements
To find occurrences of all unique elements, we can extend the functionality ?
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 count_all_elements(self):
frequency = {}
curr = self.head
while curr:
if curr.data in frequency:
frequency[curr.data] += 1
else:
frequency[curr.data] = 1
curr = curr.next
return frequency
# Example usage
class Node:
def __init__(self, data):
self.data = data
self.next = None
my_list = LinkedList_structure()
elements = [1, 2, 3, 1, 2, 4, 1]
for elem in elements:
my_list.add_vals(elem)
frequencies = my_list.count_all_elements()
print("Element frequencies:")
for element, count in frequencies.items():
print(f"{element}: {count} time(s)")
Element frequencies: 1: 3 time(s) 2: 2 time(s) 3: 1 time(s) 4: 1 time(s)
Key Components
The Node class represents individual elements with data and next pointer
The LinkedList_structure class manages the linked list operations
The add_vals method appends elements to the end of the list
The count_elem method counts occurrences of a specific element
The count_all_elements method returns frequency of all unique elements
Conclusion
This implementation provides an efficient way to count element occurrences in a linked list. The count_elem method handles single element counting, while count_all_elements gives a complete frequency analysis of all elements.
