Default Argument A default argument is a value provided during the function declaration that can be automatically assigned if no argument is provided when the function is called. If a value is passed at the time of the function call, this default value is overridden, and the argument becomes a parametrized Argument. Syntax The syntax below shows how we can define a default argument function: int fun(int x = 0){ // function body } Example of Default Argument The following C++ example shows the working of the default argument: #include using namespace std; //function defined with ... Read More
A deque is a double-ended queue linear data structure where elements can be added or removed from both the front and rear ends. Unlike a standard queue which is FIFO. A dequeue can function in both FIFO and LIFO modes. Application of Dequeue Deques are useful in a situation where you need to add or remove an element from both ends, such as in buffer implementation managing undo/redo functions or implementing certain algorithms. Deque Operations Deque supports the following operations: insert_at_beg(): inserts an item at the front of Dequeue. insert_at_end(): inserts ... Read More
Sorted Doubly Linked List A sorted doubly linked list is a type of doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. Where, insertion operation makes sure that the new node is placed in its correct sorted position. A doubly linked list is a two-way linked list in which a node is connected with two pointers, i.e., next and previous pointers, which are references to the next node and previous node, respectively. Characteristics of Sorted Doubly Linked List The following are the characteristics of the doubly linked ... Read More
In C++, both virtual functions and runtime polymorphism are key features that enable dynamic behavior and code flexibility. Virtual Functions A virtual function is the member function that is declared in the base class using the keyword virtual and is overridden in the derived class. The virtual function enables runtime polymorphism means the function that will be executed is determined at runtime rather than at compile time. Characteristics of Virtual Function Following are the characteristics of the virtual function: Virtual function make sure the correct function is called for an object, regardless of the type ... Read More
A singly linked list is a type of linear data structure where each node contains two items: The data and a link to the next node in the list. Where, first node is called as head node and the last node is called the tail. So, you can traverse the linked list from the head node and continue until the link becomes null. Sorted Singly Linked List A singly linked list is supposed to be sorted when its elements are arranged in specific order (i.e., ascending or descending order), and each node contains data and a pointer (or reference) to ... Read More
Here, we will see how to use Wagner and Fisher algorithm for string matching in C++. Using this algorithm, we can find how many minimum changes are required to match those strings. Wagner Fisher Algorithm Wagner Fisher is a dynamic programming algorithm that is used to find the minimum edit distance or levenshtein distance between two input strings. Levenshtein distance between two strings means the number of changes (ie, insertion, deletion or updation) required to convert one string into another. In simpler words, this algorithm will calculate a minimum number of changes required to convert ... Read More
A Binary Search Tree (BST) is a sorted binary tree in which each node follows two key properties: The right subtree of a node contains only keys greater than the node's key. The left subtree of a node contains only keys less than the node's key. Additionally, each node has at most two children. Tree Rotation Tree rotation is an operation that changes the structure without interfering with the order of the elements on a binary tree. It moves one node up in the tree and one node down. ... Read More
In Python, an IndexError occurs when you try to access a position (index) in a list, tuple, or similar collection that isn't there (does not exist). It means your program is trying to access the elements that are not available in the sequence (object). Using try-except to Catch IndexError You can use a try-except block to catch (handle) an IndexError and stop your program from crashing if you try to access an index that doesn't exist (or invalid). Example In this example, we try to access the 5th index of a list that only has 3 elements, which causes an ... Read More
In Python, an IOError (or OSError in latest versions) occurs when an input/output operation fails. For example, when we are trying to read a file that doesn’t exist, writing to a file that is read-only, or accessing a corrupted device. You can catch IOError using a try-except block to handle file input/output errors in Python. For compatibility with Python 3, we need to use OSError or catch both using a tuple. Using try-except Block You can catch IOError using a try-except block. This helps you handle file-related errors without crashing the program. Example In this example, we try to open ... Read More
In Python, you can check whether a substring exists within another string using Python in operator, or string methods like find(), index(), and __contains__(). A string in Python is a sequence of characters that is enclosed in quotes. You can use either single quotes '...' or double quotes "..." to write a string, like this - "Hello" //double quotes 'Python' //single quote A substring in Python simply means a part of a string. For example, text = "Python" part = "tho" Here, "tho" is a substring of the string "Python". Using the in operator We can check ... Read More