Wednesday, April 14, 2010

Multithreading programming in Win32

Here is the simple application that creates 100 threads and each thread executes the StartThraed() function


DWORD WINAPI StartThread(LPVOID threadID)
{
int tid = atoi((char *)threadID);
cout< return 0;
}

int main()
{
HANDLE hThread1;
DWORD dwGenericThread;
char threadID[10];

for (int x = 0; x < 10; ++x)
{
for (int y = 0; y < 10; ++y)
{
sprintf(threadID, "%d", y*10+x);
hThread1 = CreateThread(NULL,0,StartThread,&threadID,0,&dwGenericThread);
if(hThread1 == NULL)
{
DWORD dwError = GetLastError();
cout<<"SCM:Error in Creating thread"< return 0;
}
WaitForSingleObject(hThread1,INFINITE);
CloseHandle(hThread1);
}
}
}

Thursday, April 8, 2010

Singleton class, a different way

We all knew that, the below class will give us only one instance of the class.
class Singleton
{
private:
static Singleton* singleton;
private:
Singleton();
public:
static Singleton* getIntsance();
};

Just think that is there any other way to do the same. Yes, we have onother solution for it. See below

class Singleton
{
private:
static Singleton* singleton;
private:
~Singleton();
public:
static Singleton* getIntsance();
};

Destructor is in private scope now.

Tuesday, March 30, 2010

how to find out middle element of a linked list in a single traversal

program:

Node* findMidNode(Node* start)
{
Node* midNode = start;
while(start != NULL || start->next != NULL)
{
midNode = midNode->next;
start = start->next->next;
}
return midNode;
}

Algorithm
:

midNode is pointing to next of the midNode in every iteration and
start is pointing to next to next (i.e., increment 2) of start in every iteration.
so, when start is at the end, then midNode is pointing to middle element of the List.

reverse the linked list using recursive method


Algorithm:

Here the algorithm is, pointing p and q to the next elements in every iteration. Once we reached the end, then start pointing to back, i.e.., q->next = p;


//structure
struct Node
{
int data;
struct Node* next;
};

//global variables...
struct node* start = NULL;
struct node* p = NULL;
struct node* q = NULL;

//assusme that we have added some elements into the List, and start
//is pointing to first element in the list
reverseList(start);

void reverseList(struct Node* node)
{
if (node->next == NULL)
break;

p = node->next;
q = p->next;
if (q->next != NULL)
{
reverseList(q);
}
else
{
start->next = NULL;
start = q;
}
q->next = p;
}

how to print the bits in a reverse order (without using any variable)

Suppose, 123 is represented as 01111011. But we have to print it in reverse order, i.e.., 11011110

program

int main()
{
int i = 123;
while(i!=0)
{
if (i & 1)
printf("1");
else
printf("0");
i >>= 1;
}
}

virtual destructor purpose

Que: If there is a single virtual function in a class, one of the function of that class shold be virtual. what is that function?
Ans: Destructor

purpose of virtual destructor:

We know that, virtual destructor will help to call correct Version of destructor in dynamic binding.


class Base
{
private:
int x;
public:
virtual int getValue() {return x;}
};

class Derived : public Base
{
private:
int y;
public:
int getValue() {return y;}
};

int main()
{
Base* b = new Der();
delete b;
}


In the above program, do we need a virtual destructor?

No, we don't need a virtual destructor in the above program.
Here, there is no problem in deleting delete b;


suppose the Derived class is some thing like this

class Derived : public Base
{
private:
int y;
int* p;
public:
int getValue() {return y;}
};

then the problem will occur. What is this problem.

The problem is ....

Assume that you have allotted memory for "int *p" and the destructor is not virutal
in the case Base* pb = new Derived( );. The destructor of base class gets called
and there is no way we can free the memory allotted for p inside derived.
So, a memory leak.

Yes, to avoid memory leaks we have to use virtual destructor.

Not only for memory leaks, but also used to releasing Filehandles/resources and unlocking mutexes.

Saturday, March 27, 2010

To find the kth largest element in a Binary Search Tree, BST

1. Count the number of elements in a tree.

int nodeCount = GetNumberOfNodes(struct Node* root);

2. Traverse the tree using inorder traversal

In-order traversal is Left, Root node, Right

InorderTraversal(root, 0, count);

Root = root node of the tree
nodeCount = number of nodes in a tree
currNode = current node number
K = kth largest element in a tree

struct Node
{
int data;
struct Node* left;
struct Node* right;
};

InorderTraversal(struct Node* root, int currNode, int nodeCount)
{
if (nodeCount - currNode == k)
{
printf(“%d th largest element is: %d”, k, root->data);
break;
}

if (node->left != NULL)
InorderTraversal(node->left);
else if(node->right != NULL)
InorderTraversal(node->right);

}

Note: By traversing the BST tree using in-order, means we are traversing the tree in a sorted order. So that we will get the sorted array. Therefore, kth largest element is arr[numberOfNodesInaBST-k]; and kth smallest element is
Arr[k];