google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.

Tuesday, April 7, 2015

C program for parenthesized expression using stack

/*PARENTHESIZED EXPRESSION USING STACK */

#include<stdio.h>
#include<conio.h>
#define MAX 10
int top=-1;
int stack[MAX];
void push(char);
char pop();
void main()
{
   char exp[MAX],ch;
   int i,flag=1;
   clrscr();
   printf("\n Enter Infix Expression:-\n");
   gets(exp);
   i=0;
   while(exp[i]!='\0')
   {
     if(exp[i]=='('||exp[i]=='{'||exp[i]=='[')
     {
push(exp[i]);
     }
     if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
     {
       ch=pop();
       if(exp[i]==')'&&(ch=='{'||ch=='['))
       {
flag=0;
       }
       if(exp[i]=='}'&&(ch=='('||ch=='['))
       {
flag=0;
       }
       if(exp[i]==']'&&(ch=='('||ch=='{'))
       {
flag=0;
       }
     }
     i++;
   }
   if(top>=0)
   {
     flag=0;
   }
   if(flag==1)
   {
     printf("\n Valid Expression");
   }
   else
   {
     printf("\n Invalid Expression");
   }
   getch();
}
void push(char c)
{
  if(top==MAX-1)
  {
   printf("\n stack is full");
  }
  else
  {
    top++;
    stack[top]=c;
  }
}
char pop()
{
  char c;
  if(top==-1)
  {
   printf("\n Stack is empty");
  }
  else
  {
   c=stack[top];
   top--;
   return c;
  }
}

C program in stack to concatenate two strings using push and pop functions

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 30
void push(char ch);
char pop();
char stack[max];
int top=-1;
void main()
{
char a[20],b[20],k[40],ch;
int i,l1,l2;
clrscr();
printf("\n Enter First String:-\n");
scanf("%s",a);
printf("\n Enter Second String:-\n");
scanf("%s",b);
l1=strlen(a);
l2=strlen(b);
for(i=0;i<l2;i++)
{
 push(b[i]); //push second string into stack
}
push(' ');
for(i=0;i<l1;i++)
{
 push(a[i]); //push first string into stack
}
printf("\n After concatenation of two string:-\n");
i=0;
while(top!=-1)
{
 ch=pop();
 if(ch==' ')
{
k[i]='\0';
printf("%s",strrev(k));
i=0;
 }
else
 {
  k[i]=ch;
  i++;
 }
}
k[i]='\0';
printf("%s",strrev(k));
getch();
}
void push(char ch)
{
if(top==max-1)
{
 printf("\n Stack is full");
}
else
{
 top++;
 stack[top]=ch;
}
}

char pop()
{
char t;
if(top==-1)
{
 printf("\n Stack is Empty");
}
else
{
 t=stack[top];
 top--;
 return(t);
}
}

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...