To insert an element in the list, the first task is to create a new node, assign the element to be inserted to the info field of the node, and then place the new node at the appropriate position by adjusting the appropriate pointers. Insertion 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 Insertion at the Beginning of the List First, test whether the linked list is initially empty, if yes, then Read More
Binary Search
This method works on sorted lists by progressively making better guesses to find the location of a search key. Illustration Consider the following array: 3 10 15 20 35 40 60 Suppose we want to search the element "15" We take beg = 0, end = 6 and compute the location of the middle element as We then compare the search key with mid i.e. a[mid]=a[3] is not equal to 15. Since beg<end, we start the next iteration. As a[mid]=20>15, therefore, we take end = mid-1 = 3-1 = 2 Read More
Type Conversion – Class to Basic Type
The constructor handles the task of converting basic types to class types very well. But you cannot use constructors for converting class types to basic data types. Instead, you can define an overloaded casting operator that can be used to convert a class data type into a basic data type. The general form of an overloaded casting operator function is shown below. This function is also known as a conversion function. Syntax: .cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; } .cl { margin: 0px; } .cb1 Read More
Circular Queue
The difficulty of managing front and rear in an array-based non-circular queue can be overcome if we treat the queue position with index 0 as if it comes after the last position (in our case, index 9), i.e., we treat the queue as circular. Note that we use the same array declaration of the queue. Empty queue: Non-empty queues: Implementation of operations on a circular queue: Testing a circular queue for overflow There are two conditions: (front=0) and (rear=capacity-1) front=rear+1 If any of these two conditions is satisfied, it means Read More
Searching
Searching is the process of finding the location of a given element in a set of elements. The search is said to be successful if the given element is found i.e., the element does exist in the collection (such as an array); otherwise it is unsuccessful. Two simple approaches to searching are: Linear search: This method traverses a list sequentially to locate the search key. Binary search: This method works on sorted lists by progressively making better guesses to find the location of a search key.
Inserting in a Doubly Linked List
Inserting an Element To insert an element in the list, the first task is to allocate memory for a new node, assign the element to be inserted to the info field of the node, and then the new node is placed at the appropriate position by adjusting appropriate pointers. Insertion 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 Insertion at the Beginning of the List First, test whether Read More
File Manipulation
A file is basically a set of records stored on your computer's secondary memory. There may be situations requiring a program to open more than one file. The strategy for opening multiple files depends upon how they will be used. If the situation requires simultaneous processing of two files, then you need to create a separate stream for each file. However, if the situation demands sequential processing of files (i.e., processing them one by one), then one can open a single stream and associate it with each file in turn. Read More
Preprocessor Directives
The C++ preprocessor is a program that is executed before the source code is compiled. A preprocessor command is called directives. It begins with a hash symbol (#). No white space should appear before the # and a semi colon is not required at the end of the statement. Things that can be done during the preprocessing stage are: Inclusion of the header file through #include directive Definition of a symbolic constant and macros through #define directive The #define preprocessor allows us to define symbolic names and constants. For example: Read More
Functions – Pass by value
Functions A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program. A programmer can solve a simple problem in C++ with a single function. Difficult and complex problems can be decomposed into sub-problems, each of which can be either coded directly or further decomposed. Decomposing difficult problems, until they are directly code-able as single C++ functions, is a software engineering method of stepwise refinement. These functions are combined into other functions and are ultimately used in main() Read More
Insertion of an Element in a Heap
An element is initially inserted as the last child of the heap. On insertion, the heap remains a complete binary tree, but the order property may be violated. We have to perform a series of operations to move the element up from the last position until either it ends up in a position where the order property is satisfied or we hit the root node. In this tutorial, we refer to this process as the reheapify upward operation. Algorithm ReheapifyUpward(heap,start) Here heap is a linear array and start is the Read More
Share on: