Sunday, May 18, 2014

Thread synchronization to print alternate numbers in two threads irrespective of scheduling.



#include <stdio.h>
#include <pthread.h>
//#define

int gcount;
int thread_1();
int thread_2();

pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;


int main(int argc,char *argv[])
{

    pthread_attr_t *attr;
    pthread_t threadId[3];

    pthread_mutex_init(&count_mutex, NULL);
    pthread_cond_init (&count_threshold_cv, NULL);

    printf("entering main \n");

    pthread_create(&threadId[1],NULL,(void*)thread_1,NULL);
    pthread_create(&threadId[2],NULL,(void*)thread_2,NULL);

    while(1){
        sleep(2);
    };
    return 0;
}

int thread_1()
{
    printf("thread1 running \n");
    while(1)
    {
        pthread_mutex_lock(&count_mutex);

        while(gcount%2 == 0)
        {
            pthread_cond_wait(&count_threshold_cv,&count_mutex);
        }
        if(gcount %2 == 1)
        {
            printf("thread1: %d \n",gcount++);
            pthread_cond_signal(&count_threshold_cv);
            sleep(2);
        }

        pthread_mutex_unlock(&count_mutex);

        //        printf("thread1 : counter %d \n",gcount++);
    }
    return 1;
}

int thread_2()
{
    printf("thread2 running \n");
    while(1)
    {
        pthread_mutex_lock(&count_mutex);

        while(gcount%2 == 1)
        {
            pthread_cond_wait(&count_threshold_cv,&count_mutex);
        }
        if(gcount%2 == 0)
        {
            printf("thread2: %d \n",gcount++);
            pthread_cond_signal(&count_threshold_cv);
            sleep(2);
        }

        pthread_mutex_unlock(&count_mutex);
    }
    return 1;
}


Wednesday, December 11, 2013

numberswhosedifferenceisminandproductisgivensum

#include <stdio.h>
#include <math.h>
int main(int argc,char* argv[])
{
 int big=0,small=0;
 int givenSum=0,product=0;
 printf("Enter a number :");
 scanf("%d",&givenSum);
 big = ceil(sqrt(givenSum));
 small = big -1;
 printf("big %d\n",big);
 product = big*big;
 if((product-givenSum) <= 2 && (product-givenSum)>=0)
 {
  printf("numbers are %d %d\n",big,big);
  return 0;
 }

 while(1)
 {
  product = big * small;
  if( ((product-givenSum) <= 2) && ((product-givenSum)>=0))
  {
  printf("numbers are %d %d\n",big,small);
  break;
  }
  if(product > givenSum)
   small--;
  else
     big++;
 }
 return 0;
}

Tuesday, December 3, 2013

Binary search tree traversal logic

#include <stdio.h>
#include <stdlib.h>

#define DEBUG printf
#define LOG printf

typedef struct _bst bst;
typedef struct _linked_list list;
typedef struct _Queue queue;
typedef struct _bst
{
    int data;
    bst* left;
    bst* right;
}bst;
typedef struct _linked_list
{
    void* data;
    list *next;
}list;

void inorder_traversal(bst *node);
int insert(bst **node,int key);
void preorder_traversal(bst *node);
void postorder_traversal(bst *node);
void find_path_with_givensum(bst *node,int sum,int givenSum,int *found);
void levelorder_traversal(bst *node);

// QUEUE API starts
queue* newQ();
void* dequeue(queue* q);
void enqueue(queue* q,void* data);
void testQueue();
void printQ(queue *q);
// QUEUE API END
bst *tree = NULL;

typedef struct _Queue
{
    list *front,*rear;
}queue;

int main()
{
    int i,j;
    int ch;
    int arr[]={14,8,9,5,7,17,15,20,22};
    int len = (sizeof(arr)/sizeof(arr[0]));

    for(i=0;i<len;i++)
    {
        insert(&tree,arr[i]);
    }

    while(1)
    {
        LOG("1.Insert 2.inorder 3.preorder 4.postorder 5.givensum 6.level order 7.In-order(without recursion) 8.QUEUE 9.quit \n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            LOG("Enter the element to insert \n");
            scanf("%d",&i);
            insert(&tree,i);
            break;
        case 2:
            LOG("In order traversal of the tree\n ");
            LOG("------------------------------\n");
            inorder_traversal(tree);
            LOG("------------------------------\n");
            break;
        case 3:
            LOG("In order traversal of the tree\n ");
            LOG("------------------------------\n");
            preorder_traversal(tree);
            LOG("------------------------------\n");
            break;
        case 4:
            LOG("Post order traversal of the tree \n");
            LOG("------------------------------\n");
            postorder_traversal(tree);
            LOG("------------------------------\n");
            break;
        case 5:
            {
                int found = 0 ;
                bst *node = tree;
                int givenSum ;
                LOG("Given Sum \n");
                scanf("%d",&givenSum);
                LOG("------------------------------\n");
                find_path_with_givensum(node,0,givenSum,&found);
                LOG("found count %d \n",found);
                LOG("------------------------------\n");
            }
            break;
        case 6:
        {
            LOG("Level order traversal of the tree\n ");
            LOG("------------------------------\n");
            levelorder_traversal(tree);
            LOG("------------------------------\n");
        }
        break;
        case 8:
        {
            testQueue();
        }
        break;
        case 9:
            LOG("Bye\n");
            exit(0);
            break;
        default:
            LOG("Invalid option\n");
            break;
        }
    }
    return 0;
}


