In a doubly linked list, also called a two-way list, each node is divided into three parts: The first part, called the previous pointer field, contains the address of the preceding element in the list. The second part contains the information of the list. The third part, called the next pointer field, contains the address of the succeeding element in the list. In addition, two pointer variables, named head and tail, are used that contain the address of first element and the address of last element of the list. Implementation Read More
Template
A template is one of the features added to C++ recently. It is a new concept which enables the programmer to define generic classes and functions; thus provides support for generic programming. Generic programming is an approach, where generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures. A template can be used to create a family of classes or functions. For example, a class template for an array class would enable us to create arrays of various Read More
Merge Sort
Merging means combining elements of two arrays to form a new array. The simplest way of merging two arrays is to first copy all the elements of one array into a new array and then append all the elements of the second array to the new array. If you want the resultant array to be sorted, you can sort it by any of the sorting techniques. If the arrays are originally in sorted order, they can be merged in such a way as to ensure that the combined array is Read More
goto Statement
In programming, significance has always been given to use of structured programming technique to build reliable software that are easy to debug, maintain and modify. In some cases, performance is more important than strict obedience to structured programming technique. In these cases, some unstructured programming technique may be used. For example, we can use break to terminate execution of a repetition structure before the loop continuation condition becomes false. This saves unnecessary repetition of the loop if the task is completed before loop termination. Another instance of unstructured programming is Read More
Pointers and Arrays
Arrays and pointers are very closely linked together. In most contexts, C++ treats the name of an array as if it were a pointer i.e. memory address of some element. C++ interprets an array name as the address of its first element. That is, if age is an int array to hold 10 integers then age stores the address of age[0], the first element of the array. Consider the following code snippet: int *a ; // a is a pointer to an integer int age[10] ; cout<<"Enter the value for Read More
Doubly Linked List – Traversing and Search
Traversing a Doubly Linked List A doubly linked list can be traversed either way and that too very conveniently. Inorder traversal Reverse order traversal Inorder Traversal To traverse the doubly linked list, we walk the list from the beginning, 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; Read More
Deleting an Element from a Heap
Deleting an Element from the Heap Deletion always occurs at the root of the heap. If we delete the root element it creates a hole or vacant space at the root position. Because the heap must be complete, we fill the hole with the last element of the heap. Although the heap becomes complete, i.e. it satisfies the shape property, the order property of heaps is violated. As the value that comes from the bottom is small, we have to perform another operation to satisfy the order property. This operation Read More
Bubble Sort
It requires n-1 passes to sort an array. In each pass every element a[i] is compared with a[i+1], for i=0 to (n-k-1), where k is the pass number and if they are out of order i.e. if a[i]>a[i+1], they are swapped. This will cause the largest element to move up or bubble up. Thus after the end of the first pass the largest element in the array will be placed in the last or nth position and on the next pass, the next largest element will be placed at position Read More
Call-By-Reference in Functions
The call by reference uses a different mechanism. In place of passing value to the function, which is called, a reference to the original variable is passed. A reference is an alias for the predefined variable. That is, the value of that variable can be accessed by using any of the two: the original or the reference variable name. When a function is called by reference, then the formal parameters become reference to the actual parameters in the calling function. This means that, in call by reference method, the called Read More
Conditional Operators and Switch Statements
C++ has an operator that can be used as an alternative to an if-statement. This operator is called the conditional operator (?:). This operator can be used to replace an if-else statement of the following general form: if(expression) { statements ; – – } else { statements ; – – } The above form of if-else can be alternatively written using ?: as follows, expression1 ? expression2 : expression3 ; It works in the same way an if-statement executes. The execution is in the following manner, expression1 is evaluated If Read More
Share on: