Friday, November 25, 2016

Notes on Data Structure

Download Notes on  Data Structure

Data structure Demo using C program

Data structure Demo using C program
    1.   Download  Application
    2.   Download Source1 (header file)
    2.   Download Source2

Tree Demo with menus using C Program
    1.   Download  Application
    2.   Download Source

Double Linked List Demo with menus using C Program
    1.   Download  Application
    2.   Download Source

Simple Encryption and Decryption program in C

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

void decrypt(char a[],int n)
{
 int i;
 for(i=0;i<strlen(a);i++)
 {
  a[i]=a[i]-n;
  }
 }

 void encrypt(char a[],int n)
 {
  int i;
  for(i=0;i<strlen(a);i++)
  {
   a[i]=a[i]+n;
   }
  }

  void main()
  {
   char name[100];
   clrscr();
   cout<<"enter a string\n";
  // cin>>name;
  gets(name);
   cout<<"original string:"<<name<<"\n";
   encrypt(name,4);
   cout<<"encrypted string:"<<name<<"\n";
   decrypt(name,4);
   cout<<"decrypted string:"<<name<<"\n";
   getch();
  }

C Program to find given IP Address belongs to which Class(A,B,C,D,E)


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int check_digit_dot(char ipstr[])
{
 for(int i=0;i<strlen(ipstr);i++)
 {
  if((ipstr[i]>=48 && ipstr[i]<=57)||(ipstr[i]=='.'))
  {
   //i++;
  }
  else
     return 0;   }
  }
   return 1;
}

int check_ip(char ipstr[])
{
 char temp[20];
 int a,num,dot=0,j=0,numcount=0;
 a=check_digit_dot(ipstr);
 temp[0]='\0';
 if(a==0)
  return 0;
  else
   {
    for(int i=0;i<=strlen(ipstr);i++)
    {
      if(ipstr[i]!='.')
       {
             temp[j]=ipstr[i];
             j++;
             temp[j]='\0';
       }
      else
       {
             num=atoi(temp);
            // cout<<"num="<<num<<"\n";
             dot++;
             if((num>=0 && num<=255) && strlen(temp)>0)
             {  numcount++; }
             //cout<<"numcount="<<numcount<<"\n";
             j=0;
             temp[j]='\0';
       }
    }
    // after last dot
    num=atoi(temp);
   // cout<<"num="<<num<<"\n";
    if ((num>=0 && num<=255) && strlen(temp)>0)
    { numcount++; }
   // cout<<"numcount="<<numcount<<"\n";
  }

  //cout<<"dots="<<dot<<"  numcount="<<numcount<<"\n";
  if(dot==3 && numcount==4)
    return 1;
  else
    return 0;
 }

 

void main()
{
 clrscr();
 char a[50],temp[50];
 int check,j=0,num;
 cout<<" enter ip address\n";
 cin>>a;
 check=check_ip(a);
 if(check==0)
 {
  cout<<"not valid ip address\n";
  }
  else
  {
   cout<<" valid  ip address\n";
   temp[0]='\0';
   for(int i=0;i<strlen(a);i++)
   {
    if(a[i]=='.')
     {     break;       }
    else
     {
      temp[j]=a[i];
      j++;
      temp[j]='\0';
     }
   }

    num=atoi(temp);
    cout<<"num:"<<num<<"\n";

    if((num>=0)&&(num<128))
    cout<<"given ip address is class A\n";
    else if((num>=128)&&(num<192))
    cout<<"given ip address is class B";
    else if((num>=192)&&(num<224))
    cout<<"given ip address is class C";
    else if((num>=224)&&(num<240))
    cout<<"given ip address is class D";
    else
    cout<<"given ip address is class E";
  }

  getch();
 }

C Program to find given IP Address is Valid or not


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int check_digit_dot(char ipstr[])
{
 for(int i=0;i<strlen(ipstr);i++)
 {
  if((ipstr[i]>=48 && ipstr[i]<=57)||(ipstr[i]=='.'))
  {
   //i++;
  }
  else
  {
   return 0;
   }
  }
   return 1;
}

int check_ip(char ipstr[])
{
 char temp[20];
 int a,num,dot=0,j=0,numcount=0;
 a=check_digit_dot(ipstr);
 temp[0]='\0';
 if(a==0)
  return 0;
  else
   {
    for(int i=0;i<=strlen(ipstr);i++)
    {
      if(ipstr[i]!='.')
       {
             temp[j]=ipstr[i];
             j++;
             temp[j]='\0';
       }
      else
       {
             num=atoi(temp);
             cout<<"num="<<num<<"\n";
             dot++;
             if((num>=0 && num<=255) && strlen(temp)>0)
             {  numcount++; }
             cout<<"numcount="<<numcount<<"\n";
             j=0;
             temp[j]='\0';
       }
    }

    // after last dot
    num=atoi(temp);
    cout<<"num="<<num<<"\n";
    if ((num>=0 && num<=255) && strlen(temp)>0)
    { numcount++; }
    cout<<"numcount="<<numcount<<"\n";
  }

  cout<<"dots="<<dot<<"  numcount="<<numcount<<"\n";
  if(dot==3 && numcount==4)
    return 1;
  else
    return 0;
 }

void main()
{
 clrscr();
 char a[50];
 int check;
 cout<<" enter ip address\n";
 cin>>a;
 check=check_ip(a);
 if(check==1)
 {
  cout<<"valid ip address";
  }
  else
  {
   cout<<"not valid address";
  }

  getch();
 }

RSA Encryption and Decryption with Numerical Example and C Program on RSA Encryption and Decryption


To generate the encryption and decryption keys, we can proceed as follows.

1. Generate randomly two “large” primes p and q.

2. Compute n = pq and  Ï•= (p − 1)(q − 1).

3. Choose a number e so that gcd(e, Ï•) = 1.

4. Find the multiplicative inverse of e modulo Ï•, i.e., find d so that ed ≡ 1 (mod Ï•).

This can be done efficiently using Euclid’s Extended Algorithm.

The encryption public key is KE = (n, e) and the decryption private key is KD = (n, d).

The encryption function is   E(M) = Me mod n.

The decryption function is   D(M) = Md mod n.

These functions satisfy  D(E(M)) = M and E(D(M)) = M for any 0 ≤ M ≤ n.

*****
Let’s look at a numerical example.

1. Let p = 7 and q = 13 be the two primes.

2. n = pq = 91 and Ï• = (p − 1)(q − 1) = 72.

3. Choose e. Let’s look among the primes.

            • Try e = 2;  gcd(2, 72) = 2 (does not work)
            • Try e = 3;  gcd(3, 72) = 3 (does not work)
            • Try e = 5;  gcd(5, 72) = 1 (it works)
            We choose e = 5.

4. Let’s find d. We want to find d such that  ed   1 (mod Ï•) which is equivalent to find d such that   ed + Ï• k = 1 for some integer k. Recall that gcd(e, Ï•) = 1.

            We can use the Extended Euclid’s Algorithm to find integers x and y such that
                         ex + Ï•y = gcd(e, Ï•).

            If  e = 5 and Ï• = 72, we find x = 29 and y = −2. 
            Indeed, 5(29) + 72(−2) = gcd(5,      72) = 1.    Then,         d = 29.    

            In general, we use d = x mod Ï•.

5. The encryption function is  E(M) = Me mod n = M5 mod 91.

    The decryption function is D(M) = Md mod n = M29 mod 91.

6. Suppose the message is M = 10.

            E(M) = E(10) = 105 mod 91 = 82

           Here 82 is called as CipherText

           D(E(M)) = D(82) = 8229 mod 91 = 10

