Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Thursday, April 3, 2014

C code to convert INFIX expression into POSTFIX expression

//C program to convert given INFIX expression into POSTFIX expression using stack//

#include<stdio.h>
#include<conio.h> 
#include<string.h>
#define MAX 20

char stack[MAX];
int top=-1;
char pop();
void push(char item);

int prcd(char symbol)
{
switch(symbol)
{
case +:
case -:return 2;
break;
case *:
case /:return 4;
break;
case ^:
case $:return 6;
break;
case (:
case ):
case #:return 1; 
break;
}
}
 
int isoperator(char symbol)
{
switch(symbol) 
{
case +:
case -:
case *:
case /:
case ^:
case $:
case (:
case ):return 1;
break;
default:return 0;
}
}

 
void convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
stack[++top]=#;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else{ 
if(symbol==()
push(symbol); 
else
if(symbol==))
{
while(stack[top]!=()
{ 
postfix[j]=pop();
j++; 
}
pop();//pop out 
}

else{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);

}//end of else.

}//end of else.

}//end of else.

}//end of for.

while(stack[top]!=#)
{
postfix[j]=pop();
j++;
}
postfix[j]=
Read More..

Sunday, March 30, 2014

C code to implement BFS and DFS

/* C program to implement BFS(breadth-first search) and DFS(depth-first search) algorithm */

#include<stdio.h>

int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();

void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS
");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("
");
}

do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("
MENU");
printf("
1.B.F.S");
printf("
2.D.F.S");
printf("
ENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);

switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c==y)||(c==Y));
}


//**************BFS(breadth-first search) code**************//
void bfs(int s,int n)
{
int p,i;
add(s);
vis[s]=1;
p=delete();
if(p!=0)
printf(" %d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
p=delete();
if(p!=0)
printf(" %d ",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}


void add(int item)
{
if(rear==19)
printf("QUEUE FULL");
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}


//***************DFS(depth-first search) code******************//
void dfs(int s,int n)
{
int i,k;
push(s);
vis[s]=1;
k=pop();
if(k!=0)
printf(" %d ",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=0)
printf(" %d ",k);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i,n);
}
void push(int item)
{
if(top==19)
printf("Stack overflow ");
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}
}

/* Output of BFS(breadth-first search) and DFS(depth-first search) program */

C code to implement BFS and DFS
Output of BFS and DFS Program

C code to implement BFS and DFS
Output of BFS and DFS Program

For more related to Data Structure see List of Data Structure Programs. If you like this program, Please share and comment to improve this blog. Thanks.. :)
Read More..