Thursday, December 14, 2017

Infix to Posfix, Infix to Prefix Expression Conversion Data Structures

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

#define SIZE 100


char stack[SIZE],top=-1;

void push(char item)
{
if(top>=SIZE-1)
printf("\n stack Overflow");
else
{

top=top+1;
stack[top]=item;
}

}

char pop()
{
char x;
if(top<0)
{
printf("\n stack underflow");
getchar();
}
x=stack[top];

top=top-1;
return(x);
}

int precedence(char symbol)
{
if(symbol=='^') return 3;
else if(symbol=='*' || symbol=='/') return 2;
else if(symbol=='+' || symbol=='-') return 1;
else return 0;
}

int isOperator(char item)
{
if(item=='+' || item =='-' || item=='*' || item=='/' || item=='^') return 1; 
else return 0;
}

void infixToPostfix(char infix[], char postfix[])

{
int i,j;
char item,x;

push('(');
strcat(infix,")");
i=0,j=0;
item=infix[i];
while(item!='\0')
{
if(item=='(')
{
push(item);
}
else if(isdigit(item) || isalpha(item))
{
postfix[j]=item;
j++;
}
else if(isOperator(item)==1)
{
x=pop();
while(isOperator(x)==1 && precedence(x)>=precedence(item))
{
postfix[j]=x;
j++;
x=pop();

}
push(x);
push(item);


}
else if(item==')')
{
x=pop();
while(x!='(')
{
postfix[j]=x;
j++;
x=pop();
}
}
else
{
printf("\n Invalid expression");
return;
}
i++;
item=infix[i];


}//end while
if(top>0)
{

printf("\n Invalid expression");
return;
}
postfix='\0';
}
int main()
{
int choice;
char item,i=0;
char infix[SIZE],postfix[SIZE],prefix[SIZE];
printf("\n Enter Infix Expression:");
gets(infix);
printf("press 1->Infix to postfix evaluation \n 2->Infix to prefix Evaluation");
scanf("%d",&choice);
switch(choice)
{
case 1:
infixToPostfix(infix,postfix);
printf("\n Postfix Expression");
puts(postfix);
break;

case 2:
/*reverse the Infix and replace '(' by ')' then find as postfix then  once again reverse it found prefix*/
strrev(infix);


i=0;
item=infix[i];

while(infix[i]!='\0')
{

if(infix[i]=='(')
  infix[i]=')';
  else if(infix[i]==')')
  infix[i]='(';
 
i++;

}

infixToPostfix(infix,postfix); 

printf("\n Prefix Expression for given Infix:");
puts(strrev(postfix));
break;

default:
printf("\n Wrong choice");
}

return 0;
}

Wednesday, December 6, 2017

C++ program to swap two numbers using macros

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

#define SWAP(a,b) {int temp; temp=a; a=b; b=temp;}

void main()
{
clrscr();
int x,y;
cout<<“Enter two numbers:”;
cin>>x>>y;

cout<<“x=”<<x<<” y=”<<y;
SWAP(x,y);
cout<<“nx=”<<x<<” y=”<<y;
getch();
}

Monday, November 13, 2017

Deleting every Nth node from a linked lint - Implementaion Using C - Interview Question

                             Scenario

Consider  a game in which players are arranged circularly. The

game has to be played in a way demonstrared as follows.

1.) The games starts from first player as current player.

2.)A random number(n) from 1 to 10 is generated.

3.)  *The count goes from the current player and deletes the             
         Nth player
      *After deleting the player, now the game starts from the player
         next to the  deleted player
      *The game is to be stopped if there is only one player and the 
        player is  declared as a winner
      (Design an algorithm for the game)

 #include<stdio.h>
#include<stdlib.h>
typedef struct Node
 {
    int data;
    struct Node *prev;
    struct Node *next;
 }Node;
Node *start=NULL;

