C++ supports both implicit and explicit type casting of variables. Error c2664 is displayed while developing C++ when an incompatible conversion is encountered. This can occur if a class is created and an implicit constructor conversion is attempted while an explicit keyword is in the code statement. Type Casting in C++ Converting an expression into another is referred to as “type casting” in C++. The language supports both implicit and explicit type casting. When implicitly converting one variable into another, no operator is required prior to the variables. This is Read More
Binary Tree – Inserting a Node
If the binary search tree is initially empty, then the element is inserted as a root node. Otherwise the element is inserted as terminal node. If the element is less than the element in the root node, then the element is inserted in the left sub tree else to the right sub tree. void insertelement(BST *tree, int element) { BST *ptr,*nodeptr,*parentptr; ptr = (BST*)malloc(sizeof(BST)); ptr->info = element; ptr->left = ptr->right = NULL; if(tree == NULL) *tree = ptr; else { parentptr = NULL; nodeptr = *tree; while(nodeptr != NULL) { Read More
Dijkstra Algorithm
Dijkstra algorithm gets its name from the Dutch computer scientist Edsger Dijkstra who invented this algorithm in 1959. It is used to find the shortest path between two non-negative nodes in a graph. Dijkstra algorithm is widely used for the purpose of routing. For any giving node in a graph the algorithm finds the path which is the shortest from that node to all the corresponding nodes in the graph. As stated earlier also that this algorithm is used to find the shortest path between two distinct nodes on the Read More
Deleting an Element from a Doubly Linked List
To delete an element from the list, first the pointers are set properly and then the memory occupied by the node to be deleted is deallocated (freed). Deletion in the list can take place at the following positions. At the beginning of the list At the end of the list After a given element Before a given element Deleting from the Beginning of the List An element from the beginning of the list can be deleted by performing the following steps: Assign the value of head (address of the first Read More
Quick Sort
Quick sort is a divide-and-conquer sorting algorithm. To understand quick-sort, let us look at a high-level description of the algorithm. Divide: If the sequence S has 2 or more elements, select an element x from S to be your pivot. Any arbitrary element, like the last, will do. Remove all the elements of S and divide them into 3 sequences: L, which holds S’s elements less than x E, which holds S’s elements equal to x G, which holds S’s elements greater than x Recurse: Recursively sort L and G Read More
Postfix
The sum of X and Y is written as X+Y where + is the operator while X and Y are the operands. We have always learnt to write the sum of two numbers as X + Y; this notation is know as infix. Here, we’ll be talking about the postfix notation of representing arithmetic operations. XY+ // postfix The relative position of the operator with respect to the operands tells whether the expression is written in postfix or infix notation. As the above expression shows, when the position of the Read More
Traversing and Searching a Linear Linked List
Traversing a list A linear list can be traversed in two ways In order traversal Reverse order traversal In order Traversal To traverse the linear linked list, we walk the list using the pointers, and process each element until we reach the last element. .cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; } .cl { margin: 0px; } .cb1 { color: green; } .cb2 { color: blue; } .cb3 { color: maroon; } void traverseinorder(node *head) { while(head!=NULL) { printf(“%dn”,head->info); head=head->next; } } Reverse Order Read More
Throw and Catch Mechanism
Throw Mechanism When an exception, which is desired to be handled, is detected, it is thrown using the throw statement. It can be used in any one of the following form: throw(exception) ; throw exception ; throw ; // used for re-throwing an exception The object passed to the throw statement may be of any type, including constants. It is also possible to throw objects not intended for error handling. When an exception is thrown, it will be caught by the catch statement associated with the try block, i.e., the Read More
Stacks
Stacks are one of the commonly used data structures. A stack is also known as a last in first out (LIFO) system. It can be considered as a linear list in which insertion and deletion can take place only at one end called the top. This structure operates in much the same way as a stack of trays. This article covers the representation of stacks using arrays. For the representation using self-referential structures, please refer to Stacks using Dynamic Memory Allocation. Here is a stack of trays: The following figures Read More
Stacks using Dynamic Memory Allocation
A stack that dynamically allocates memory for each of its elements (just like a linked list) is also known as a linked stack. The array-based representation of a stack suffers from the following limitations. The size of the stack must be known in advance. We may come across a situation when we need to push more elements in the stack, but cannot do so because we’ve reached the maximum size. However, a stack is an abstract data structure and should not become full. Hence, abstractly, it is always possible to Read More
Share on: