Followers

Wednesday, 1 July 2020

Data Structures: Lecture 10

Department: MCA
Semester    : II
Subject       : Data Structures through C++
Paper          : CCMCA 203 
Faculty        : Avinash Kumar




Syllabus covered in  this blog
STACK (Program to implement Stack)


Program to convert infix to postfix


#include <iostream.h>
class stack
{
    public:
    char stack_array [50];
    int top;
    stack()
    {
        top = -1;
    }

    void push(char symbol)
    {
        if (full())
        {
            cout << "\nStack overflow"<< endl;
        }
        else
        {
            top = top + 1;
            stack_array[top] = symbol;
        }
    }

    char pop()
    {
        if (empty())
            return ('#');   //Return value '#' indicates stack empty
        else
            return (stack_array[top--]);
    }

    int empty()
    {
        if (top == -1)
            return (1);
        else
            return (0);
    }

    int full()
    {
        if (top == 49)
            return (1);
        else
            return (0);
    }
};

class Expression
{
    char infix[50];
    char postfix[50];

    public:
    void read()
    {
        cout << "\nEnter an infix expression: ";
        cin >> infix;
    }

    int white_space(char symbol)
    {
        if (symbol == ' ' || symbol == '\t' || symbol == '\0')
            return 1;
        else
            return 0;
    }

    void ConvertToPostfix()
    {

        stack s;
        int l, precedence, p;
        char entry1, entry2;       
        p = 0;
        for (int i = 0; infix[i] != '\0'; i++)
        {
            entry1 = infix[i];
            if (!white_space(entry1))
            {
                switch (entry1)
                {
                    case '(':
                        s.push(entry1);
                        break;
                    case ')':
                        while ((entry2 = s.pop()) != '(')
                            postfix[p++] = entry2;
                        break;
                    case '+':
                    case '-':
                    case '*':
                    case '/':
                        if (!s.empty())
                        {
                            precedence = prec(entry1);
                            entry2 = s.pop();
                            while (precedence <= prec(entry2))
                            {
                                postfix[p++] = entry2;
                                if (!s.empty())
                                    entry2 = s.pop();
                                else
                                    break;
                            }
                            if (precedence > prec(entry2))
                                s.push(entry2);
                        }
                        s.push(entry1);
                        break;
                    default:
                        postfix[p++] = entry1;
                        break;
                }
            }
        }
        while (!s.empty())//While not stack empty
            postfix[p++] = s.pop();
                postfix[p] = '\0';   
        cout << "\nThe postfix expression is: " << postfix << endl;
    }

    int prec(char symbol)
    {
        switch (symbol)
        {
            case '/': return (4);  /*Precedence of / is 4 */
            case '*': return (3);  /*Precedence of * is 3 */
            case '+': return (2);  /*Precedence of + is 2 */
            case '-': return (1);  /*Precedence of - is 1 */
            case '(': return (0);  /*Precedence of ( is 0 */
            default: return (-1);
        }
    }
   
    int openingParenthesis(char entry)
    {
        if ((entry == '(') || (entry == '{') || (entry == '['))
            return (1);
        else
            return (0);
    }

    int closingParenthesis(char entry)
    {
        if ((entry == ')') || (entry == '}') || (entry == ']'))
            return (1);
        else
            return (0);
    }

    int match(char entry1, char entry2)
    {
        if ((entry1 == '(') && (entry2 == ')'))
            return (1);
        else if ((entry1 == '{') && (entry2 == '}'))
            return (1);
        else if ((entry1 == '[') && (entry2 == ']'))
            return (1);
        else
            return (0);
    }
   
    int checkParenthesis()
    {
        stack s;
        char entry;

        //Determine the length of the infix expression
            int len;

        for (len=0; infix[len]!= '\0'; len++);

        for (int i = 0; i < len; i++)
        {
            if (openingParenthesis(infix[i]))
                s.push(infix[i]);
            else if (closingParenthesis(infix[i]))
            {
                if (s.empty())
                    return (1);
                entry = s.pop();

                if (!match(entry, infix[i]))
                    return (2);
            }
        }

        if (!s.empty())
            return (3);
        else
            return (0); 
    }
};