void insert(Node *temp)
{
    Node *var,*last;
    Node *ptr=start;
  
    if(start==NULL)
    {
        temp->next=temp;
        temp->prev=temp;
        start=temp;
    }
    else
    {
      while(ptr->next!=start)
        {  
            ptr=ptr->next;
        }
         last=start->prev;
         ptr->next=temp;
         temp->prev=last;
         last->next=temp;
         temp->next=start;
         start->prev=temp;
    }
}

void display(Node *start)
{
  
    Node *ptr=start;
    if(start==NULL)
    printf("\n No elements present");
    else
    {
         printf("\n Elements in the Linked List are:");
        while(ptr->next!=start)
        {  
            printf("\n%d",ptr->data);      
            ptr=ptr->next;
        }
            printf("\n%d",ptr->data);  
    }
return;
}

int delete1(Node *start,int n)
{
    Node *t=start,*p,*q;
    int count;
    while(t->next!=t->next->next)  
    {       
        count=1;
        while(count<n)
        {  
            t=t->next;
            count++;
        }
        p=t->prev; q=t->next;
        p->next=q; q->prev=p;  
        free(t);
      
        t=q;  
    }
    return t->data;
}

Node *createNode(int data)
{
    Node *temp=(struct Node*)malloc(sizeof(struct Node));
    temp->data=data;
    return temp;  
}
int main()
{
    int data;
    Node *temp;

    temp=createNode(10);
    insert(temp);
      
    temp=createNode(20);
    insert(temp);  
  
    temp=createNode(30);
    insert(temp);  
  
    temp=createNode(40);
    insert(temp);  
  
    temp=createNode(50);
    insert(temp);  
  
    display(start);
  
    data=delete1(start,4);     /*Winner */
    printf("\n Winner is :: %d",data);  
    return 0;
}

 

 

O/P:  when N=4


 

 

 

Saturday, November 4, 2017

FLAMES - Finding realtionship between You both Using C

//flames using C
#include<stdio.h>
int main()
{
char name1[25],name2[25];
register int i,j;
static int count_fname,count_lname;
int sum=0;
char temp;
printf("\n Enter your name::");
scanf("%s",name1);
printf("\nEnter Miss/Mr 'X' Name::");
scanf("%s",name2);
i=0,j=0;

while(name1[i]!='\0')  /*Sorting the first name*/
{
j=i+1;
while(name1[j]!='\0')
{
if(name1[i]>name1[j])
{
temp=name1[i];
name1[i]=name1[j];
name1[j]=temp;
}

j++;
   }
 
i++;
count_fname=i;
}
printf("\n%s",name1);


i=0,j=0;
while(name2[i]!='\0')          /*Sorting the second name*/
{
j=i+1;
while(name2[j]!='\0')
{
if(name2[i]>name2[j])
{
temp=name2[i];
name2[i]=name2[j];
name2[j]=temp;
}

j++;
   }
 
i++;
count_lname=i;
}

printf("\n%s",name2);



i=0,j=0;
while(name1[i]!='\0' && name2[j]!='\0')     /*Compare to get perfect count*/
{
if(name1[i]<name2[j])
{
i++;
}
else if(name1[i]>name2[j])
{
j++;
}
else if(name1[i]==name2[j])
{

count_fname=count_fname-1;
count_lname=count_lname-1;


i++;
j++;
}
}
printf("\n Value of count_fname:%d",count_fname);
printf("\n Value of count_fname:%d",count_lname);
sum=(count_fname+count_lname);  //sum up remaining letters in two names*/
//sum=sum%6;
switch(sum)
{
case 0:
printf("\n Sorry Same Name found!!! else alike palindrome or anagram");
break;
case 1:
printf("\n -->Sister-->");
break;

case 2:
printf("\n -->Enemy-->");
break;
case 3:
printf("\n -->Friends-->");
break;

case 4:
printf("\n -->Enemy-->");
break;

case 5:
printf("\n -->Friends-->");
break;

case 6:
printf("\n -->You Gonna MARRY each other-->");
break;

case 7:
printf("\n -->Enemy-->");
break;

case 8:
printf("\n -->Affection-->");
break;

case 9:
printf("\n -->Enemy-->");
break;

case 10:
printf("\n Love");

case 11:
printf("\n  You Gonna MARRY each other");
break;

case 12:
printf("\n Affection");
break;

case 13:
printf("\n Affection");
break;

case 14:
printf("\n Friends");
break;
case 15:
printf("\n You gonna marry each other");
break;

case 16:
printf("\n Friends");
break;



}


return 0;
}