7. Let’s see how to compute efficiently 8229 mod 91 using the square-and-multiply algorithm.

            (82)1     82 (mod 91)
            (82)2    81 (mod 91)
            (82)4  (81)2 9 (mod 91)
            (82)8  (9)2    81 (mod 91)
            (82)16 (81)2 9 (mod 91
 
            Since 29 = 16 + 8 + 4 + 1 (in binary 29 is 11101),  we deduce that
            8229      (82)16(82)8(82)4(82)1 (mod 91)
                        (9)(81)(9)(82) (mod 91)
                         10 (mod 91)
 
            We conclude that 8229 mod 91 = 10 (Original Message).

 

Consider an another example
1. Let p = 19 and q = 23 be the two primes.

2. n = p*q = 437 and Ï• = (p − 1)*(q − 1) = 396.

3. Choose e. Let’s look among the primes.

            • Try e = 2;  gcd(2, 396) = 0 (does not work because it has factors)
            • Try e = 3;  gcd(3, 396) = 0 (does not work because it has factors)
            • Try e = 5;  gcd(5, 396) = 1 (it works)
             We choose e = 5.

4. Let’s find d. We want to find d such that  ed   1 (mod Ï•) which is equivalent to find d such that   ed + Ï• k = 1 for some integer k. Recall that gcd(e, Ï•) = 1.

            We can use the Extended Euclid’s Algorithm to find integers x and y such that
                         ex + Ï•y = gcd(e, Ï•).  i.e.  5x+396y=1
                                                                   x= (1-396y)/5

            If  e = 5 and Ï• = 396,
            we find x = 317 and y = −4.  Indeed, 5(317) + 396(−4) = gcd(5, 396) = 1.
            Then,         d = 317.    

            In general, we use d = x mod Ï•.

5. The encryption function is  E(M) = Me mod n = M5 mod 437.

    The decryption function is  D(M) = Md mod n = M317 mod 437.

6. Suppose the message is M = 7.

            E(M) = E(10) = 75 mod 437 = 201

           Here 201 is called as CipherText

            D(E(M)) = D(201) = 201317 mod 437 = 7 (Original Message)

 
 
 
 
C program on RSA Encryption and Decryption
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int prime( int n)
{
 int i,r;
  for(i=2;i<n-1;i++)
  {
   r=n%i;
   if(r==0)
    {  return 0;  }
  }
  return 1;
 }
int gcd(int a, int b)
{
  int r=0;
  while(b>a)
  {
   r=b%a;
   if(r==0){ break;}
     b=a;
     a=r;
  }
   return a;
}
 
 void main()
 {
  clrscr();
  int n,pi,x,y,privatekey, publickey,p,q,primecheck,gcdvalue;
  cout<<"enter two prime numbers"<<"\n";
  cin>>p>>q;
  if (prime(p)==0 || prime(q)==0 ||p==q)
  {
     cout<<" one of the number is not prime or both numbers are same"<<"\n";
  }
  else
  {
   n=p*q;
   pi= (p-1)*(q-1);
   for(int e=3; e<pi-1; e++)
   {
      if (prime(e)==1)
       {
             if (gcd(e,pi)==1)
             {
               publickey= e;
               break;
             }
       }
   }
   cout<<"Public Key="<<publickey<<"\n";
   y=1;
   while(1)
   {
      x= (1-(pi*(-y)))/publickey ;
      if (publickey*x+pi*(-y)==1) { break; }
      y++;
   }
   privatekey=x;
   cout<<"private key="<< privatekey<<"\n";
   cout<<"enter message"<<"\n";
   int msg;
   cin>>msg;
   long ctext=1,i;
   for (i=0;i<publickey; i++)
    ctext=(ctext*msg)%n;
 
   ctext=ctext%n;
   cout<<"ciphertext or encrypt="<<ctext<<"\n";
   long ptext=1,j;
   for(j=0;j<privatekey;j++)
     ptext=(ptext*ctext)%n;
   ptext=ptext%n;
   cout<<"plain Text="<<ptext<<"\n";
  }
   getch();
 }
 
 

Thursday, April 18, 2013

Radix Sort Sorting Technique and C Program


Radix Sort
Consider an array  A= { 54, 33, 25, 17, 09, 37, 13 }.

In radix sort traverse array from left to right. Check maximum no. of digits in the array.  In the above case, it is 2 so it will have 2-pass i.e, in the second pass information will be in sorted.
Traverse array from left to right,  check unit place of  each number in the array.  Unit place of 54 is 4, so put 54 in bucket 4.  Similarly, unit place of 33 is 3, so put 33 in buctet 3.  Similarly, check all the numbers in the array and put those numbers in their respective buckets

Bucket
 Pass-1
Pass-2
0


1


2


3
33,13

4
54

5
25

6


7
17,37

8


9
09


Now  Merge the Output of pass-1.  Result is as follows A= {33,13,54, 25, 17, 37, 09}

Now tranverse output of Pass-1 from left to right.  Now check 10th place of each number in the array.  10th place of 33 is 3, put 33 in bucket 3.  Similarly, 10th place of 13 is 1, put 13 in bucket 1.  Similarly, check all the numbers in the array and put those numbers in their respective buckets.


Bucket
 Pass-1
Pass-2
0

09
1

13,17
2

25
3
33,13
33,37
4
54

5
25
54
6


7
17,37

8


9
9


Now Merge the output of pass-2.  Result is as follows A= {09, 13, 17, 25, 33, 37, 54}

Note: if Maximum no. of digits is 3 then there will be 3 passes. If 4-digits, 4 passes etc.


The same can be done using the following calculation.

Consider an array  A= { 54, 33, 25, 17, 09, 37, 13 }.

For Pass-1 Calculation (unit place)
 
     54/100  =  54/1 =  54 =  54%10 =  4       Put 54 in bucket 4
     33/100  =  33/1 =  33 =  33%10 =  3       Put 33 in bucket 3
       25/100  =  25/1 =  25 =  25%10 =  5       Put 25 in bucket 5
     17/100  =  17/1 =  17 =  17%10 =  7       Put 17 in bucket 7
     09/100  =  09/1 =  09 =  09%10 =  9       Put 09 in bucket 9
     37/100  =  37/1 =  37 =  37%10 =  7       Put 37 in bucket 7
     13/100  =  13/1 =  13 =  13%10 =  3       Put 13 in bucket 3
Bucket
 Pass-1
Pass-2
0


1


2


3
33,13

4
54

5
25

6


7
17,37

8


9
09


Merge output of pass-1 i.e., A= {33,13,54, 25, 17, 37, 09}

For Pass-2 Calculation (10th  place).
 
     33/101  =  33/10 =  3 =  3%10 =  3       Put 33 in bucket 3
     13/101  =  13/10 =  1 =  1%10 =  1       Put 13 in bucket 1
       54/101  =  54/10 =  5 =  5%10 =  5       Put 54 in bucket 5
     25/101  =  25/10 =  2 =  2%10 =  2       Put 25 in bucket 2
     17/101  =  17/10 =  1 =  1%10 =  1       Put 17 in bucket 1
     37/101  =  37/10 =  3 =  3%10 =  3       Put 37 in bucket 3
     09/101  =  09/10 =  0 =  0%10 =  0       Put 09 in bucket 0
Bucket
 Pass-1
Pass-2
0

09
1

13,17
2

25
3
33,13
33,37
4
54

5
25
54
6


7
17,37

8


9
09


Now Merge the output of pass-2.  Result is as follows A= {09, 13, 17, 25, 33, 37, 54}



C Program for radix sort.

#include<stdio.h>
#include<math.h>
struct node
{
   int data;
   struct node *link;
};
struct node * createnodes(int n)
{
  int i;
  struct node *p,*first,*temp;
  printf("Enter elements\n");
  for (i=0; i<n; i++)
  {
    p= (struct node *) malloc(sizeof(struct node));
    scanf("%d", &p->data);
    p->link = NULL;

    if (i==0)
    {
      first=p;
    }
    else
    {
      temp=first;
      while (temp->link!=NULL)
      {
            temp=temp->link;
      }
      temp->link=p;
    }
  }
  return first;
}

void displaynodes(struct node *f)
{
  struct node *temp;
  temp=f;
  while (temp!=NULL)
  {
    printf("%d-->",temp->data);
    temp=temp->link;
  }
  printf("Null\n");
}


int MaxDigits(struct node *f)
{
  struct node *temp;
  int max,ct,n;
  temp=f;
  max=0;
  while (temp!=NULL)
  {
    n=temp->data;
    ct=0;
    while(n>0)
    {
      ct++;
      n=n/10;
    }
    if (ct>max)
    { max=ct; }
    temp=temp->link;
  }
  return max;
}

void RadixSort(struct node *f1)
{
  struct node *first,*f[10],*r[10],*temp;
  int max,i,j,k,p;
  max=MaxDigits(f1);
  first=f1;
  for(i=0;i<max;i++)
  {
    for(j=0; j<10;j++)
    {
      f[j]=NULL;
      r[j]=NULL;
    }
    while(first!=NULL)
    {
      temp=first;
      first=first->link;
      p=(temp->data/pow(10.0,(float)i));
      p=p%10;
      if(f[p]==NULL && r[p]==NULL)
      {
            f[p]=temp;
            r[p]=temp;
            temp->link=NULL;
      }
      else
      {
            r[p]->link=temp;
            temp->link=NULL;
            r[p]=temp;
      }
    }
    first=f[0];
    for(k=1;k<10;k++)
    {
      if (first==NULL && f[k]==NULL)
            first=NULL;
      else if (first==NULL && f[k]!=NULL)
            first=f[k];
      else
      {
            temp=first;
            while(temp->link!=NULL)
             { temp=temp->link; }
            temp->link=f[k];
      }
    }
    printf("Result of Pass - %d  : ",i+1);
    displaynodes(first);
  }
}




void main()
{
  int n;
  struct node *first;
  clrscr();
  printf ("Enter size of array/Linked list \n");
  scanf("%d",&n);
  first = createnodes(n);
  printf("Printing the elements from LL before sorting\n");
  displaynodes(first);
  RadixSort(first);
  getch();
}