Thursday 10 July 2014

Problem Statement: Given two numbers, where each digit is represented as a node of a linked list, add those numbers


 /*Given two numbers, where each digit is represented as a node of a linked list, add those numbers*/  
 #include<stdio.h>  
 #include<math.h>  
 #include<stdlib.h>  
 int cntList1=0;  
 int cntList2=0;  
 struct node  
 {  
  int data;  
  struct node *next;  
 };  
 /*Utility function to create node*/  
 void createNode(struct node** head, int data)  
 {  
  struct node *temp = (struct node *)malloc(sizeof(struct node *));  
  temp->data = data;  
  temp->next=(*head);  
  (*head) = temp;   
 }  
 /*Utility function to reverse list*/  
 void reverseList(struct node** head,int *cnt)  
 {  
  if(!head)  
  return;  
  else  
  {  
  struct node *prev,*curr,*next;  
  prev= NULL;  
  curr=(*head);  
  while(curr!=NULL)  
   {  
   next = curr->next;  
   curr->next=prev;  
   prev=curr;  
   curr=next;  
   (*cnt)++;  
   }  
  (*head) = prev;  
  }  
 }  
 /*Utility function to print list*/  
 void printList(struct node *head)  
 {  
  int i=0;  
  while(head)  
  {  
  printf("element %d : %d\n",i++,head->data);  
  head=head->next;  
  };  
 }  
 /*Utility function to calculate sum*/  
 void printSum(struct node* head1, struct node* head2)  
 {  
  reverseList(&head1,&cntList1);  
  //printf("Reversed List 1 is:\n");  
  //printList(head1);  
  reverseList(&head2,&cntList2);  
  //printf("Reversed List 2 is:\n");  
  //printList(head2);  
  int sum=0,power=0,result=0;  
  int carry=0;  
  struct node* root1 = head1;  
  struct node* root2 = head2;  
  while(cntList1 > 0 || cntList2 > 0)  
  {  
  if(root1 && root2)  
  {  
   sum = root1->data + root2->data + carry ;  
   root1 = root1->next;  
   root2 = root2->next;  
   cntList1--;  
   cntList2--;  
  }   
  else if(root1)  
  {  
   sum = root1->data + carry ;  
   root1 = root1->next;  
   cntList1--;  
  }  
  else if(root2)  
  {  
   sum = root2->data + carry ;  
   root2 = root2->next;  
   cntList2--;  
  }  
  carry = sum/10;  
  sum = sum % 10;  
  result = result + sum * pow(10,power);  
  power++;  
  //printf("cntList1 : %d cntList2 : %d\n",cntList1,cntList2);  
  //printf("sum: %d carry: %d result: %d\n",sum,carry,result);  
  };  
  if(carry)  
  result = result + carry * pow(10,power++);  
  printf("Sum is %d\n",result);  
 }  
 /*Helper function to create linked lists*/  
 int main()  
 {  
  struct node* head1 = NULL;  
  createNode(&head1,4);  
  createNode(&head1,2);  
  createNode(&head1,6);  
  createNode(&head1,1);  
  createNode(&head1,4);  
  //printf("List 1 is:\n");  
  //printList(head1);  
  struct node* head2 = NULL;  
  createNode(&head2,5);  
  createNode(&head2,7);  
  createNode(&head2,8);  
  createNode(&head2,0);  
  createNode(&head2,9);  
  //printf("List 2 is:\n");  
  //printList(head2);  
  printSum(head1,head2);  
  return 0;  
 }  

Problem Statment: Given a binary tree, find if there exists a root to leaf path of distance k.


 /*Level : Easy question */  
 #include <stdio.h>  
 #include <stdlib.h>  
 /* A binary tree node has data, pointer to left child  
   and a pointer to right child */  
 struct node  
 {  
   int data;  
   struct node* left;  
   struct node* right;  
 };  
 /* Helper function to create a new node */  
 struct node* newNode(int data)  
 {  
   struct node* node = (struct node*)  
                malloc(sizeof(struct node));  
   node->data = data;  
   node->left = NULL;  
   node->right = NULL;  
   return(node);  
 }  
 /*Utility function to determine path from root to leaf with given distance exists or not*/  
 int kdistance(struct node *root , int dist)  
 {  
  if(!root)  
  return 0;  
  if(dist==0 && root->left == NULL && root->right == NULL)  
  {  
  printf("leaf reached when k = 0 is %d\n",root->data);  
  return 1;  
  }  
  else  
  {  
  (--dist);  
  return (kdistance(root->left,dist)) || (kdistance(root->right,dist)) ;  
  }  
 }  
 /* Driver program*/  
 int main()  
 {  
   struct node *root1 = newNode(1);  
   root1->left = newNode(2);  
   root1->right = newNode(3);  
   root1->left->left = newNode(4);  
   root1->left->right = newNode(5);  
   root1->right->left = newNode(6);  
   root1->right->right = newNode(7);  
  int k=2;  
  if(kdistance(root1,k))  
     printf("Path exists for k = %d\n",k);  
    else  
  printf("Path does not exist for k = %d\n",k);  
   getchar();  
  return 0;  
 }  

