Showing posts with label narasimha karumanchi. Show all posts
Showing posts with label narasimha karumanchi. Show all posts

Saturday, February 7, 2015

Queue implementation and operation in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class queue
{ public:
    int size;
    int elmnt[50];
    int front;
    int rear;
    int isfull();
int isempty();
    void enqueue(int e);
    int dequeue();
    queue()
    {size=5;
     front=-1;
     rear=-1;
    }
};
int queue::isfull()
{if((rear+1)%size==front)
  {cout<<"Full";
   return 1;
  }
  else
   return 0;
}
int queue::isempty()
{if(front==-1)
  {cout<<"Empty";
   return 1;
  }
  else
  return 0;
}
void queue::enqueue(int e)
{ if(isfull())
   exit(1);
  else
   { rear=(rear+1)%size;
     elmnt[rear]=e;
     if(front==-1)
     front++;
   }
}
int queue::dequeue()
{ int c;
  if(isempty())
   exit(1);
  else
   {c=elmnt[front];
    if(front==rear)
    {front=-1;
     rear=-1;
    }
    else
     front=(front+1)%size;
    return(c);
   }
}
int main()
{queue s;int d,i;
  s.enqueue(4);
  s.enqueue(5);
  for(i=0;i<3;i++)
  {d=s.dequeue();
   cout<<d<<endl;
  }
}

Generic stack in C++

#include<iostream>
using namespace std;
union u
{
int i;
char c;
float f;
};
struct elmnt
{
u b;
int tag;
};
class stack
{
public:
int top;
elmnt e[10],r,t;
void push(elmnt r);
elmnt pop();
stack()
{
top=-1;
}
};
void stack::push(elmnt r)
{
e[++top]=r;
}
elmnt stack::pop()
{
return(e[top--]);
}
int main()
{
stack s;
char ch;
int ctr=0,j;
do
{
cout<<"Press \n1 for int\n2 for char\n3 for float\n";
cin>>s.r.tag;
if(s.r.tag==1)
{
cin>>s.r.b.i;
s.push(s.r);
}
else if(s.r.tag==2)
{
cin>>s.r.b.c;
s.push(s.r);
}
else
{
cin>>s.r.b.f;
s.push(s.r);
}
ctr++;
cout<<"Press y for more:";
cin>>ch;
}
while(ch=='y' || ch=='Y');
cout<<"\n\nThus the stack contains:\n";
for(j=0;j<ctr;j++)
{
s.t=s.pop();
if(s.t.tag==1)
cout<<s.t.b.i<<endl;
else if(s.t.tag==2)
cout<<s.t.b.c<<endl;
else
cout<<s.t.b.f<<endl;
}
}

Evaluating the postfix expression using stack in C++

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class stack
{ public:
char arr[50];
int k,top,arr2[50],c[2];
int pop();
void push(int c);
};
void stack::push(int c)
{arr2[++top]=c;
}
int stack::pop()
{c[k]=arr2[top--];
}
int main()
{stack s;
s.top=-1;
int i,j,z,x;
char c2;
cin.getline(s.arr,30);
j=strlen(s.arr);
for(i=0;i<j;i++)
 {if(s.arr[i]=='-' || s.arr[i]=='+' || s.arr[i]=='*' || s.arr[i]=='/')
     {for(s.k=0;s.k<2;s.k++)
     s.pop();
     c2=s.arr[i];
     switch(c2)
     {case '-':z=s.c[1]-s.c[0];
               break;
      case '+':z=s.c[1]+s.c[0];
               break;
      case '*':z=s.c[1]*s.c[0];
               break;
      case '/':z=s.c[1]/s.c[0];
               break;
     } s.push(z);
}
  else
    {x=s.arr[i];
     x-=48;s.push(x);
    }
 }
 cout<<s.arr2[s.top];
}

validation of infix expression in C++

#include<iostream>
#include<cstring>
using namespace std;
class stack
{
int top;
int size;
char elmnt[50];
public:
void push(char ch);
char pop();
stack()
{
top=-1;
size=50;
}
}s;
void stack::push(char ch)
{
elmnt[++top]=ch;
}
char stack::pop()
{
return(elmnt[--top]);
}
void check(char arr[],int l)
{
int i,flag;
char c;
for(i=0;i<l;i++)
{
flag=0;
if(arr[i]=='(' || '{' || '[')
{
if(arr[i]=='(')
s.push(')');
if(arr[i]=='{')
s.push('}');
else
s.push(']');
}
else if(arr[i]==')' || arr[i]=='}' || arr[i]==']')
{
if(arr[i]==')')
{
c=s.pop();
if(c!=')')
{
flag=1;
break;
}
}
if(arr[i]=='}')
{
c=s.pop();
if(c!='}')
{
flag=1;
break;
}
}
else
{
c=s.pop();
if(c!=']')
{
flag=1;
break;
}
}
}
}
cout<<"The Expression is ";
if(flag==1)
cout<<"Not Balanced";
else
cout<<"Balanced";
}
int main()
{
int l;
char arr[50];
cout<<"Enter the Infix Expression...\n";
cin.getline(arr,50);
l=strlen(arr);
check(arr,l);
}

