Thursday, 19 September 2013

Program for converting infix string to postfix using Stack implementation

Aim: Infix to Postfix
Steps:
1. Input the infix string
2. The program use the stack implementation to convert this infix to postfix
3. There are 5 function are used
     Pop() is used to pop the elements
     Push() is used to push the element in the stack
     createstack() is used to create head node
     check() is used to test whether the element has to push or print
     priority() is used to check the priority of the operators


output







Program


#include<stdio.h>
int priority(char a);
int check(char a);
typedef struct node
{
        void *data;
        struct node *link;
}stacknode;
typedef struct
{
        int count;
        stacknode *top;
}stack;
stack *createstack()
{
      stack *sp;
      sp=(stack*)malloc(sizeof(stack));
      sp->top=NULL;
      sp->count=0;
}
void push(stack *sp,void *data)
{
     stacknode *node;
     node=(stacknode*)malloc(sizeof(stacknode));
     node->data=data;
     node->link=sp->top;
     sp->top=node;
     sp->count++;
}
void *pop(stack *sp)
{
     stacknode *temp;
     void *data;
     data=sp->top->data;
     temp=sp->top;
     sp->top=sp->top->link;
     free(temp);
     sp->count--;
     return(data);
}
void *top(stack *sp)
{
       return(sp->top->data);    
}
int main()
{
    stack *sp;
    char s[100];
    int i;
    int j;
    char *a;
    sp=createstack();
    printf("\nenter the string\n");
    scanf("%s",s);
    for(i=0;i<=strlen(s);i++)
    {
       if(check(s[i]))
       {
          printf("%c",s[i]);
       }
       else if(s[i]==')')
       {
            while(*(char*)top(sp)!='(')
            {
                a=(char*)pop(sp);
                printf("%c",*a);
                free(a);
            }
            a=(char*)pop(sp);
            free(a);
       }
       else
       {
           if(s[i]=='(')
           {
               a=(char*)malloc(sizeof(char*));
               *a=s[i];
               push(sp,a);
           }
           else
           {
               if(s[i]=='('||priority(*(char*)top(sp))<priority(s[i]))
               {
                  a=(char*)malloc(sizeof(char*));
                  *a=s[i];
                  push(sp,a);
               }
               else
               {
                         a=(char*)pop(sp);
                         printf("%c",*a);
                         free(a);
                         while(priority(*(char*)top(sp))>=priority(s[i])&&*(char*)pop(sp)!='(')
                         {
                            a=(char*)pop(sp);
                            printf("%c",*a);
                            free(a);   
                         }
                         if(!check(s[i]))
                         {
                           a=(char*)malloc(sizeof(char*));
                           *a=s[i];
                           push(sp,a);
                         }          
               }
           }
       }
    }            
    free(s);
    free(sp);
    getch();
    return(0);
}
int priority(char a)
{
    switch(a)
    {
        case '(': return(-1);
        break;     
        case '/': return(1);
        break;
        case '*': return(1);
        break;
        case '-': return(0);
        break;
        case '+': return(0);
    }
    return(0);
}
int check(char a)
{
    if(a=='*'||a=='/'||a=='-'||a=='+'||a=='('||a==')')
    {
        return(0);
    }
    else    
    {
       return(1);
    }
}            








Tuesday, 17 September 2013

Program For Eight Queen Problem

Output



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

// Declaration of Structure
typedef struct p
{
int row;
int col;
}position;

typedef struct node
{
position *p;
struct node *link;
}structnode;
typedef struct
{
int count;
structnode *link;
}stack;

//  Declaration of Function
int getsize();
int guarded(int board[9][9],int row,int col,int boardsize);
void *createstack();
void fillboard(stack *st,int boardsize);
void push(stack *st,position *p);
position pop(stack *st);
void printboard(stack *st,int boardsize);

// Main function
int main()
{
position p;
int boardsize;
stack *st;
boardsize=getsize();
st=(stack*)createstack();
fillboard(st,boardsize);
printboard(st,boardsize);
printf("\nwe hope you enjoyed queens\n");
getch();
}

// Return valid Size of the board
int getsize()
{
int boardsize;
printf("welcome toeight queens.you may select\n"
"a board size from 4*4 to 8*8.i will\n"
"then position a queen in each row of the\n"
"board so no queen may capture another\n"
"queen.note:there is no solution for board\n"
"less than 4*4 and more than 8*8\n");
printf("\nenter the size of board\n");
scanf("%d",&boardsize);
while(boardsize<4||boardsize>8)
{
printf("board size is greater than 3 and\n"
       "lesst han 9.you entered %d.\n"
       "please re-enter.thankyou.\a\a\n\n"
       "your board size:",boardsize);
scanf("%d",&boardsize);
}
return(boardsize);
}

// Create Stack header
void *createstack()
{
stack *st;
st=(stack*)malloc(sizeof(st));
st->count=0;
st->link=NULL;
return(st);
}

// Correct arrangements for queens on board
void fillboard(stack *st,int boardsize)
{
int row;
int col;
int board[9][9]={0};
position *p;
position s;
row=1;
col=0;
while(row<=boardsize)
{
while(col<=boardsize&&row<=boardsize)
{
col++;
if(guarded(board,row,col,boardsize)==0)
{
board[row][col]=1;
p=(position*)malloc(sizeof(position));
p->row=row;
p->col=col;
push(st,p);
row++;
col=0;
}
while(col>=boardsize)
{
s=pop(st);
row=s.row;
col=s.col;
board[row][col]=0;
}
}
}
}

// Stack push
void push(stack *st,position *p)
{
structnode *node;
(st->count)++;
node=(structnode*)malloc(sizeof(structnode));
node->p=p;
printf("\npush %d %d",node->p->row,node->p->col);
node->link=st->link;
st->link=node;
}
position pop(stack *st)
{
void *temp;
position s;
s.row=st->link->p->row;
s.col=st->link->p->col;
printf("\npop %d %d",s.row,s.col);
free(st->link->p);
temp=st->link;
st->link=st->link->link;
free(temp);
(st->count)--;
return(s);
}

// checking queen position
int guarded(int board[][9],int chkrow,int chkcol,int boardsize)
{
int row;
int col;
col=chkcol;
for(row=1;row<=chkrow;row++)
{
if(board[row][col]==1)
{
return(1);
}
}
for(row=chkrow-1,col=chkcol+1;row>0&&col<=boardsize;row--,col++)
{
if(board[row][col]==1)
{
return(1);
}
}
for(row=chkrow-1,col=chkcol-1;row>0&&col>0;row--,col--)
{
if(board[row][col]==1)
{
return(1);
}
}
return(0);
}

// Board
void printboard(stack *st,int boardsize)
{
int col;
position s;
stack *sp;
position *p;
sp=(stack*)createstack();
while(st->count!=0)
{
s=pop(st);
p=(position*)malloc(sizeof(position));
p->row=s.row;
p->col=s.col;
push(sp,p);
}
free(st);
if(sp->count==0)
{
printf("\nthere are no position on this board\n");
return;
}
printf("\nplace queens in following positions\n");
while(sp->count!=0)
{
s=pop(sp);
printf("row %d-col %d:\t|",
 s.row,s.col);
for(col=1;col<=boardsize;col++)
{
if(s.col==col)
{
printf(" Q|");
}
else
{
printf("  |");
}
}
printf("\n");
}
free(sp);


}