Important Question Of Stack.

Important Question Of Stack.



Qn1. What condition is represents a stack is empty in array and link list ?

Ans:- Stack Underflow condition is represents a stack is empty in array and link list .

Qn2. What condition it represents a stack is full in array and link list ?

Ans.  Stack overflow condition is represents a stack is empty in array and link list .

Qn3. What is the time complexity to push and pop elements in stack ?

Ans The time complexity to push and pop elements in stack is O(1).

Qn4. Write a c program to push and pop elements in a stack ?

Ans:-#include<stdio.h>
int x[5];
int i=-1;
void push (int );
int pop();
int full();
int empty();
main()
{
        int data;
        int n;
        do
        {
                printf("\n1 for push");
                printf("\n2 for pop");
                printf("\n0 for stop");
                printf("\nEnter choice :  ");
                scanf("%d",&n);
                if(n==1)
                {
                        if (full())
                        {
                                printf("stack is full !\n");
                                continue;
                        }
                        printf("enter data to push : ");
                        scanf("%d",&data);
                        push (data);
                }
                else
                if(n==2)
                {
                        if(empty())
                        {
                                printf("Stack is empty !\n");
                                continue;
                        }
                        data=pop();
                        printf("%d\n",data);
                }
        }while(n!=0);
}
void push(int data)
{
        i++;
        x[i]=data;
}
int pop()
{
        int n=x[i];
        i--;
        return n;
}
int full()
{
        if(i>4)
        {
                return 1;
        }
        else
                return 0;
}
int empty()
{
        if(i==-1)
                return 1;
        else
                return 0;
}

output:-1 for push
2 for pop
0 for stop
Enter choice :  1

enter data to push : 10

1 for push
2 for pop
0 for stop
Enter choice :  1

enter data to push : 11


1 for push
2 for pop
0 for stop
Enter choice :  2

11

Qn5. Write a c program to convert  a number decimal to binary ?

Ans:-#include<stdio.h>
#define MAX 10

typedef struct stack
{
        int data[MAX];
        int top;
}stack;

int empty(stack *s)
{
        if(s->top==-1)
                return(1);
        return(0);
}

int full (stack *s)
{
        if(s->top==MAX-1)
                return(1);
        return(0);
}

void push(stack *s,int x)
{
        s->top=s->top+1;
        s->data[s->top]=x;
}

int pop(stack *s)
{
        int x;
        x=s->data[s->top];
        s->top=s->top-1;
        return (x);
}

void main()
{
        stack s;
        int num;

        s.top=-1;
                printf("enter decimal number:");
                scanf("%d",&num);

                while(num!=0)
                {
                        if(!full(&s))
                                {
                                        push(&s,num%2);
                                        num=num/2;
                                }
                        else
                                {
                                        printf("nstack overflow");
                                        exit(0);
                                }
                }
        while(!empty(&s))
                {
                        num=pop(&s);
                        printf("%d",num);
                }
}

Output:-enter decimal number:55

110111

Qn6. Write a program to check the string"([]({()}[()]))" is balanced or not ?

Ans:-#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
#define MAX 25
 
int top = -1;
int stack[MAX];
 
void push(char item)
{
      if(top == (MAX - 1))
      {
            printf("Stack is Full\n");
            return;
      }
      top++;
      stack[top] = item;
}
 
char pop()
{
      if(top == -1)
      {
            printf("Stack is Empty\n");
            exit(1);
      }
      return(stack[top--]);
}
 
int match_paranthesis(char a, char b)
{
      if(a == '[' && b == ']')
      {
            return 1;
      }
      else if(a == '{' && b == '}')
      {
            return 1;
      }
      else if(a == '(' && b == ')')
      {
            return 1;
      }
      return 0;
}
 
