dslabrecord

Upload: joyal-ks

Post on 05-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 DSLabRecord

    1/7

    CS010 407: Data Structures Lab Record

    ST. JOSEPH'S COLLEGE OF ENGINEERING AND TECHNOLOGY, PALAI

    Department of Computer Science and Engineering

    Lab Record

    CS010 407: Data Structures

    Program : B. Tech. Semester : IV

    Session : 2012-13 Subject Code : CS010 407

    Subject Name : Data Structures Credits : 2

    Student Name : Mr/Ms XXXXX

    Department of Computer Science and Engineering P a g e | 1

  • 8/2/2019 DSLabRecord

    2/7

    CS010 407: Data Structures Lab Record

    Index

    EXPNO

    AIM OF THE PROGRAMDATE OF

    SUBMISSIONSIGNATURE

    1 Implement Infix to postfix conversion using Array DD/MM/YY

    2 DD/MM/YY

    3 DD/MM/YY

    Exp No: 01

    Date : DD/MM/YY

    Department of Computer Science and Engineering P a g e | 2

  • 8/2/2019 DSLabRecord

    3/7

    CS010 407: Data Structures Lab Record

    AIM : Implement Infix to postfix conversion using Array

    ALGORITHM :

    SOURCE CODE:

    /************************************************************* Filename: infix_to_postfix.c* Description: infix to postfix conversion using stack* Author:Sarju S* Date: 01-Dec-2011*************************************************************/#include#include#define MAX_EXPR_SIZE 100#define MAX_STACK_SIZE 100

    typedef enum {eos,lparen, rparen, plus, minus, times, divide,mod,operand} precedence;char infixExpr[MAX_EXPR_SIZE],postfixExpr[MAX_EXPR_SIZE];

    precedence stack[MAX_STACK_SIZE];int top=-1,infixCount=0,postfixCount=0;

    void stackFull(){

    fprintf(stderr, "Stack is full cannot add elements\n");exit(EXIT_FAILURE);

    }

    void stackEmpty(){

    fprintf(stderr, "Stack is empty cannot delete elements\n");exit(EXIT_FAILURE);

    }

    void push(precedence item){/* add an element to stack */

    if(top>=MAX_STACK_SIZE-1)stackFull();

    top++;stack[top] = item;

    }

    precedence pop(){/*Delete top element from the stack*/

    if(top==-1)stackEmpty();

    return stack[top--];}

    void printToken(precedence token){

    /* to print the symbol corresponding to token*/switch(token) {

    case 3 :postfixExpr[postfixCount]='+';

    Department of Computer Science and Engineering P a g e | 3

  • 8/2/2019 DSLabRecord

    4/7

    CS010 407: Data Structures Lab Record

    postfixCount++;break;

    case 4 :postfixExpr[postfixCount]='-';postfixCount++;

    break;case 5 :postfixExpr[postfixCount]='*';

    postfixCount++;break;

    case 6 :postfixExpr[postfixCount]='/';postfixCount++;break;

    case 7 :postfixExpr[postfixCount]='%';postfixCount++;}

    }

    precedence getToken(char *symbol){ /* get the next token, symbol is the character representation,which is

    returned, the token is represented by its enumerated value, which isreturned in the function name */

    *symbol = infixExpr[infixCount];infixCount++;switch(*symbol) {

    case '\0': return eos;case '(' : return lparen;case ')' : return rparen;case '+' : return plus;case '-' : return minus;case '/' : return divide;case '*' : return times;case '%' : return mod;default : return operand;}

    }

    int checkPriority(precedence token){switch(token) {

    case plus :

    case minus : return 4;case times :case divide : return 6;}

    }

    void infixToPostfix(){/* output posfix of expression */

    char symbol;precedence token;top = 0;

    stack[0]= eos;token = getToken(&symbol);while(token!=eos){

    if(token == operand){

    Department of Computer Science and Engineering P a g e | 4

  • 8/2/2019 DSLabRecord

    5/7

    CS010 407: Data Structures Lab Record

    postfixExpr[postfixCount]=symbol;postfixCount++;

    }

    else if(token == rparen){/*unstack tokens until left paranthesis */

    while(stack[top]!= lparen)printToken(pop());

    pop(); /*remove left paranthesis */}else {/*remove and print symbols whose in-stack precedence is greater than or

    equal to the current tokens incomming precedence */if(token!=1) /*Avoid Comparison of Left Paranthesis */while(checkPriority(stack[top])>= checkPriority(token))

    printToken(pop());

    push(token);}

    token =getToken(&symbol);}while((token = pop())!=eos)

    printToken(token);postfixExpr[postfixCount]='\0';

    }

    void main(){printf("\nEnter the INFIX expression:");scanf("%s",infixExpr);infixToPostfix(); /* Function Call*/

    printf("\nThe POSTFIX expression:");printf("%s\n",postfixExpr);

    }

    INPUT :

    a) a/b-c+d*e-a*cb) ((a/(b-c+d)))*(e-a)*c

    OUTPUT:

    sjcet@sjcet-laptop:~$ gcc infix_to_postfix.csjcet@sjcet-laptop:~$ ./a.out

    Enter the INFIX expression:a/b-c+d*e-a*c

    The POSTFIX expression:ab/c-de*+ac*-sjcet@sjcet-laptop:~$ ./a.out

    Enter the INFIX expression:((a/(b-c+d)))*(e-a)*c

    The POSTFIX expression:abc-d+/ea-*c*sjcet@sjcet-laptop:~$

    Department of Computer Science and Engineering P a g e | 5

  • 8/2/2019 DSLabRecord

    6/7

    CS010 407: Data Structures Lab Record

    RESULT: The above program is executed successfully and the output is verified.

    Department of Computer Science and Engineering P a g e | 6

  • 8/2/2019 DSLabRecord

    7/7

    CS010 407: Data Structures Lab Record

    Exp No: 02

    Date : DD/MM/YY

    Department of Computer Science and Engineering P a g e | 7