...::: Recent Updates :::...

Saturday, May 28, 2011

DBMS II

Wednesday, May 18, 2011

Program for perform merge sort using linklist


//program for perform merge sort using linklist
//writter : kartik
//27-4-11

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

struct linklist
{
 int info;
 struct linklist *next;
}*start=NULL,*first=NULL,*start2,*temp,*node,*last;
typedef struct linklist lk;

void insert(lk **);
void disp(lk **);
void mergesort();

void main()
{
int ch;
char con,cn;
clrscr();

     do
     {
     printf("\n***********************************************");
     printf("\n      K@rtik's Merge sort");
     printf("\n***********************************************");
     printf("\n 1. insert sorting data:");
     printf("\n 2. display");
     printf("\n 3. perform mergeing sort");
     printf("\n================================================");
     printf("\nenter ur choice:");
     scanf("%d",&ch);

    switch(ch)
    {
      case 1:
      printf("\n enter data for 1st linklist");
      do
      {
        insert(&start);
        fflush(0);
        printf("\n do u wnt to insert more(y/n):");
        scanf("%c",&cn);
      }while(cn=='y');

      printf("\n enter data for 2nd linklist");

      do
      {
        insert(&first);
        fflush(0);
        printf("\n do u wnt to insert more(y/n):");
        scanf("%c",&cn);
      }while(cn=='y');

      break;

      case 2:
      printf("\n*********>display data of 1st linklist..........");
      disp(&start);
      printf("\n*********>display data of 2nd linklist..........");
      disp(&first);
      break;

      case 3:
      mergesort();
      break;

      default:
           printf("\n==> u must i/p 1 to 3 onlly.....");
      }
      fflush(0);
      printf("\n do u wnt to con(y/n):");
      scanf("%c",&con);
       }while(con=='y');
   getch();
}

void insert(lk **head)
{
lk *node=(lk *)malloc(sizeof(lk));
int item;

printf("\nenter item:");
scanf("%d",&item);

   if((*head)==NULL)
   {
     (*head)=(lk *)malloc(sizeof(lk));
     (*head)->info=item;
     (*head)->next=NULL;
    }
    else
    {
     lk *temp=(*head);
     while(temp->next!=NULL)
          temp=temp->next;

      node->info=item;
      temp->next=node;
      node->next=NULL;
      }
}

void disp(lk **head)
{
lk *temp=(*head);
printf("\n");
   if((*head)==NULL)
    printf("\n linklist is empty");
   else
   {
       while(temp)
       {
     printf("%3d",temp->info);
     temp=temp->next;
     }
    }
}

void mergesort()
{

lk *tmp1;
lk *tmp2;

lk (*start2)=(lk *)malloc(sizeof(lk));

tmp1=start;
tmp2=first;

start2=NULL;
   while((tmp1) && (tmp2))
   {

         temp=(lk *)malloc(sizeof(lk));
         temp->next=NULL;


          if(tmp1->info < tmp2->info)
           {
         temp->info=tmp1->info;
         tmp1=tmp1->next;
           }
          else if(tmp1->info > tmp2->info)
          {
        temp->info=tmp2->info;
        tmp2=tmp2->next;
        }


          if(start2==NULL)
         {printf("\n NULL");
            start2=temp;
            }
           else
            node->next=temp;
        node=temp;
       last=temp;
             }//end of while loop*/

      while(tmp1!=NULL)
      {
         temp=(lk *)malloc(sizeof(lk));
         temp->next=NULL;
         temp->info=tmp1->info;
         tmp1=tmp1->next;

        node->next=temp;
        node=temp;
       last=temp;
       }
       while(tmp2!=NULL)
       {
         temp=(lk *)malloc(sizeof(lk));
         temp->next=NULL;
         temp->info=tmp2->info;
         tmp2=tmp2->next;

        node->next=temp;
        node=temp;
       last=temp;
        }



     printf("\n ***** after merging sort:");
     disp(&start2);
}











----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com

Program for perfomr arithmetic operation on Two Polynomials using linklist


//program for perfomr arithmetic operation on Two Polynomials using linklist
//writter : K@rtik
//20-03-11  sunday

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

struct linklist
{
 int data;
 int power;
 struct linklist *next;
}*fnode=NULL,*start1=NULL,*last,*start2,*start3,*snode=NULL;

typedef struct linklist lk;
int item,i,pow,c;

void insert(lk **);
void add();
void sub();
void mul();
void disp(lk **);