int check_paranthesis(char expression[])
{
      int count;
      char temp;
      for(count = 0; count < strlen(expression); count++)
      {
            if(expression[count] == '(' || expression[count] == '{' || expression[count] == '[')
            {
                  push(expression[count]);
            }
            if(expression[count] == ')' || expression[count] == '}' || expression[count] == ']')
            {
                  if(top == -1)
                  {
                        printf("The Right Parentheses are more than the Left Parentheses\n");
                        return 0;
                  }
                  else
                  {
                        temp = pop();
                        if(!match_paranthesis(temp, expression[count]))
                        {
                              printf("The Mismatched Parentheses in the Expression are:\t%c and %c\n", temp, expression[count]);
                              return 0;
                        }
                  }
            }
      }
      if(top == -1)
      {
            printf("\nThe Expression has Balanced Parentheses\n");
            return 1;
      }
      else
      {
            printf("The Expression has unbalanced parentheses\n");
            return 0;
      }
}
int main()
{
      char expression[MAX];
      int validity;
      printf("\nEnter an Algebraic Expression:\t");
      scanf("%s", expression);
      validity = check_paranthesis(expression);
      if(validity == 1)
      {
            printf("The Expression is Valid\n");
      }
      else
      {
            printf("The Expression is Invalid\n");
      }
      return 0;
}


Output:-Enter an Algebraic Expression: {()}() The Expression has Balanced Parentheses The Expression is Valid

Qn7. Write a program to convert infix expression to postfix expression ?

Ans:- #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int pre(char );
main()
{
        char x[30];
        int i,j=-1,k=-1,a,b;
        char opd[30],opr[30];
        printf("Enter infix Expression : ");
        scanf("%s",x);
        for(i=0;i<strlen(x);i++)
        {
                if(x[i]>=48 && x[i]<=57)
                {
                        j++;
                        opd[j]=x[i];
                }
                else
                {
                        if(k==-1)
                        {
                                k++;
                                opr[k]=x[i];
                        }
                        else
                        {
                                a=pre(x[i]);
                                b=pre(opr[k]);
                                while(k>=0 && a<=b )
                                {
                                        j++;
                                        opd[j]=opr[k];
                                        k--;
                                        if(k==-1)
                                        {
                                                break;
                                        }
                                        b=pre(opr[k]);
                                }
                                k++;
                                opr[k]=x[i];
                        }
                }
        }
        while(k>=0)
        {
                j++;
                opd[j]=opr[k];
                k--;
        }
        for(i=0;i<=j;i++)
        {
                printf("%c",opd[i]);
        }
}
int pre(char m)
{
        switch(m)
        {
                case '+':
                case '-':
                        return 4;
                case '*':
                case '/':
                case '%':
                        return 5;
        }
}

Output:-  Enter infix Expression : (6+3-1)*(7-3+2) 

6(3+1-)*7(3-2+) postfix expression 

Qn8. Write a program to evaluate the postfix or prefix expression ?

Ans:-  Postfix Evaluation:-

#include<stdio.h>
#include<string.h>
main()
{
        char x[30];
        int i,j=-1,a,b,c;
        char stack[10];
        printf("Enter postfix Expression : ");
        scanf("%s",x);
        for(i=0;i<strlen(x);i++)
        {
                if(x[i]>=48 && x[i]<=57)
                {
                        j++;
                        stack[j]=x[i]-48;
                }
                else
                {
                        b=stack[j];
                        j--;
                        a=stack[j];
                        j--;
                        if(x[i]=='+')
                                c=a+b;
                        else if(x[i]=='-')
                                c=a-b;
                        else if(x[i]=='*')
                                c=a*b;
                        else if(x[i]=='/')
                                c=a/b;
                        j++;
                        stack[j]=c;
                }
        }
        printf("%d",stack[j]);
}

Output:- Enter postfix Expression : 593/+8+6-

10

Qn9. Which data structure used by tower of hanoi ?

Ans :- Stack data structure is used by tower of hanoi .

إرسال تعليق

0 تعليقات