Thursday, 27 September 2012

BINARY SEARCH


#include
int binsearch(int a[],int ,int,int);
int main()
{
    int n,i,x;
    scanf("%d",&n);
    scanf("%d",&x);
    int a[n];
    for(i=0;i
    {
        scanf("%d",&a[i]);
    }
    int lb=0,ub=n-1;
    int pos=binsearch(a,lb,ub,x);
    if(pos==-1)
    printf("unsuccessful\n");
    else
    printf("%d\n",pos+1);
    return 0;
}
int binsearch(int a[],int lb,int ub,int x)
{
    int mid;

    while(lb<=ub)
    {
        mid=(lb+ub)/2;
        if(a[mid]==x)
        return mid;
        else if(a[mid]>x)
        ub=mid-1;
        else
        lb=mid+1;
    }
    return -1;
}


DELETION IN LINK LIST

#include
#include
int main()
{
    struct node{
        int data;
        struct node *link;
    }*head;
    struct node *temp,*temp1;
    head=NULL;
    int num,i,item;
    scanf("%d",&num);
    for(i=0;i
    {
        scanf("%d",&item);
        if(head==NULL)
        {
            head=malloc(sizeof(struct node));
            head->data=item;
            temp=head;
        }
        else
        {
            temp->link=malloc(sizeof(struct node));
            temp->link->data=item;
            temp=temp->link;
        }
    }
    temp->link=NULL;
    temp=head;
    struct node *prev,*temp2;
    while(temp->link!=NULL)
    {
        prev=temp;
        temp=temp->link;

    }
    temp2=temp->link; //without these two lines
    free(temp2);  // deletion at end can be done
    prev->link=NULL;
    printf("after deletion at end\n");
    temp=head;
    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp=temp->link;
    }
    return 0;
}



deletion at any node -----



#include
#include
int main()
{
    struct node{
        int data;
        struct node *link;
    }*head;
    struct node *temp,*temp1;
    head=NULL;
    int num,i,item;
    scanf("%d",&num);
    for(i=0;i
    {
        scanf("%d",&item);
        if(head==NULL)
        {
            head=malloc(sizeof(struct node));
            head->data=item;
            temp=head;
        }
        else
        {
            temp->link=malloc(sizeof(struct node));
            temp->link->data=item;
            temp=temp->link;
        }
    }
    temp->link=NULL;
    temp=head;
    struct node *prev;
    int pos;
    printf("enter position from where is to be deleted");
    scanf("%d",&pos);
    for(i=0;i
    {
        temp1=temp;
        temp=temp->link;
    }
    temp1->link=temp->link;
    free(temp);
    temp=head;
    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp=temp->link;
    }
    return 0;
}


Tuesday, 25 September 2012

STRING MULTIPLICATION

#include
#include
int main()
{
    int k1,k2,k=0,i,j,temp,temp1,temp2,m=0,x;
    char str1[10000];
    char str2[10000];
    char strmult[100000];
    static int arr[100000];
    scanf("%s",str1);
    scanf("%s",str2);
    k1=strlen(str1);
    k2=strlen(str2);
    temp2=0;
    for(i=k2-1;i>=0;i--)
    {
        k=m;
        temp2=0;
        for(j=k1-1;j>=0;j--)
        {
            temp=(str1[j]-'0')*(str2[i]-'0')+temp2+arr[k];
            temp1=(temp%10)%10%10;
            arr[k]=((temp1))%10%10;
           // strmult[k]=(temp1+'0')+strmult[k];
           strmult[k]=arr[k]+'0';
            k=k+1;
            temp2=temp/10;
            if(j==0&&temp2!=0)
            {
                while(temp2>0)
                {
                    x=temp2%10;
                    arr[k]=x+arr[k];
                    int temp4=arr[k]/10;
                    strmult[k]=(arr[k]%10)+'0';
                    temp2+=temp4;
                    temp2=temp2/10;
                    k=k+1;
                }
            }
        }
        m=m+1;
    }
    printf("\n");
    int k3=strlen(strmult);
    for(i=k3-1;i>=0;i--)
    printf("%c",strmult[i]);
    return 0;
}



THIS PROGRAM CAN BE USE TO FIND MULTIPLICATION UPTO LARGE NUMBER TRY IT NOW AND TRY TO SOLVE IT URSELF!! :) 

Sunday, 23 September 2012

CREATION OF LINK LIST AND INSERTION IN THE LINK LIST AT ANY NODE


CREATION OF LINK LIST AND INSERTION IN THE LINK LIST AT ANY NODE


#include
#include
int main()
{
    struct node{
        int data;
        struct node *next;
    }*head;
    struct node *temp,*temp1;
    head=NULL;
    int num,i,item;
    scanf("%d",&num);
    for(i=0;i    {
        scanf("%d",&item);
        if(head==NULL)
        {
            head=malloc(sizeof(struct node));
            head->data=item;
            temp=head;
        }
        else
        {
            temp->next=malloc(sizeof(struct node));
            temp->next->data=item;
            temp=temp->next;
        }
    }

    temp->next=NULL;
    temp=head;
    int num1,p;
    printf("ener a number=");
    scanf("%d",&num1);
    printf("position\n");
    scanf("%d",&p);
    struct node *head1,*temp2;
    head1=malloc(sizeof(struct node));
    head1->data=num1;
    p=p-1;
    for(i=0;i    {
        if(i==p)
        {
            //store previous link in another pointer//
            temp2->next=temp->next;
            temp->next=head1;
            head1->next=temp2->next;
        }
        else
        {
            temp=temp->next;
        }
    }
    temp=head;
    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp=temp->next;
    }
    return 0;
}


Thursday, 6 September 2012

STRING COPY WITH POINTERS


#include
void strcpy(char*,char*);
int main()
{
    char t[10];
    scanf("%s",t);
    char s[10];
    strcpy(s,t);
    printf("%s",s);
}
void strcpy(char* s,char* t)
{
    int i;
    i=0;
    while((s[i]=t[i])!='\0')
    i++;
}



2ND METHOD---

void strcpy(char* s,char* t)
{
      while((*s=*t)!='\0')
      {
      s++;
      t++;
      }
}

3RD METHOD----

void strcpy(char* s,char* t)
{
     while((*s++=*t++)!='\0')
     ;
}

STRING LENGTH USING POINTER


#include
int strlen(char*);
int main()
{
    char str[1000];
    scanf("%s",str);
    int k=strlen(str);
    printf("%d",k);
}
int strlen(char* s)
{
    int n;
    for(n=0;*s!='\0';s++)
        n++;
    return n;
}