• Click below to download Unit 3 programs in pdf file: -



Unit 3


/* 1. Write a program to implement following operations in stack Using array and Linked List.

• PUSH

• POP

• PEEP

*/


/**** Using Array ****/


#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;


void main ()

{


    printf("Enter the number of elements in the stack : ");

    scanf("%d",&n);


printf("\n----------------------------------------------\n");

    while(choice != 4)

    {

        printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit");


        printf("\n Enter your choice : ");

        scanf("%d",&choice);


        switch(choice)

        {

            case 1:

            {

                push();

                break;

            }

            case 2:

            {

                pop();

                break;

            }

            case 3:

            {

                peep();

                break;

            }

            case 4:

            {

                display();

                break;

            }

            case 5:

            {

                printf("Exiting");

                break;

            }

            default:

            {

                printf("Please Enter valid choice ");

            }

        };

    }

}


void push ()

{

    int val;

    if (top == n )

    printf("\n Overflow");

    else

    {

        printf("Enter the value : ");

        scanf("%d",&val);

        top = top +1;

        stack[top] = val;

    }

}


void pop ()

{

    if(top == -1)

    printf("Underflow");

    else

    top = top -1;

}


void peep()

{

int item = 0;


if(top == -1)

{

printf("Underflow");

}

else

{

item = stack[top];

    printf(item);

}

}


void display()

{

    for (i=top;i>=0;i--)

    {

        printf("%d\n",stack[i]);

    }

    if(top == -1)

    {

        printf("Stack is empty");

    }

}



/**** Using Linked List ****/


/*


#include <stdio.h>

#include <stdlib.h>


struct node

{

int val;

struct node *next;

};

struct node *head;


void main ()

{

    int choice=0;


    while(choice != 4)

    {

        printf("1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit");

        printf("\nEnter your choice : ");

        scanf("%d",&choice);

        switch(choice)

        {

            case 1:

            {

                push();

                break;

            }

            case 2:

            {

                pop();

                break;

            }

             case 3:

            {

                peep();

                break;

            }

            case 4:

            {

                display();

                break;

            }

            case 5:

            {

                printf("Exiting");

                break;

            }

            default:

            {

                printf("Please Enter valid choice ");

            }

    };

}

}

void push ()

{

    int val;

    struct node *ptr = (struct node*)malloc(sizeof(struct node));

    if(ptr == NULL)

    {

        printf("not able to push the element");

    }

    else

    {

        printf("Enter the value");

        scanf("%d",&val);

        if(head==NULL)

        {

            ptr->val = val;

            ptr -> next = NULL;

            head=ptr;

        }

        else

        {

            ptr->val = val;

            ptr->next = head;

            head=ptr;


        }

        printf("Item pushed");


    }

}


void pop()

{

    int item;

    struct node *ptr;

    if (head == NULL)

    {

        printf("Underflow");

    }

    else

    {

        item = head->val;

        ptr = head;

        head = head->next;

        free(ptr);

        printf("Item popped");


    }

}


int peep(int pos)

{

    struct node* ptr = head;

    for (int i = 0; (i < pos-1 && ptr!=NULL); i++)

    {

        ptr = ptr->next;

    }

    if(ptr!=NULL){

        return ptr->val;

    }

    else{

        return -1;

    }

}


void display()

{

    int i;

    struct node *ptr;

    ptr=head;

    if(ptr == NULL)

    {

        printf("Stack is empty\n");

    }

    else

    {

        printf("Printing Stack elements \n");

        while(ptr!=NULL)

        {

            printf("%d\n",ptr->val);

            ptr = ptr->next;

        }

    }

}


*/



/* 2. Write a program to implement Evaluation of given postfix expression. */


#include<stdio.h>

#include<ctype.h>

#define MAX 100


float st[MAX];

int top=-1;


void push(float val)

{

    if(top==MAX-1)

    {

        printf("\nStack Overflow:");

        return;

    }

    top++;

        st[top]=val;

}


float pop()

{

    float tmp;

    if(top==-1)

    {

        printf("\nStack Underflow:");

        return '0';

    }

    tmp=st[top];

    top--;

    return tmp;

}