void main()
{
char con;
int ch;
fnode=(lk *)malloc(sizeof(lk));
snode=(lk *)malloc(sizeof(lk));
fnode=NULL;
snode=NULL;

clrscr();

do
{
printf("\n\n ********************************************************");
printf("\n             K@rtik's LINKLIST OPERATION");
printf("\n\n -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
printf("\n *----->  1.Add Polynomial  equation                   *");
printf("\n *----->  2.Adddition of Two Polynomial                *");
printf("\n *----->  3.Subtraction of Two Polynomial              *");
printf("\n *----->  4.Multiplication of Two Polynomial           *");
printf("\n\n -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

printf("\n\n=> enter ur choice:");
scanf("%d",&ch);

       switch(ch)
       {
     case 1:
     printf("\n Enter 1st Equation\n");
     insert(&fnode);
     printf("\n enter 2nd Equation\n");
     insert(&snode);
     printf("\n 1st Polynomial equation is====>");
     disp(&fnode);
     printf("\n 2nd Polynomial equation is====>");
     disp(&snode);
     break;

     case 2:
     add();
     break;

     case 3:
     sub();
     break;

     case 4:
     mul();
     break;

     default:
     printf("\n u must enter any number from 1 to 5");
     }
 fflush(0);
 printf("\n\n==> do u want to con...(Y/N)?");
 scanf("%c",&con);
 }while (con!='n');
getch();
}
//user define code for add item at last
void insert(lk **start)
{
   lk *node,*temp,*first;
   while(1)
   {
   if((*start)==NULL)
    {
    printf("\n enter an item:");
    scanf("%d",&item);
    printf("\n enter power:");
    scanf("%d",&pow);

      (*start)=(lk *)malloc(sizeof(lk));
      (*start)->data=item;
      (*start)->power=pow;
      (*start)->next=NULL;
     }
   else
   {

    node=(lk *)malloc(sizeof(lk));
    printf("\n enter an item:");
    scanf("%d",&item);
   l: printf("\n enter power:");
    scanf("%d",&pow);

     temp=(*start);
     while(temp->next!=NULL)
     {
       if(pow >= temp->power)
       {
      printf("\n ==> u must i/p power value less then %d ",temp->power);
      goto l;
      }
      temp=temp->next;
      }

    node->data=item;
    node->power=pow;
    node->next=NULL;
    temp->next=node;

    } //end of else
     if(pow==0)
       break;
     }
}

//Udf for display Polynomial
void disp(lk **head)
{
   lk *node,*node1;
   node=(lk *)malloc(sizeof(lk));

   node=(*head);
   printf("%d",node->data);
   printf("x^%d",node->power);
   node=node->next;
      while(node!=NULL)
       {
       if(node->data > 0)
        printf("+%d",node->data);
       else
        printf("%d",node->data);

    if (node->power==0)
         goto l1;
    else
         printf("x");

       printf("^%d",node->power);
      l1:
       node=node->next;
    }

}

//udf for addition of two polynomial
void add()
{
 lk *temp1,*temp2,*temp,*node;
    temp1=fnode;
    temp2=snode;

    while(temp1 || temp2)
    {
         temp=(lk *)malloc(sizeof(lk));
         temp->next=NULL;
           if(temp1->power==temp2->power)
           {
         temp->power=temp1->power;
         temp->data=temp1->data + temp2->data;
         temp1=temp1->next;
         temp2=temp2->next;
           }
           else if(temp1->power>temp2->power)
           {
         temp->power=temp1->power;
         temp->data=temp1->data;
         temp1=temp1->next;
        }
           else
           {
         temp->power=temp2->power;
         temp->data=temp2->data;
         temp2=temp2->next;
           }
          if(start2==NULL)
            start2=temp;
           else
            node->next=temp;
         node=temp;
         last=temp;
         }//end of while loop
         printf("\n 1st Polynomial equation is====>");
         disp(&fnode);
         printf("\n\n 2nd Polynomial equation is====>");
         disp(&snode);
         printf("\n\n After addition the equation is ====>");
         disp(&start2);

 }

//udf for addition of two polynomial
void sub()
{
 lk *temp1,*temp2,*temp,*node,*start5=NULL;
    temp1=fnode;
    temp2=snode;
    while(temp1 || temp2)
    {
         temp=(lk *)malloc(sizeof(lk ));
         temp->next=NULL;

           if(temp1->power==temp2->power)
           {
         temp->power=temp1->power;
         temp->data=temp1->data - temp2->data;
         temp1=temp1->next;
         temp2=temp2->next;
           }
           else if(temp1->power>temp2->power)
           {
         temp->power=temp1->power;
         temp->data=temp1->data;
         temp1=temp1->next;
        }
           else
           {
         temp->power=temp2->power;
         temp->data=temp2->data;
         temp2=temp2->next;
           }
          if(start5==NULL)
            start5=temp;
           else
            node->next=temp;
         node=temp;
         last=temp;
        }//end of while loop
     printf("\n 1st Polynomial equation is====>");
     disp(&fnode);
     printf("\n\n 2nd Polynomial equation is====>");
     disp(&snode);
     printf("\n\n After Subtraction the equation is ====>");
     disp(&start5);

}

//mul

void mul()
{
 int max=0,c=0;
 lk *temp1,*temp2,*temp,*node,*start3=NULL,*start4=NULL;
 lk *tmp1,*tmp2;
    temp1=fnode;

    while(temp1)
    {
        temp2=snode;
        while(temp2)
        {
         temp=(lk *)malloc(sizeof(lk));
         temp->next=NULL;
         temp->data=temp1->data * temp2->data;
         temp->power=temp1->power+temp2->power;
             temp2=temp2->next;

         if(max < temp->power)
             max=temp->power;

         if(start3==NULL)
            start3=temp;
         else
            node->next=temp;

         node=temp;
         last=temp;
           }

       temp1=temp1->next;
      }
    tmp1=start3;
    while(tmp1)
    {
      c=0;
      tmp2=tmp1;
         temp=(lk *)malloc(sizeof(lk));
         temp->next=NULL;
         temp->data=0;

         while(tmp2)
         {
          if(max==tmp2->power)
           {
           c=1;
         temp->power=tmp2->power;
         temp->data =temp->data+ tmp2->data;
        }
         tmp2=tmp2->next;

          }//end of inner loop

           if(c==0)
           {
            temp->power=tmp1->power;
            temp->data=tmp1->data;
            }
           if(start4==NULL)
            start4=temp;
           else
            node->next=temp;
         node=temp;
         last=temp;

         max--;
           if (max<0)
          break;

          tmp1=tmp1->next;
        }//end of while loop
    printf("\n 1st Polynomial equation is====>");
    disp(&fnode);
    printf("\n\n 2nd Polynomial equation is====>");
    disp(&snode);
    printf("\n\n After Multiplication the equation is ====>");
    disp(&start4);
}







----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com

Monday, May 16, 2011

Mergesort And Quicksort

----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com

Sunday, May 15, 2011

QUICK SORT IN C


Quick sort

Quick sort sorts a list based on the divide and conquer strategy.
In quick sort algorithm we divide the list into two sub-list, sort these sub-lists and recursively until the list is sorted,
The basic steps of quick sort algorithm are as follows;
Step 1:          Choose a key element in the list which is called a key.
Step 2:          Reorder the list with the rule that all elements which are less than the key comes before the pivot and so that all elements greater than the key come after it, after the partitioning the key in its final position
Step 3:          Recursively re,order two sub-lists the sub-list of lesser elements and the sub-list of greater elements.

Quick Sort Code in C
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
int arr[size];
void create()
{
      int i;
      for(i=0;i<size;i++)
      {
            arr[i]=random(1000);
      }

}
void display()
{
      int i;
      for(i=0;i<size;i++)
      {
            printf("%d\t",arr[i]);
      }
}
void quick(int lb,int ub)
{
      int i,j,key,temp;
      int flag=0;
      i=lb+1;
      j=ub;
      key=arr[lb];

      while(flag==0)
      {
            if(lb<ub)
            {
                        while(arr[i]<key)
                        {
                              i=i+1;
                        }
                        while(arr[j]>key)
                        {
                              j=j-1;
                        }
                        if(i<j)
                        {
                              temp=arr[i];
                              arr[i]=arr[j];
                              arr[j]=temp;
                        }
                        else
                        {
                              flag=1;
                              temp=arr[lb];
                              arr[lb]=arr[j];
                              arr[j]=temp;
                        }
            }
            else
            {
                  return;
            }
      }
      quick(lb,j-1);
      quick(j+1,ub);
}
void main()
{
      int lb=0;
      int ub=size-1;
      create();
      display();
      quick(lb,ub);
      display();
}

 
* Special Thanks to Maulin &  Naresh & Also  Using Reference Book Data Structures by Tremblay


----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com

Tuesday, May 10, 2011

quick sort

/*
Taking help from the jean paul trembley book
Authore:- Maulin Desai
GNU/GPL :) :) */

#include
int main()
{
int m[]={42,23,74,11,65,58,94,36,99,87};
int lb=0;
int ub=9,i;
void q_sort(int m[],int lb,int ub);
q_sort(m,lb,ub);
for(i=0;i<10;i++)
{
printf("%d\t\t",m[i]);
}
return 0;
}
void q_sort(int m[],int lb,int ub)
{
int i,j,key,temp;
int flag=0;
i=lb+1;
j=ub;
key=m[lb];
while(flag==0)
{
if(lb {
while(m[i] i=i+1;

while(m[j]>key)
j=j-1;
if(i {
temp=m[i];
m[i]=m[j];
m[j]=temp;
}
else
{
flag=1;
temp=m[lb];
m[lb]=m[j];
m[j]=temp;
}
}
else
{
return;
}
}
q_sort(m,lb,j-1);
q_sort(m,j+1,ub);
}