Thursday, 9 February 2017

DATA STRUCTURE CODES-IV

PROGRAM 1:


PROGRAM TO IMPLEMENT POST-FIX OPERATION

#include<stdio.h>
#include<string.h>
char *strev(char *);

int 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;
}
}
//* Input prcedence function*//

int 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;                          
char s[30];                        
int j;                               //            
int i;                               //Index to access the INFIX expression
char symbol;                            
top=-1;                                    
s[++top]='#';                    // Initialize stack to #
j=0;                                
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];              // Scan the next symbol
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];          
j++;
}
if(F(s[top])!=G(symbol))
s[++top]=symbol;            // PUSH the input symbol
else
top--;          
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';       
}

void main()
{
char infix[20];
char postfix[20];
printf("Enter the valid expression\n");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("The Post expression is \n");
printf("%s\n",postfix);

}




PROGRAM 2:

PROGRAM TO IMPLEMENT PREFIX OPERATION

#include<stdio.h>
#include<string.h>
char *strev(char *);
              
             
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case ')':return 0;
case '#':return-1;
default:return 8;
}
}
//* Input prcedence function*//

int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case ')':return 9;
default:return 7;
}
}

void infix_prefix(char infix[],char prefix[])
{
int top;                          
char s[30];                      
int j;                             
int i;                              
char symbol;                            
top=-1;                                    
s[++top]='#';                   
j=0;                                 // Point to first char of prefix expression
strev(infix);                     // Reverse the infix expression
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];              // Scan the next symbol
while(F(s[top])>G(symbol))
{
prefix[j]=s[top--];            //POP from the stack and place into prefix
j++;
}
if(F(s[top])!=G(symbol))
s[++top]=symbol;           
else
top--;                              
}
while(s[top]!='#')
{
prefix[j++]=s[top--];
}
prefix[j]='\0';                            
strev(prefix);                            
}

void main()
{
char infix[20];
char prefix[20];
printf("Enter the valid expression\n");
scanf("%s",infix);
infix_prefix(infix,prefix);
printf("The Prefix expression is \n");
printf("%s\n",prefix);
}

char *strev(char *str)
{
char *p1, *p2;
if(!str||!str)
return str;
for(p1=str,p2=str+strlen(str)-1;p2>p1;++p1,--p2)
{
*p1^=*p2;
*p2^=*p1;
*p1^=*p2;
}
return str;
}




PROGRAM 3:

PROGRAM TO SORT THE GIVEN NUMBERS (QUICK SORT)

#include<stdio.h>             
int partition(int a[],int low,int high);    
void quicksort(int a[],int low,int high);

struct p       
{
 char n[100];    
 int  a[20];
}p;             

void main()
{
system("clear");
int i,n;               
printf("enter the number of matches played\n");   
scanf("%d",&n);       
printf("enter the name of the bowler \n");
scanf("%s",p.n);
printf("enter the average of the bowler per match \n");
for(i=0;i<n;i++){                 
scanf("%d",&p.a[i]);
}
quicksort(p.a,0,n-1);
printf("Name of the bowler is %s\n",p.n);
printf("the order of performance is\n");

for(i=0;i<n;i++)printf("%d\n",p.a[i]);
}
int  partition(int a[],int low,int high)   
{
    int i,j,temp,key;      
    key=a[low];            
    i=low+1;               
    j=high;                
    while(1)
    {
        while(i<high&&key>=p.a[i])i++;
        while(key<a[j])j--;
        if(i<j){
            temp=p.a[i];
            p.a[i]=a[j];
            a[j]=temp;
        }
        else{
            temp=a[low];
            a[low]=a[j];
            a[j]=temp;
            return j;
        }
    }
}

void quicksort(int a[],int low,int high)   
{
    int j;                                        
    if(high>low){
        j=partition(a,low,high);
        quicksort(a,low,j-1);
        quicksort(a,j+1,high);
    }
}






No comments:

Post a Comment

we are hear to discuss your queries ,so please feel free to ask any of your queries.

Wikipedia

Search results