Saturday 8 February 2014

Problem statement: Reverse a list in single traversal in C


 #include<stdio.h>  
 struct node  
 {  
   int data;  
   struct node *link;  
 };  
 void addNode(int data,struct node **rt)  
 {  
   struct node *temp=(struct node *)malloc(sizeof(struct node));  
   temp->data = data;  
   temp->link = (*rt);  
   (*rt)=temp;  
   printf("%d\n",(*rt)->data);  
 }  
 struct node *reverse(struct node **rt)  
 {  
   struct node *prev,*curr,*next;  
   prev= NULL;  
   curr=(*rt);  
   while(curr!=NULL)  
   {  
     printf("%d\n",curr->data);  
     next = curr->link;  
     curr->link=prev;  
     prev=curr;  
     curr=next;  
   }  
   return prev;  
 }  
 int main()  
 {  
   struct node *root=(struct node *)malloc(sizeof(struct node));  
   root->data = -1;  
   root->link=NULL;  
   addNode(5,&root);  
   addNode(4,&root);  
   addNode(9,&root);  
   addNode(1,&root);  
   addNode(3,&root);  
   addNode(8,&root);  
   addNode(10,&root);  
   addNode(13,&root);  
   addNode(2,&root);  
   struct node *head=(reverse(&root))->link;  
   while(head!=NULL)  
   {  
     printf("%d->",head->data);  
     head=head->link;  
   }  
 }  

Problem statement: Find a mid of list in single traversal in C


 #include<stdio.h>  
 struct node  
 {  
   int data;  
   struct node *link;  
 };  
 struct node *root;  
 void addNode(int val)  
 {  
   struct node *temp = (struct node *)malloc(sizeof(struct node));  
   temp->data = val;  
   temp->link = NULL;  
   if(root == NULL)  
   {  
     root = temp;  
     printf("%d\n",root->data);  
   }    
   else  
   {  
     struct node *ptr = root;  
     while(ptr->link !=NULL)  
     {  
       ptr = ptr->link;  
     }  
     ptr->link = temp;  
     printf("%d\n",ptr->link->data);  
   }  
 }  
 void findmid()  
 {  
   struct node *start , *end;  
   start = end = root;  
   while(end->link !=NULL)  
   {  
     start = start->link;  
     end = end->link;  
     if(end->link!=NULL)  
       end = end->link;  
   }  
   printf("\nMid element is %d\n",start->data);    
 }  
 int main()  
 {  
   addNode(4);  
   addNode(14);  
   addNode(54);  
   addNode(1);  
   addNode(23);  
   addNode(89);  
   addNode(56);    
   addNode(30);  
   addNode(55);  
   addNode(100);  
   //addNode(77);  
   findmid();  
 }  

problem statement: Given n, display nth node in a list in C.


 problem statement: Given n, display nth node in a list in C.  
 #include<stdio.h>  
 struct node  
 {  
   int data;  
   struct node *link;  
 };  
 struct node *root;  
 void addNode(int val)  
 {  
   struct node *temp = (struct node *)malloc(sizeof(struct node));  
   temp->data = val;  
   temp->link = NULL;  
   if(root == NULL)  
   {  
     root = temp;  
     printf("%d\n",root->data);  
   }    
   else  
   {  
     struct node *ptr = root;  
     while(ptr->link !=NULL)  
     {  
       ptr = ptr->link;  
     }  
     ptr->link = temp;  
     printf("%d\n",ptr->link->data);  
   }  
 }  
 void displaynthnode(int pos)  
 {  
   struct node *start , *move;  
   start = move = root;  
   int cnt = 0;  
   while(cnt != pos-1)  
   {  
     move = move->link;  
     cnt++;  
   }  
   while(move->link!=NULL)  
   {  
     start = start->link;  
     move = move->link;  
   }  
   printf("\nElement at %d position is:%d\n",pos,start->data);  
 }  
 int main()  
 {  
   int pos;  
   addNode(4);  
   addNode(14);  
   addNode(54);  
   addNode(1);  
   addNode(23);  
   addNode(89);  
   addNode(56);    
   addNode(30);  
   addNode(55);  
   addNode(100);  
   printf("\nEnter the element position:");  
   scanf("%d",&pos);  
   displaynthnode(pos);  
 }