Monday, June 13, 2011

Data Structure in C++

1.Stack Using Top fixed Method

/*Top fixed method*/

#include<iostream>
#define size 4
int bos=0;
using namespace std;


class stack
{
    private:
        int arr[size];

    public:
       void push()
       {
         char response;
         while(response!='n')
         {

             if (bos==size)
             {
               cout<<"\nStack Full";
               break;
             }
             else
             {

               cout<<"\n Enter arraay element 0 :";
               for(int i=bos;i>0;i--)
               {
                   arr[i]=arr[i-1];

               }
               bos++;
               cin>>arr[0];
               cout<<"\n Do you push more items (y or n)??";
               cin>>response;
             }
        }
       }
       void show()
       {
           cout<<"\n The array elements starting from top are :";
           for(int i=bos-1;i>=0;i--)
           {
            cout<<"\n"<<arr[i];
           }

       }
    void pop();
};
void stack::pop()
{
  char response;
   while(response!='n')
    {
     if(bos==0)
     {
       cout<<"\n Stack Empty fill data first";
       break;
     }

     else
     {
      for(int i=0;i<bos;i++)
      {
        arr[i]=arr[i+1];

      }
       bos--;
       cout<<"\n want to pop more (y or n)???";
       cin>>response;
     }
    }
}
int main()
{
    stack s1;
    int res;
    while(1)
    {
        cout<<"\n Enter your choice \n 1 to push\n 2 to pop \n 3 to show array elements \n 4 to exit \n:";
        cin>>res;
        if(res==1)
         s1.push();
        else if (res==2)
          s1.pop();
        else if(res==3)
          s1.show();
        else
           break;
    }
    return 0;
}

2.Stack Using Top Varying Method


/*A stack is an ordered collection of items intowhich new items may be inserted and from which deleted
at one end called top of the stack . It is the Last in irst Out type of data structure.Operations that
can be performed on stack are
- Create stack
-Push items
- Pop items
- Display Stack items
-Stack implementation
*/


#include<iostream>
#define size 5
int top=-1;
using namespace std;
class stack
{
    private:
        int arr[size];

    public:
       void push()
       {
         char response;
         while(response!='n')
         {
             if (top>size)
             {
               cout<<"\nStack Full";
               break;
             }
             else
             {
               cout<<"\n Enter arraay element"<<top+1<<": ";
               cin>>arr[top+1];
               top++;
               cout<<"\n Do you push more items (y or n)??";
               cin>>response;
             }
        }
       }
       void show()
       {
           cout<<"\n The array elements are :";
           for(int i=0;i<10;i++)
           {
            cout<<"\n"<<arr[i];
           }

       }
    void pop();
};
void stack::pop()
{
char response;
 while(response!='n')
 {
     if(top==-1)
     {
       cout<<"\n Stack Empty fill data first";
       break;
     }

     else
       arr[top]=' ';
       top--;
       cout<<"\n want to pop more (y or n)???";
       cin>>response;
 }
}
int main()
{
    stack s1;
    int res;
    while(1)
    {
        cout<<"\n Enter your choice \n 1 to push\n 2 to pop \n 3 to show array elements \n 4 to exit \n:";
        cin>>res;
        if(res==1)
         s1.push();
        else if (res==2)
          s1.pop();
        else if(res==3)
          s1.show();
        else
           break;
    }
    return 0;
}



3. C++ Code to change Infix expression to Postfix

///infix expression to postfix
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class infixtopost
{
    private:
      string infix,postfix;
      stack<char> stk;
    public:
       void get()
       {
            cout<<"\nEnter a infix expression: ";
            cin>>infix;
           infix='('+infix+')';
       }
       void show()
       {
           cout<<"\nInfix expression you entered is: ";
           cout<<infix;
           cout<<"\nThe equivalent postfix expression is: ";
           cout<<postfix;
       }
       bool checkoperand(char a)
       {
           if((a>='a'&& a<='z') || (a>='0' and a<='9') or (a>='A' && a<='Z'))
             {
              return true;
              }
           else
             return false;
       }
      int checkprecedence(char &infi,char &stak)
       {
            if (infi==')')
                return 2;     /// 2 for poping action in stack
            else if(infi=='$'and stak !='$')
                return 1;    ///1 to push that infix to stack
            else if (infi!='$' and stak=='$')
                return 4;
            else if (infi=='$' and stak=='$')
                return 1;
            else if((infi=='*') or (infi=='/'))
                return 1;
            else if(((infi=='+') or (infi=='-'))and (stak=='*' or stak=='/'))
                 return 3;
            else
                 return 1;
        }
       void evaluate()
       {
           for(int i=0;i<infix.size();i++)
           {
               if(checkoperand(infix[i]))
                 {
                     postfix=postfix+infix[i];
                 }
               else if (infix[i]=='(')
                    stk.push(infix[i]);
               else
                {
                    {
                        char chara;
                      int choice=checkprecedence(infix[i],stk.top());
                      switch(choice)
                      {
                        case 1:
                            stk.push(infix[i]);
                            break;
                        case 2:
                                   while(stk.top()!='(')
                                        {
                                            chara=stk.top();
                                            postfix=postfix+chara;
                                            stk.pop();
                                        }stk.pop();
                                        break;
                        case 3:
                                   while(stk.top()!='(')
                                        {
                                            chara=stk.top();
                                            postfix=postfix+chara;
                                            stk.pop();
                                        }stk.push(infix[i]);
                                break;
                          case 4:
                                   while(stk.top()!='+')
                                        {
                                            chara=stk.top();
                                            postfix=postfix+chara;
                                            stk.pop();
                                        }stk.push(infix[i]);
                                break;
                    }
                    }
                }

           }
       }
};
int main()
{
    char res;
    while(res!='n')
    {
    infixtopost in;
    in.get();
    in.evaluate();
    in.show();
    cout<<"\n Do more calculation y or n ??";
     cin>>res;
    }
}




4.C++ SourceCode To evaluate postfix expression


#include<iostream>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
class postfix
{
  private:
    string postfix;
     stack<float>stk;
     float result,value1,value2;
  public:
     void display()
     {
         cout<<"\n The postfix expression you entered is:  ";
         cout<<postfix;
         cout<<"\n evaluated result is: ";
         cout<<result;
     }
     void getpostfix()
     {
         cout<<"\nEnter a postfix expression: ";
         cin>>postfix;
         postfix=postfix;
     }
     void evaluate()
     {
         char bbc;
             for (int i=0;i<postfix.size();i++)
             {
                 bbc=postfix[i];
                 if(isdigit(bbc)==true)
                   {
                        result=bbc-'0';
                        stk.push(result);
                   }
                 else
                 {
                     value2=stk.top();
                     stk.pop();
                     value1=stk.top();
                     stk.pop();
                     switch (bbc)

                       {
                           case '$':
                                          result=pow(value1,value2);
                                          break;
                           case '+':
                                          result = value1 + value2;
                                          break;
                           case '-':
                                          result = value1 - value2;
                                          break;
                           case '*':
                                          result = value1*value2;
                                          break;
                           case '/':
                                          result = value1/value2;
                                          break;
                       }
                       stk.push(result);
                 }
             }
             result=stk.top();
     }
};
int main()
{
    char response;
    while(response!='n')
    {
      postfix p1;
      p1.getpostfix();
      p1.evaluate();
      p1.display();
      cout<<"\n Redo this Calculation??(y or n)";
      cin>>response;
    }
}

No comments:

Post a Comment