Thursday, November 2, 2017

Writing an Array of structures into a file & Reading a file into an Array of structures in c

                      Student Mark List Preparation

#include<stdio.h>

FILE *fp;
struct stud
{
    int sid;
    int sm[5];
    int tot;
    float avg;
    int cls;
    char pf;
};
void cal(struct stud s[],int);
void print(struct stud s[],int);
int main()
{

    struct stud s[10];
    int i,j,n;

    printf("Enter the no.of students:");
    scanf("%d",&n);

       //s=(struct stud*)malloc(sizeof(struct stud)*n);
    for(i=0;i<n;i++)
    {
        printf("Enter the student id:");
        scanf("%d",&s[i].sid);
        printf("enter 5 marks:");
        for(j=0;j<5;j++)
        scanf("%d",&s[i].sm[j]);
    }
        cal(s,n);
return 0;
}
void cal(struct stud s[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(s[i].sm[0]>50 && s[i].sm[1]>50 && s[i].sm[2]>50 && s[i].sm[3]>50 && s[i].sm[4]>50)
            s[i].pf='p';
        else
            s[i].pf='f';
        if(s[i].pf=='p')
        {
            s[i].tot=(s[i].sm[0]+s[i].sm[1]+s[i].sm[2]+s[i].sm[3]+s[i].sm[4]);
            s[i].avg=(s[i].tot)/5;
            if(s[i].avg>70)
                s[i].cls=1;
            else if(s[i].avg>60 && s[i].avg<70)
                s[i].cls=2;
            else //(s[i].avg>50 && s[i].avg<60)
                s[i].cls=3;


        }
        else
        {
            printf("FAIL");
        }
    }
            print(s,n);


   
    return;
}
void print(struct stud s[],int n)
{
    int i,j;
fp =fopen("marklist.txt","w+");

    for(i=0;i<n;i++)
    {
      
        fprintf(fp,"%d    ",s[i].sid);
        for(j=0;j<5;j++)
        {
        fprintf(fp,"%d    ",s[i].sm[j]);
        }
        fprintf(fp,"%d    ",s[i].tot);
        fprintf(fp,"%f    ",s[i].avg);
        fprintf(fp,"%d    ",s[i].cls);

       fprintf(fp,"\n");
    }
    fclose(fp);
   
   
   
fp=fopen("marklist.txt","r");
printf("SID   C   COA   DS   RDBMS    JAVA   TOTAL   AVG    CLASS \n");
for(i=1;i<=n;i++)
{
    j=0;
   
fscanf(fp,"%d  %d %d %d %d %d %d %f %d",&s[i].sid,&s[i].sm[j],&s[i].sm[j+1],&s[i].sm[j+2],&s[i].sm[j+3],&s[i].sm[j+4],&s[i].tot,&s[i].avg,&s[i].cls);

printf("%d %d %d %d %d %d %d %f %d\n",s[i].sid,s[i].sm[j],s[i].sm[j+1],s[i].sm[j+2],s[i].sm[j+3],s[i].sm[4],s[i].tot,s[i].avg,s[i].cls);

}
fclose(fp);
    return;
}

 


 

Friday, October 20, 2017

Hackerrank Data Strcture Arrays - Left rotation solution in Java

Solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int d = in.nextInt();
        int i,j,temp;
        int[] a = new int[n];
        for(int a_i = 0; a_i < n; a_i++){
            a[a_i] = in.nextInt();
        }
        
        for( i=0;i<d;i++)
        {
            temp=a[0];
            for(j=0;j<n-1;j++)
            {            
                a[j]=a[j+1];
            }
             a[j]=temp;
        }
      
        for( i=0;i<n;i++)
        {         
         System.out.print(a[i] + " ");          
        }
        in.close();
    }
}