Wednesday, 8 April 2015

Frequently used String operations in C & C++

Frequently used String operations in C & Cpp with code examples:

So, the below are the some of the working and perfectly tested common string operations, that we have used in our projects.

GetStrLength
Strdup
AddString2List
GetCount
Destroy
StrTokenizeCmplx
Strcpy
StrEqual
FindStrEqualn
MakeStrLower

Sunday, 5 April 2015

Linked list implementation using Templates in C++

Linked list implementation using Templates in Cpp:

Working example for different operations on the linked list using the templates. The below is the header file of the linked list contains below list of functions

Insert: this function is used to add the new node into the list.
Delete: this function is used to delete the requested node from the linked list
Search: this function is used to search the node requested node from the linked list
Reverse. This function is used to reverse the linked list.

Sort: This function is used to sort the list using merge merge sort algorithm
Merge: This function is used to merge the sorted nodes in the list.

Double linked list example code in C++ using templates


Double linked list example code in Cpp using templates:

The below listed functions are used to manipulate the double linked list.

GetHeadNode: This function gets the head node from the list.
GetLastNode: This function gets the last node from the list.
GetCount (): This function gets the number of nodes in the list.
AddNode:This function adds the new node nto the list.
RemoveAt: This function remove the node at the given index.
GetAt:This function gets the node at the given index.
Destroy:This function destroys the entire list.

Wednesday, 1 April 2015

Basics of Linked Lists in C++

Basics of Linked Lists in Cpp with code examples:

A linked list is a data structure consisting of a group of nodes which together represent a sequence. Linked lists are used to store collections of data. The specific type of element is not important since essentially the same structure works to store elements of any type.

Each node is composed of a data and a reference to the next node in the sequence. More complex variants add additional links. Each node contain two fields: an integer value and a link to the next node.

The last node is linked to a null terminator used to signify the end of the list.


Convert the Decimal value to the binary in C++

Convert the Decimal value to the binary in Cpp with code example:

We can convert the decimal values to binary values in two different ways
In this example one we need to include the iomanip to convert decimal to binary
#include <iomanip> 


Unsigned & signed integers implementation in C++

Unsigned & signed integers implementation in Cpp with code example:

The below program gives us to identify how the difference between signed and unsigned integer affects the loops when they are used as loop variables.

Setting and clearing the bit in C++:



Setting and clearing the bit in C++:

#include <iostream>

using namespace std;

bool GetBitVal(int Num, int iIndex)
{
 return ( (Num & (1 << iIndex) ) > 0);
}

int SetBitVal(int Num, int iIndex, bool b)
{
 if(b)
  return (Num | (1 << iIndex)) ; 
 else {
  int mask = ~(1 << iIndex);
  return Num & mask;
 }
}

int main ()
{
 int iNumber = 0, iIndex = 0;

 iNumber = 20;

 cout << "Input" << endl;
 for (int iInd = 7; iInd >= 0; iInd--) 
  cout << GetBitVal(iNumber,iInd) << " ";
 cout << endl;

 /* set bit */
 iIndex = 4;
 cout << "Setting " << iIndex << "-th bit" << endl;
 iNumber = SetBitVal(iNumber, iIndex, true);
 for (int iInd = 7; iInd >= 0; iInd--) 
  cout << GetBitVal(iNumber,iInd) << " ";
 cout << endl;

 /* unset (clear) bit */
 iIndex = 5;
 cout << "Unsetting (Clearing) " << iIndex << "-th bit" << endl;
 iNumber = SetBitVal(iNumber, iIndex, false);
 for (int iInd = 7; iInd >= 0; iInd--) 
  cout << GetBitVal(iNumber,iInd) << " ";
 cout << endl;
  getchar ();

 return 0;
}
Output of the above program is:
Input
0 0 0 1 0 1 0 0
Setting 4-th bit
0 0 0 1 0 1 0 0
Unsetting (Clearing) 5-th bit
0 0 0 1 0 1 0 0

Bitwise operators with examples in C++

Bitwise operators with examples in Cpp with code examples:

Byte is the lowest level at which we can access data; there is no "bit" type.
And also it is not possible to perform operations on a single bit -- every bitwise operator will be applied to, at a minimum, an entire byte at a time.

Using bitwise operators, it is possible to write functions that allow us to compact 8 Booleans into a single byte-sized variable, enabling significant memory savings at the expense of more complex code. In the past, this was a good trade-off. Today, at least for application programming, it is probably not.