Wednesday, 9 December 2015

Que Implementation


#include <iostream>
using namespace std;

class que
{
private: int q[5], F, R;
public:
    que()
    {
        R = F = -1;
    }

    void qinsert(int val)
    {
        if (R > 4)
            cout << "que full";
        else
        {
            R++;
            q[R] = val;
        }
            if (F == -1)
                F = 0;
       
    }
    int qdel()
    {
        int temp;
        if (F == -1)
            cout << "que empty";
        else
        {
            temp = q[F];
            if (F == R)
                F = R = -1;
            F++;
            return temp;
        }
   
    }
    void print()
    {

        for (int i = F; i <= R; i++)
            cout << q[i] << "-";

    }
};

int main()
{
    int n;
    que obj;
    for (int i = 0; i <= 4; i++)
    {
        cout << "Enter value to Que: ";
        cin >> n;
        obj.qinsert(n);

    }
    cout << "The Que is: ";
    obj.print();
    {
        for (int k = 0; k < 2; k++)

        {
            cout <<endl<< "After Removing Number " << obj.qdel()<<endl;
            cout << "The Que is: ";
            obj.print();
        }
    }
    cout << endl;
}

Thursday, 3 December 2015

Infix to Postfix Conversion

#include <iostream>
using namespace std;


class expression
{
private:
    char st[100], stack[10];
    char ch;
    int top;
public:
    expression()
    {
        top = -1;
    }
    void input()
    {
        cout << "Please Enter Expression having operator + *  / - ( )" << endl;
        cin >> st;
    }
    void scan(void);
    void push(void);
    void pop(void);

};
int main()
{
    expression obj;
    obj.input();
    obj.scan();
    cout << endl;
}
void expression::scan(void)
{
    for (int i = 0; st[i] != '\0';i++)
    {
        ch = st[i];
        if (ch == '(' || ch == ')' || (ch == '*') || (ch == '+') || (ch == '-') || (ch == '/'))
        {
            if (ch == '(')
                push();

            else if (top == -1)
                push();
            else if (stack[top] == '(')
                push();
            else if (((ch == '*') || (ch == '/')) && ((stack[top] == '+') || (stack[top] == '-')))
                push();
            else if (ch == ')')
                pop();
            else
            {
                pop();
                push();
            }

        }
        else
        {
            cout << ch;
        }
    }

    pop();
}
void expression::push(void)
{
    top++;
    stack[top] = ch;
}
void expression::pop(void)
{
    while (top != -1)
    {
        if (ch == ')')
        {
            cout << stack[top];
            top--;


            if (stack[top] == '(')
            {

                top--;
                break;
            }

        }


        else
        {
            cout << stack[top];
            top--;
            if (stack[top] = '(')
            {

                break;
            }
        }
    }
}

Tree Insertion

#include <iostream> using namespace std; struct node {     int data;     node*left;     node* right; }; class tree { priva...