calculate the value of infix expression using 1 stack in C++

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class stack
{public:
int top,t,c[3],z,i,j;
char a[50],b;
void push(char c);
char pop();
void check(char d);
};
void stack::push(char c)
{a[++top]=c;
 t=top;
}
char stack::pop()
{c[0]=a[top--]-48;
 c[1]=a[top--];
 c[2]=a[top--]-48;
 if(c[1]=='-')
 z=c[2]-c[0];
 else if(c[1]=='+')
 z=c[2]+c[0];
 else if(c[1]=='*')
 z=c[2]*c[0];
 else
 z=c[2]/c[0];
 b=z+48;
 push(b);
 if(i==j);
 else check(a[i]);
}
void stack::check(char d)
{--t;
 switch(d)
 {case '+':
  case '-':if(t<0)
           push(d);
           else
           pop();
           break;
  case '*':
  case '/':if(a[t]=='*' || a[t]=='/')
           pop();
           else
           push(d);
           break;
  default :push(d);
           break;
 }
}
int main()
{stack s;s.top=-1;s.t=-1;
 int k;
 cout<<"Enter the infix:\n";
 cin.getline(s.a,50);
 s.j=strlen(s.a);
 for(s.i=0;s.i<s.j;s.i++)
 {s.check(s.a[s.i]);
 }
 while(s.top>0)
 {s.pop();
 }
 cout<<"Hence the ans is:";
 k=s.a[0]-48;
 cout<<k;
}

calculate the value of infix expression using 2 stacks in C++

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class stack
{ public:
int t,arr1[20],m,e[2],top1,top2,i;
char a[50],arr2[20];
void pus1(int p);
void pus2(char q);
int pop1();
char pop2();
void check(char w);
};
void stack::pus1(int p)
{arr1[++top1]=p;
}
void stack::pus2(char q)
{arr2[++top2]=q;
}
void stack::check(char w)
{switch(w)
 {case '+':
  case '-':if(top2>-1)
           pop2();
           else
           pus2(w);
           break;
  case '*':
  case '/':if(arr2[top2]=='*' || arr2[top2]=='/')
           pop2();
           else
           pus2(w);
           break;
 }
}
int stack::pop1()
{e[0]=arr1[top1--];
 e[1]=arr1[top1--];
 if(arr2[top2]=='-')
 m=e[1]-e[0];
 else if(arr2[top2]=='+')
 m=e[1]+e[0];
 else if(arr2[top2]=='*')
 m=e[1]*e[0];
 else
 m=e[1]/e[0];
 top2--;
 pus1(m);
}
char stack::pop2()
{pop1();
 check(a[i]);
}
int main()
{stack s;
 s.top1=-1;
 s.top2=-1;
 int j,z;
 cout<<"Enter the infix expression:\n";
 cin.getline(s.a,50);
 j=strlen(s.a);
 for(s.i=0;s.i<j;s.i++)
 {if(s.a[s.i]=='-' || s.a[s.i]=='+' || s.a[s.i]=='*' || s.a[s.i]=='/')
   {s.check(s.a[s.i]);
   }
  else
   {s.t=s.a[s.i];
    s.t-=48;
    s.pus1(s.t);
   }
 }
 z=s.top2;
 for(s.i=0;s.i<=z;s.i++)
 {s.pop1();
 }
 cout<<"Hence the ans is:\n";
 cout<<s.arr1[0];
}

stack implementation in C++

#include<iostream>
#include<cstdlib>
using namespace std;
class stack
{public:
int top[8],arr[8][125];
void push(int a,int b);
int pop(int a);
stack()
{top[0]=-1;
top[1]=-1;
top[2]=-1;
top[3]=-1;
top[4]=-1;
top[5]=-1;
top[6]=-1;
top[7]=-1;
}
};
void stack::push(int a,int b)
{arr[a][++top[a]]=b;
}
int stack::pop(int a)
{return arr[a][top[a]--];
}
int main()
{stack s;int i,j,k,n,p,q;
 for(i=0;i<4;i++)
 {cout<<"Enter the array no:";
  cin>>n;
  cout<<"Enter the element\n";
  for(k=0;k<125;k++)
  { cin>>p;
    s.push(n,p);
  }
 }
 for(i=0;i<8;i++)
 {cout<<"Enter the array:";
  cin>>n;
  for(j=0;j<125;j++)
   {k=s.pop(n);
    cout<<k<<endl;
   }
 }
}

Contributors

Translate