void main()
{
    char choice = 'y';
    Expression expr;
    while (choice == 'y')
    {
        expr.read();
        int flag = expr.checkParenthesis();
        if (flag == 1)
            cout<<"\nNumber of closing parentheses is more than the number of opening parentheses.";
        else if (flag == 2)
            cout << "\nA closing parenthesis does not match its corresponding opening parenthesis.";
        else if (flag == 3)
            cout << "\nNumber of opening parentheses is more than the number of closing parentheses.";
        else
            expr.ConvertToPostfix();       
        cout << "\n\nDo you want to continue? (y/n):";
        cin >> choice;
    }
}

Saturday, 20 June 2020

Data Structures: Lecture 9

Department: MCA
Semester    : II
Subject       : Data Structures through C++
Paper          : CCMCA 203 
Faculty        : Avinash Kumar




Syllabus covered in  this blog
STACK (Infix to Post-fix, Evaluation of Post-fix)



Infix to Postfix Conversion

X is an arithmetic expression written in infix notation.

This algorithm finds the equivalent post-fix expression Y.

  

1.    Scan X from left to right and repeat Step 2 to 5 for each element of X until the Stack is empty.

2.    If an operand is encountered, add it to Y.

3.    If a left parenthesis is encountered, push it onto Stack.

4.    If an operator is encountered ,then:

a.    Pop from Stack and add each operator which has the same precedence as or higher precedence than scanned operator to Y.

b.    Add the scanned operator to Stack.

5.    If a right parenthesis is encountered ,then:

a.    Pop each operator from the Stack until a left parenthesis is encountered and add it to Y.

b.    Remove the left Parenthesis


Let’s take an example to better understand the algorithm

Infix Expression: 
 
    A+ (B*C-(D/E^F)*G)*H
  where ^ is an exponential operator.




Evaluation of Post-fix expression

·         While reading the expression from left to right, push the element in the stack if it is an operand.

·         Pop the two operands from the stack, if the element is an operator and then evaluate it.

·         Push back the result of the evaluation. Repeat it till the end of the expression.


Algorithm


1.    Add ) to postfix expression.

2.    Read postfix expression Left to Right until ) encountered

3.    If operand is encountered, push it onto Stack

4.    If operator is encountered, Pop two elements

a.    A -> Top element

b.    B-> Next to Top element

c.    Evaluate B operator A

d.    Push B operator A onto Stack


Example

Expression:  456*+




Friday, 19 June 2020

Data Structures : Lecture 8

Department: MCA
Semester    : II
Subject       : Data Structures through C++
Paper          : CCMCA 203 
Faculty        : Avinash Kumar



Syllabus covered in  this blog
STACK (Checking expressions)


Checking expressions using STACK 



Consider an example using the expression:

{(a + b) × (c + d) + (c × d)]}

Scan the expression from left to right. 


·         The first entry to be scanned is ‘{’, which is a left parenthesis.

·         Push it into the stack.

·         The next entry to be scanned is ‘(’, which is a left parenthesis.

·         Push it into the stack.

·         The next entry is ‘a’, which is an operand. Therefore, it is discarded.

·         The next entry is ‘+’, which is an operator. Therefore, it is discarded.

·         The next entry is ‘b’, which is an operand. Therefore, it is discarded.

·         The next entry to be scanned is ‘)’, which is a right parenthesis

·         POP the topmost entry from the stack.

·         Match the two brackets.

·         The next entry to be scanned is ‘×’, which is an operator. Therefore, it is discarded.

·         The next entry to be scanned is ‘(’, which is a left parenthesis

·         Push it into the stack

·         The next entry to be scanned is ‘c’, which is an operand. Therefore it is discarded

·         The next entry to be scanned is ‘+’, which is an operator. Therefore it is discarded

·         The next entry to be scanned is ‘d’, which is an operand. Therefore it is discarded

·         The next entry to be scanned is ‘)’, which is a right parenthesis.

·         POP the topmost element from the stack.

·         Match the two brackets.

·         The next entry to be scanned is ‘+’, which is an operator. Therefore, it is discarded.

·         The next entry to be scanned is ‘(’, which is a left parenthesis.

·         Push it into the stack.

·         The next entry to be scanned is ‘c’, which is an operand. Therefore, it is discarded.

·         The next entry to be scanned is ‘×’, which is an operator. Therefore, it is discarded.

·         The next entry to be scanned is ‘d’, which is an operand. Therefore, it is discarded.

·         The next entry to be scanned is ‘)’, which is a right parenthesis.

·         POP the topmost element from the stack.

·         Match the two brackets.

·         The next entry to be scanned is ‘]’, which is a right parenthesis.

·         POP the topmost element from the stack.

·         Match the two brackets


Since the brackets do not match,
it is an INVALID expression.