shiva (ds)

Upload: pavan-kumar

Post on 08-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 SHIVA (DS)

    1/30

    1

    /*WRITE A C PROGRAM TO SEARCH FOR AN ELEMENT IN AN ARRAY

    USING BINARY SEARCH */

    #include#includevoid bin_search(int item,int a[],int n,int *pos){int low,high,mid;low=0;high=n-1;while(low

  • 8/7/2019 SHIVA (DS)

    2/30

    bin_search(item,a,n,&position);if(position==-1){

    printf("not found\n");}else{printf("item found at %dth the position",position);}getch();}

    /*OUTPUT

    enter the value of n

    5enter the array element

    234567

    enter the item to be searched5

    item found at 3th the position */

  • 8/7/2019 SHIVA (DS)

    3/30

    2/*WRITE A C PROGRAM TO SORT A LIST OF N ELEMENTS USING

    BUBBLE SORT */

    #include#includemain(){int a[20],i,j,temp,n;clrscr();printf("enter the size\n");scanf("%d",&n);

    printf("enter the array element\n");for(i=0;i

  • 8/7/2019 SHIVA (DS)

    4/30

    /*OUT PUT

    enter the size5enter the array element

    94538

    sorted array is345

    89 */

  • 8/7/2019 SHIVA (DS)

    5/30

    3

    /*WRITE A C PROGRAM TO SORT A LIST OF N ELEMENTS

    USING MERGE SORT */

    #include#include#includevoid marge_sort(int a[],int low,int high);void simple_marge(int a[],int low,int mid,int high);

    void main(){

    int i,n,a[20];clrscr();printf("enter the limit\n");scanf("%d",&n);printf("enter the array element\n");for(i=0;i

  • 8/7/2019 SHIVA (DS)

    6/30

    while(i

  • 8/7/2019 SHIVA (DS)

    7/30

    /*OUT PUTenter the limit

    7enter the array element

    20405506028

    sorted array as fallow

    2

    5820405060 */

  • 8/7/2019 SHIVA (DS)

    8/30

    4

    /*WRITE A C PROGRAM TO FIND THE BINOMIAL CO-EFFICENT

    USING RECURSION */

    #include#includeint bio_cof(int k,int m){if(m==0||m==k)return(1);return bio_cof(k-1,m-1)+bio_cof(k-1,m);}

    void main(){int k,m,ncr;clrscr();printf("enter the value\n");scanf("%d%d",&k,&m);ncr=bio_cof(k,m);printf("bio_cof(%d,%d)=%d\n",k,m,ncr);getch();}

    /*OUT PUTenter the value

    32

    bio_cof(3,2)=3 */

  • 8/7/2019 SHIVA (DS)

    9/30

    5

    /*WRITE A C PROGRAM THE TOWER HONNAI OF PROBLEM

    USING RECURSION */

    #include#includevoid tower(int n,int sourse,int temp,int dest){if(n==1){printf("\n move disc %d form %c to %c\n",n,sourse,dest);

    return;}tower(n-1,sourse,dest,temp);printf("\n move disc %d form %c to %c\n",n,sourse,dest);tower(n-1,temp,sourse,dest);}

    void main(){int n;

    clrscr();printf("enter the value of n\n\n");scanf("%d",&n);tower(n,'A','B','C');getch();}

  • 8/7/2019 SHIVA (DS)

    10/30

    /*OUT PUT

    enter the value of n3

    move disc 1 form A to C

    move disc 2 form A to B

    move disc 1 form C to B

    move disc 3 form A to C

    move disc 1 form B to A

    move disc 2 form B to C

    move disc 1 form A to C */

  • 8/7/2019 SHIVA (DS)

    11/30

    6

    /*WRITE A C PROGRAM TO SORT TA LIST OF N ELEMENT

    USING QUICK SORTS */

    #include#includevoid quicksort(int a[],int low,int high);int partition(int a[],int low,int high);

    main(){int i,n,a[70];clrscr();printf("enter the limit\n");scanf("%d",&n);printf("enter the array element\n");for(i=0;i

  • 8/7/2019 SHIVA (DS)

    12/30

    {int i,j,temp,key;key=a[low],i=low+1,j=high;

    while(1){while(key>=a[i])i++;while(key

  • 8/7/2019 SHIVA (DS)

    13/30

    7

    /*

    WRITE A C PROGRAM TO SEARCH AN ELEMENT USING

    SEQUENTIAL SEARCH*/

    #include#includemain(){int i,n,a[10],item;clrscr();printf("enter the value\n ");scanf("%d",&n);printf("enter the array element\n");for(i=0;i

  • 8/7/2019 SHIVA (DS)

    14/30

    /*OUT PUTenter the value

    5enter the array element

    702030510

    enter the item10

    element 10 found */

  • 8/7/2019 SHIVA (DS)

    15/30

    8/*WRITE A C PROGRAM TO STIMULATE THE WORKINK OF AN

    STACK USING ARRAY */

    #include#include#include#define stacksize 5int p;int semp(int top){return(top==-1)?1:0;}int sfull(int top){return(top==stacksize-1)?1:0;}void push(int s[],int *top,int item){if(sfull(*top)){printf("stack full\n");return;}s[++(*top)]=item;

    }

    int pop(int s[],int *top){int itemdel;if(semp(*top)){printf("under flow\n");return 0;}return s[(*top)--];

    }void display(int s[25],int top){int i;if(semp(top)){

  • 8/7/2019 SHIVA (DS)

    16/30

    printf("stack empty");}else

    {printf("element on stack are\n");

    for(i=0;i

  • 8/7/2019 SHIVA (DS)

    17/30

    }while(p);

    getch();}

    /*OUT PUT***program to perform stack operations***

    1:push2:pop

    3:display4:exitenter the choice3***stack empty***

    do you want to continue (1/0?)11:push2:pop3:display4:exit

    enter the choice1enter the item10

    do you want to continue (1/0?)11:push2:pop3:display4:exitenter the choice

    1enter the item20

    do you want to continue (1/0?)11:push

  • 8/7/2019 SHIVA (DS)

    18/30

    2:pop3:display4:exit

    enter the choice1enter the item30

    do you want to continue (1/0?)11:push2:pop3:display4:exitenter the choice

    3element on stack are

    10 20 30

    do you want to continue (1/0?)11:push2:pop3:display4:exitenter the choice

    230

    do you want to continue (1/0?)11:push2:pop3:display4:exitenter the choice220

    do you want to continue (1/0?)11:push2:pop3:display4:exit

  • 8/7/2019 SHIVA (DS)

    19/30

    enter the choice210

    do you want to continue (1/0?)11:push2:pop3:display4:exitenter the choice2***under flow***

    do you want to continue (1/0?)1

    1:push2:pop3:display4:exitenter the choice4 */

  • 8/7/2019 SHIVA (DS)

    20/30

    9/*WRITE A C PROGRAM TO STIMULATE THE WORKINK OF AN

    ODINARY QUEUE USING ARRAY */

    #include#include#include#define queuesize 5int f,r,i,item,q[10];void insertrear(){ if(r==queuesize-1)

    {printf("**queue over flow**\n");

    return;}

    else{printf(" enter the item to be inserted\n");scanf("%d",&item);r=r+1;q[r]=item;

    } return;}

    voiddelete()

    {if(f>r){printf("**under flow**\n");

    return;}else{item=q[f];printf(" the element deleted is %d\n",item);f++;

    }}

    void display(){if(f>r)

  • 8/7/2019 SHIVA (DS)

    21/30

    {printf("***queue is empty***\n");

    return;

    }printf("content of the queue\n");for(i=f;i

  • 8/7/2019 SHIVA (DS)

    22/30

    /*OUTPUT ***program to perform stack operations***

    1:insert2:delete3: display4:exitenter the choice3***queue is empty***1:insert2:delete3: display4:exitenter the choice

    1enter the item to be inserted101:insert2:delete3: display4:exitenter the choice1enter the item to be inserted20

    1:insert2:delete3: display4:exitenter the choice1enter the item to be inserted301:insert2:delete3: display

    4:exitenter the choice3content of the queue1020

  • 8/7/2019 SHIVA (DS)

    23/30

    301:insert2:delete

    3: display4:exitenter the choice2the element deleted is 101:insert2:delete3: display4:exitenter the choice2

    the element deleted is 201:insert2:delete3: display4:exitenter the choice2the element deleted is 301:insert2:delete3: display

    4:exitenter the choice2**under flow**1:insert2:delete3: display4:exitenter the choice4 */

  • 8/7/2019 SHIVA (DS)

    24/30

    10/*WRITE A C PROGRAM TO STIMULATE THE WORKINK OF AN

    CIRCULAR QUEUE USING ARRAY */

    #include#include#include#define qsize 9int i,f=0,r=-1,count=0;int ch,q[10],item,pos;void insertrear(){if(count==qsize){printf("queue is full\n");

    return;}r=(r+1)%qsize;q[r]=item;count++;}void display(){int i;

    if(count==0){printf("\t**queue is empty**\n\n");

    return;}printf("\t**queue contain**\n");for(i=f;i

  • 8/7/2019 SHIVA (DS)

    25/30

    return;}printf("\t**deleted item %d from the position

    %d**\n",q[f],f);f=(f+1)%qsize;count--;}main(){clrscr();printf("program to perform stack operations\n");

    for(;;){

    printf(" 1:insert\n 2:delete\n"); printf(" 3:display\n 4:exit\n");printf("enter the choice\n");scanf("%d",&ch);

    switch (ch){

    case 1:printf(" enter the element to be inserted);scanf("%d",&item);insertrear();

    If(count

  • 8/7/2019 SHIVA (DS)

    26/30

    /*OUT PUT***program to perform stack operations***

    1:insert2:delete3:display4:exitenter the choice3

    **queue is empty**

    1:insert2:delete3:display

    4:exitenter the choice1enter the element to be inserted10

    item inserted 10 at the position 0**queue contain****item 10 is in the position 0**

    1:insert2:delete3:display

    4:exitenter the choice1enter the element to be inserted20

    item inserted 20 at the position 1**queue contain****item 10 is in the position 0****item 20 is in the position 1**

    1:insert2:delete3:display4:exitenter the choice1

  • 8/7/2019 SHIVA (DS)

    27/30

    enter the element to be inserted30

    item inserted 30 at the position 2

    **queue contain****item 10 is in the position 0****item 20 is in the position 1****item 30 is in the position 2**

    1:insert2:delete3:display4:exitenter the choice2

    **deleted item 10 from the position 0**

    **queue contain****item 20 is in the position 1****item 30 is in the position 2**

    1:insert2:delete3:display4:exitenter the choice2

    **deleted item 20 from the position 1****queue contain**

    **item 30 is in the position 2**1:insert2:delete3:display4:exitenter the choice3

    **queue contain****item 30 is in the position 2**

    1:insert2:delete3:display4:exitenter the choice4 */

  • 8/7/2019 SHIVA (DS)

    28/30

    10/*WRITE A C PROGRAM TO CONVERT AND PRINT A GIVEN

    VALIDE FULLY PARENTHESIZED INFIX EXPRESSION TO

    POSTFIX EXPRESSION */

    #include#include#include#define stack_size 20char F(char symbol){switch(symbol){case'+':case'_':return 2;case'*':case'/':return 4;case'^':case'$':return 5;case'(':return 0;case'#':return -1;default:return 8;}}char G(char symbol){

    switch(symbol){case'+':case'-':return 1;case'*':case'/':return 3;case'^':case'$':return 6;case'(':return 9;case')':return 0;default:return 7;

    }}void infix_postfix(char infix[],char postfix[]){int top,j,i;

  • 8/7/2019 SHIVA (DS)

    29/30

    char s[30],symbol;top=-1;

    push('#',&top,s);j=0;for(i=0;iG(symbol)){postfix[j++]=s[top--];}if(F(s[top])!=G(symbol))push(symbol,&top,s);

    elsepop(&top,s);}while(s[top]!='#'){postfix[j++]=pop(&top,s);}postfix[j]='\0';}void main(){

    char infix[20];char postfix[20];clrscr();printf("enter valid infix expression\n");scanf("%s",infix);

    infix_postfix(infix,postfix);printf("postfix expression is %s\n",postfix);getch();}int push(char symbol,int *top,char s[]){if(*top==stack_size-1){printf("stack is overflow\n");}s[++(*top)]=symbol;

  • 8/7/2019 SHIVA (DS)

    30/30

    return;}int pop(int *top,char s[])

    {char item_deleted;item_deleted=s[(*top)--];}

    /*OUTPUTenter valid infix expression(A+(B-C)*D)postfix expression is AB-CD*+ */