Sunday, August 18, 2013

Matrix Rotation by 90 Degree Clockwise direction

// Matrix rotation by 90 degree clockwise direction...for N*N matrix.

solution 1:- No extra space.(with extra space see below solution).
#include <stdio.h>
#include <string.h>
#include <math.h>
#define N 4   // for row and column (square matrix).
void Display(int arr[N][N])
{
int i=0,j=0;
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
  printf("%d ",arr[i][j]);
printf("\n");
}
}
//Rotation goes here
void Rotate(int arr[N][N])
{
int i,j,k,p,temp;  

for(i=0;i<sqrt(N);i++)
{
  k=N-1-i;p=N-1-i;
  for(j=i;j<N-i-1;j++)
  {
      temp=arr[i][j];
      arr[i][j]=arr[k][i];
      arr[k][i]=arr[p][k];
      arr[p][k]=arr[j][p];
      arr[j][p]=temp;
     k--;
  }
}
}
main()
{
    int arr[N][N];
    int i=0,j=0;
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
       scanf("%d",&arr[i][j]);
      Rotate(arr);
       Display(arr);
      return 0;
}

solution2:- with O(N) extra space

#include <stdio.h>
#include <string.h>
#define N 3
void Display(int arr[N][N])
{
int i=0,j=0;
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
  printf("%d ",arr[i][j]);
printf("\n");
}
}
//Rotation goes here
void Rotate(int arr[N][N])
{
int i,j,k,p,temp;  
int T[N];
for(i=0;i<2;i++)
{
    for(j=i;j<N-i;j++)
    {
    T[j]=arr[i][j];  
    }
    j--;
    for(k=i;k<N-i;k++)
    {
        temp=arr[k][j];
        arr[k][j]=T[k];
        T[k]=temp;
    }
    k=k--;
    p=i+1;
    for(k--;k>=i;k--)
    {
        temp=arr[j][k];
        arr[j][k]=T[p];
        T[p++]=temp;
    }
    p=i+1;//
    k++;
    for(--j;j>=i;j--)
    {
        temp=arr[j][k];
   
       arr[j][k]=T[p];
        T[p++]=temp;
    }
    j=i;p=i+1;
    for(k=p;k<N-p;k++)
    arr[j][k]=T[k];
}
}
main()
{
    int arr[N][N];
    int i=0,j=0;
    for(i=0;i<N;i++)
     for(j=0;j<N;j++)
       scanf("%d",&arr[i][j]);
      Rotate(arr);
       Display(arr);
      return 0;
}

Saturday, August 17, 2013

Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop

DEFINITION 
Circular linked list: A (corrupt) linked list in which a node's next pointer points 
to an earlier node, so as to make a loop in the linked list. 
EXAMPLE 
Input: A - > B - > C - > D - > E - > C [the same C as earlier] 
Output: C 

#include <stdio.h>
#include <stdlib.h>
 
typedef struct LinkList
{
    int data;
    struct LinkList *next;
}List;
 
void CreateList(List **Head,int value)
{
    List *ptr=NULL,*node=(List*)malloc(sizeof(List));
    node->data=value;
    node->next=NULL;
    if(*Head==NULL)
    *Head=node;
    else
     {ptr=*Head;
      while(ptr->next)
      ptr=ptr->next;
      ptr->next=node;
         
     }
}
 
List *CorruptNode(List *Head)
{
    List *sptr=Head,*fptr=Head;
    if(!Head)return NULL;
    
    while(fptr->next!=NULL)
    {
        fptr=fptr->next;
        if(fptr->next!=NULL)
        fptr=fptr->next;
        else
        break;
        sptr=sptr->next;
        if(fptr==sptr)
        {
            fptr=Head;
            while(fptr!=sptr)
            {
                fptr=fptr->next;
                sptr=sptr->next;
            }
        return fptr;
        }
    }
return NULL;
}
 
 
main()
{  
List *Head=NULL,*Head1=NULL,*Head2=NULL;
int i=0,value,n=3;
printf("Enter the no. of elements in 2nd list ");
scanf("%d",&n);
for(i=0;i<n;i++) {  scanf("%d",&value);  CreateList(&Head1,value); }
printf("Enter the no. of elements in 2nd list ");
scanf("%d",&n);
for(i=0;i<n;i++)                     
{
 scanf("%d",&value);
 CreateList(&Head2,value);
}
 
Head=Head1;
while(Head1->next!=NULL)
Head1=Head1->next;
List *temp;
temp=Head1;
Head1->next=Head2;
while(Head2->next!=NULL)
Head2=Head2->next;
Head2->next=temp;   //creating corrupt node as List1 last node 
temp=CorruptNode(Head);
if(temp)
printf("corrupt one is= %d",temp->data);
else
printf("No corrupt node");
return 0;
}