float evalpostfixexpr(char *exp)

{

    int i=0;

    float op1, op2, value;

    while(exp[i]!='\0')

    {

        if(isdigit(exp[i]))

        {

            push((float)(exp[i]-'0'));

        }

        else

        {

            op2=pop();

            op1=pop();

            switch(exp[i])

            {

            case '+':

                value=op1+op2;

                break;

             case '-':

                value=op1-op2;

                break;

             case '*':

                value=op1*op2;

                break;

             case '/':

                value=op1/op2;

                break;

             case '%':

                value=(int)op1%(int)op2;

                break;

            }

            push(value);

        }

        i++;

    }

    return(pop());

}


void main() {

    float val;

    char exp[MAX];

    printf("Enter Any Postfix Expression:");

    gets(exp);

    printf("\nValue of the Postfix Expression is:");

    val=evalpostfixexpr(exp);

    printf("%f",val);

}

/* 3. Write a program to implement conversion of infix expression into postfix expression (parentheses and non parentheses). */


#include<stdio.h>

#include<ctype.h>

#define MAX 100


char st[MAX];

int top=-1;


void display()

{

    int i;

    for(i=0;i<=top;i++)

    {

        printf("%c",st[i]);

    }

}


void push(char val)

{

    if(top==MAX-1)

    {

        printf("\nStack Overflow:");

        return;

    }

    top++;

        st[top]=val;

}


char pop()

{

    char tmp;

    if(top==-1)

    {

        printf("\nStack Underflow:");

        return '0';

    }

    tmp=st[top];

    top--;

    return tmp;

}


int getPriority(char op)

{

    if(op=='^')

        return 3;

    else if(op=='/' || op =='*' )

        return 2;

    else if(op=='%')

        return 1;

    else

        return 0;

}


void infixtopostfix(char *source, char *target)

{

    int i=0,j=0;

    char tmp;

    while(source[i] != '\0')

    {

        if(source[i]=='(')

        {

            push(source[i]);

            i++;

        }

        else if(source[i]==')')

        {

            while((top==-1) || st[top]!='(')

            {

                target[j]=pop();

                j++;

            }

            if(top==-1)

            {

                printf("\nInvalid Expression:");

                return;

            }

            tmp=pop(); //Removing left parenthesis

            i++;

        }

        else if(isdigit(source[i]) || isalpha(source[i]))

        {

            target[j]=source[i];

            j++;

            i++;

        }

        else if(source[i]=='+' || source[i]=='-' || source[i]=='*' || source[i]=='%' || source[i]=='/')

        {

            while((top!=-1) && (st[top] != '(') && (getPriority(st[top]) >= getPriority(source[i])))

            {

                target[j]=pop();

                j++;

            }

            push(source[i]);

            i++;

        }

        else

        {

            printf("\nInvalid Expression:");

            return;

        }

    }

    while((top != -1) && (st[top]!='('))

    {

        target[j]=pop();

        j++;

    }

    target[j]='\0';

}


void main()

{

    char infix[MAX], postfix[MAX];


    printf("Enter Any Infix Expression:");

    gets(infix);


    infixtopostfix(infix, postfix);


    printf("\nCorresponding postfix expression is:");

    puts(postfix);

}



/* 4. Write a program to implement recursion. */



/* 5. Write a program to reverse the string using the stack */


#include<stdio.h>

#include<string.h>

#include<conio.h>

#define MAX 10


int stack[MAX];

int top=-1;

 void push (char val)

 {

     if ( top==MAX-1)

     {

         printf("\nStack is overflow:");

         return ;

     }

     top++;

     stack[top]=val;

 }

 char pop()

 {

     char temp;

     if( top==-1)

       return ' ';

else

{

    temp=stack[top];

    top--;

    return temp;


}


 }


 int main()

 {

     int i;

     char name[10],ch;


     printf("\nEnter your name :");

     scanf("%s",name);


     for (i=0; name[i]!='\0';i++)

     {

         push(name[i]);

     }



printf("\nReverse String is :");

while(top!= -1)

{

    printf("%c",pop());

}