void testQueue()
{
    int ch;
    int exitloop = 0;
    queue *q = NULL;
    int data;
    int *pdata=NULL;

    while(!exitloop)
    {
        LOG("1.Create Q 2.Enqueue 3.Dequeue 4.DeleteQueue 5.Print Q\n");
        scanf("%d",&ch);

        switch(ch)
        {
        case 1:
            q = newQ();
            break;
        case 2:
            scanf("%d",&data);
            enqueue(q,(void *)&data);
            break;
        case 3:
            pdata = (int *)dequeue(q);
            LOG("data %d\n",*pdata);
            break;
        case 4:
            free(q);
            q=NULL;
            break;
        case 5:
            printQ(q);
            break;
        case 9:
            exitloop=1;
            break;
        default:
            break;
        }
    }
}

void printQ(queue *q)
{
    if(!q)
        return;

    list *tempNode = q->front;
    while(tempNode)
    {
        LOG("%d\n",tempNode->data);
        tempNode = tempNode->next;
    }
}

void enqueue(queue* q,void* data)
{
    list *tempNode = (list *)calloc(1,sizeof(list));
    tempNode->data = data;
    LOG("=> enqueuing %p data %d\n",data,((bst *)data)->data);
    if(q->front == NULL)
    {
        q->front = tempNode;
        q->rear = tempNode;
        return;
    }
    q->rear->next = tempNode;
    q->rear = tempNode;
}

void* dequeue(queue* q)
{
    void* rc = 0;
    list *tempNode= NULL;

    if(!q || !(q->front))
    {
        LOG("ERROR- empty queue passed \n");
        return NULL;
    }

    rc = q->front->data;
    tempNode = q->front;
    q->front = q->front->next;
    free(tempNode);
    return ((void*)rc);
}

queue* newQ()
{
    return (queue *)calloc(1,sizeof(queue));
}

void inorder_traversal(bst *node)
{
    if(node != NULL)
    {
        inorder_traversal(node->left);
        LOG("%d\n",node->data);
        inorder_traversal(node->right);
    }
}

void preorder_traversal(bst *node)
{
    if(node)
    {
        LOG("%d\n",node->data);
        preorder_traversal(node->left);
        preorder_traversal(node->right);
    }
}

void postorder_traversal(bst *node)
{
    if(node)
    {
        postorder_traversal(node->left);
        postorder_traversal(node->right);
        LOG("%d\n",node->data);
    }
}

void levelorder_traversal(bst *node)
{
    queue *q = newQ();
    bst* tempNode = NULL;
    enqueue(q,(void *)node);
    tempNode = (bst *)dequeue(q);
    while(tempNode!=NULL)
    {
        LOG("%d\n",tempNode->data);
        if(tempNode->left)
            enqueue(q,tempNode->left);
        if(tempNode->right)
            enqueue(q,tempNode->right);

        tempNode = (bst *)dequeue(q);
    }
}

void find_path_with_givensum(bst *node,int sum,int givenSum,int *found)
{

 if(node == NULL)
     return;

 sum = sum + node->data ;
 DEBUG("=> sum = %d,given sum %d node->data %d\n",sum,givenSum,node->data);

 if(sum == givenSum && node->left == NULL && node->right == NULL)
 {
     (*found)++;
     return;
 }
 else if(sum > givenSum)
 {
     LOG("This path doesn't contain the given sum , cumulative sum so far %d\n",sum);
     return;
 }
 else
 {
     find_path_with_givensum(node->left,sum,givenSum,found);
     find_path_with_givensum(node->right,sum,givenSum,found);
 }

 return ;
}

int insert(bst **node,int key)
{
    bst *newnode = NULL;
    int rc = 0;
    newnode = (bst *)malloc(sizeof(bst));
    newnode->data = key ;
    newnode->left = NULL;
    newnode->right = NULL;
    bst *temp = *node;

    if(*node == NULL)
    {
        DEBUG("inserting it at the head \n");
        *node = newnode;
    }
    else
    {
        while(temp)
        {
            DEBUG("Entering into the element %d \n",(temp)->data);
            if(key < (temp)->data)
            {
                if((temp)->left == NULL)
                {
                    (temp)->left = newnode;
                    LOG("Item inserted to the left of %d \n",(temp)->data);
                    return 1;
                }
                else
                    temp = (temp)->left;
            }
            else if(key > (temp)->data)
            {
                if((temp)->right == NULL)
                {
                    (temp)->right = newnode;
                    LOG("Item inserted to the right of %d \n",(temp)->data);
                    return 1;
                }
                else
                    temp = temp->right;
            }
            else
            {
                LOG("Element %d already exist inthe tree \n",newnode->data);
                free(newnode);
                return 0;
            }
        }
    }
    return rc;
}