Friday, August 16, 2013

Adding two numbers using Singly Linked List

This program has asked many times in Amazon written..... Addition of two number's using singly linked list.
// A simple solution would be ..... easy to follow the below program.
Example:- n1=1 2 3
                n2=5 6 7 8
              ouptut would be  5 8 0 1

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

typedef struct LinkList
{
    int data;
    struct LinkList *next;
}List;

void CreateList(List **Head,int value)
{
    List *ptr=NULL,*node=(List*)malloc(sizeof(List));
    node->data=value;
    node->next=NULL;
    if(*Head==NULL)
    *Head=node;
    else
     {ptr=*Head;
      while(ptr->next)
      ptr=ptr->next;
      ptr->next=node;
       
     }
}
void AddLL(List **Head,List *Head1,List *Head2)
{
    int sum=0,h1=0,h2=0;
    List *node=NULL,*ptr=NULL;
   Reverse(&Head1);Reverse(&Head2);
     while(Head1!=NULL||Head2!=NULL||sum>0)
     {   h1=0;h2=0;
         if(Head1!=NULL)
         {h1=Head1->data;Head1=Head1->next;}
         if(Head2!=NULL)
         {h2=Head2->data;Head2=Head2->next;}
         sum+=h1+h2;
         node=(List *)malloc(sizeof(List));
         node->data=sum%10;
         node->next=NULL;
         sum/=10;
         if(*Head==NULL)
         {
             *Head=node;
             ptr=*Head;
         }
         else
         {
             ptr->next=node;
             ptr=ptr->next;
         }
     }

Reverse(Head);
}

void Reverse(List **Head)
{
 List *temp=NULL,*ptr=*Head,*prev=NULL;
 if(!ptr)return;
 while(ptr->next!=NULL)
 {
     temp=ptr->next;
     ptr->next=prev;
     prev=ptr;
     ptr=temp;
 }
 ptr->next=prev;
 *Head=ptr;
}

void Display(List *ptr)
{
    if(!ptr)return;
    printf("%d ",ptr->data);
    Display(ptr->next);
}
main()
{
List *Head=NULL,*Head1=NULL,*Head2=NULL;
int i=0,value,n;
printf("Enter the length of digit of first no.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
 scanf("%d",&value);
 CreateList(&Head1,value);
}
printf("\nEnter the length of digit of first no.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
 scanf("%d",&value);
 CreateList(&Head2,value);
}
printf("\n");
AddLL(&Head,Head1,Head2);
Display(Head);
return 0;
}

Quicksort using singly linked list

//Quicksort using singly linked list.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct LinkList 
{
    int data;
    struct LinkList *next;
}List;
void Insert(List **Head,int value)
{
    List *ptr=*Head;
    List *newnode;
    newnode=(List*)malloc(sizeof(List));
    newnode->data=value;
    newnode->next=NULL;
    if(ptr==NULL)
    *Head=newnode;
    else
    {
        while(ptr->next!=NULL)
        ptr=ptr->next;
        ptr->next=newnode;
    }
    
}
void Quicksort(List *Head,List *end)
{  if(Head==end)return;
    int count=0,count1=0,count2=0,temp=0;
    List *front=Head,*back=end,pf,bf,*ptr=Head,*pivot=Head;
    while(ptr!=end)
    {
        count++;
        ptr=ptr->next;    
    }
 
    while(count>=0&&front->next!=back)
    {
        while(front->next&&front->next->data<pivot->data)
          front=front->next;
        while(count>0&&front->next!=back)
        {
            back=Head;
            count1=--count;
            while(count1)
            {
                back=back->next;
                count1--;
            }
            if(back->data<pivot->data)
            {
               temp=front->next->data;
               front->next->data=back->data;
               back->data=temp;
               front=front->next;
               count;
               break;
            }
        }
   
   }
    temp=front->data;
    front->data=pivot->data;
    pivot->data=temp;
   // Display(Head);
    Quicksort(Head,front);
   Quicksort(front->next,end);
}
void Display(List *Head)
{
    if(!Head)return ;
    printf("%d  ",Head->data);
    Display(Head->next);
}
int main(void)
{
List *Head=NULL;
int i,value;
for(i=0;i<10;i++) 
{
    scanf("%d",&value);
    Insert(&Head,value);
}
    Display(Head);printf("\n");
    Quicksort(Head,NULL);
    printf("\n");
   Display(Head);
return 0;    
}
// Input could be like... 9  8  11  2  15  4  13  1  17  5