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
Server Side Programming Articles
Page 422 of 2109
Python program to create a doubly linked list of n nodes and display it in reverse order
A doubly linked list is a data structure where each node contains data and pointers to both the next and previous nodes. To create and reverse a doubly linked list, we need to define a Node class and a list class with methods for adding nodes and reversing the order. Node Class Structure The Node class contains three attributes: data, next pointer, and previous pointer ? class Node: def __init__(self, my_data): self.prev = None self.data = ...
Read MorePython program to create a doubly linked list of n nodes and count the number of nodes
A doubly linked list is a data structure where each node contains three components: data, a pointer to the next node, and a pointer to the previous node. This allows traversal in both directions. To count the nodes, we traverse the list from head to tail and increment a counter. In a doubly linked list, the first node's previous pointer is None, and the last node's next pointer is None. This bidirectional linking makes it more flexible than a singly linked list. Node Class Structure First, we create a Node class to represent individual nodes ? ...
Read MorePython program to convert a given binary tree to doubly linked list
When it is required to convert a given binary tree to a doubly linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and pointers to the left and right nodes. A binary tree is a non-linear data structure which contains one root node, and every node can have at most two children. In a doubly linked list, nodes have pointers to both the next and previous nodes, allowing traversal in both directions. The conversion follows an in-order traversal approach, where we visit left ...
Read MorePython program to sort the elements of the Circular Linked List
A Circular Linked List is a data structure where the last node points back to the first node, forming a circle. To sort elements in a circular linked list, we need to create a Node class and implement sorting algorithms. In a circular linked list, unlike regular linked lists, there's no NULL value at the end. Instead, the tail node connects back to the head, creating a continuous loop. Node Class Structure First, we define a Node class to represent individual elements ? class Node: def __init__(self, data): ...
Read MorePython program to search an element in a Circular Linked List
When it is required to search for an element in a circular linked list, a Node class needs to be created. In this class, there are two attributes: the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have NULL value in the last node. Another class needs to be created that would have an initialization function, and the head of the node would be initialized ...
Read MoreHow to get unique elements in nested tuple in Python
When working with nested tuples, you often need to extract unique elements from all inner tuples. Python provides several approaches to achieve this using sets, loops, and built-in functions. A set is a built-in Python data type that automatically stores only unique elements. It's perfect for removing duplicates and performing operations like union, intersection, and difference. Using Nested Loops with Set This approach manually iterates through each tuple and tracks unique elements ? my_tuples = [(7, 8, 0), (0, 3, 45), (3, 2, 22), (45, 12, 9)] print("The list of tuples is:") print(my_tuples) ...
Read MoreMultiply Adjacent elements in Python
When it is required to multiply adjacent elements, the zip() method, the tuple() method, and generator expressions can be used. The zip() method takes iterables, aggregates them into a tuple, and returns it as the result. Generator expressions provide a simple way of creating iterators that automatically implement __iter__() and __next__() methods. Basic Example Here's how to multiply adjacent elements in a tuple ? my_tuple = (7, 8, 0, 3, 45, 3, 2, 22) print("The tuple is:") print(my_tuple) my_result = tuple(i * j for i, j in zip(my_tuple, my_tuple[1:])) print("The tuple after ...
Read MoreCheck if variable is tuple in Python
When it is required to check if a variable is a tuple, Python provides several methods. A tuple is an immutable data type, meaning values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They ensure read-only access and are important containers in Python programming. Python offers multiple ways to check if a variable is a tuple: using type(), isinstance(), and checking the __class__ attribute. Using type() Method The type() method returns the exact type of the object. We can compare it with the ...
Read MoreAdd dictionary to tuple in Python
When you need to add a dictionary to a tuple in Python, you can convert the tuple to a list, append the dictionary, and convert it back to a tuple. This approach leverages the mutability of lists since tuples are immutable. A list can store heterogeneous values (data of any type like integers, strings, dictionaries, etc.). The append() method adds elements to the end of the list. Method 1: Using list() and append() Convert the tuple to a list, append the dictionary, then convert back to a tuple ? my_tuple = (7, 8, 0, 3, ...
Read MoreChunk Tuples to N in Python
When it is required to chunk tuples into groups of 'N' values, list comprehension provides an efficient solution. This technique divides a tuple into smaller sub-tuples of specified size. List comprehension is a shorthand to iterate through sequences and perform operations on them in a single line. Basic Chunking Example Here's how to chunk a tuple into groups of N elements ? my_tuple = (87, 90, 31, 85, 34, 56, 12, 5) print("The original tuple is:") print(my_tuple) N = 2 print(f"Chunking into groups of {N}") result = [my_tuple[i : i + N] ...
Read More