Ds Lab Assignment 8
Ds Lab Assignment 8
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
scanf("%d",&n);
while(choice != 4)
printf("\n1.Push\n2.Pop\n3,show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
{
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting....");
break;
default:
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
top = top +1;
stack[top] = val;
void pop ()
if(top == -1)
printf("Underflow");
else
void show()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
1.Push
2.Pop
3,show
4.Exit
1.Push
2.Pop
3,show
4.Exit
2.Pop
3,show
4.Exit
1.Push
2.Pop
3,show
4.Exit
1.Push
2.Pop
3,show
4.Exit
43
1.Push
2.Pop
3,show
4.Exit
Exiting....
Q2.
Write a program to evaluate a Postfix expression.
// mention all your assumptions and limitations of your program
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
# define MAXCOL 80
struct stack {
int top;
int items[MAXCOL];
};
if(ps->top == -1)
return true;
else
return false;
}
int pop(struct stack *ps){
if(empty(ps)){
exit(1);
return(ps->items[ps->top--]);
if(ps->top == MAXCOL-1){
exit(1);
ps->items[++ps->top] = x;
switch(symb){
exit(1);
}
float eval(char expr[]){
char c;
int position;
opndstk.top= -1;
if(isDigit(c))
push(&opndstk, (float)(c-'0'));
else {
op2 = pop(&opndstk);
op1 = pop(&opndstk);
push(&opndstk, value);
return (pop(&opndstk));
int main()
printf("enter expression\n");
char expr[MAXCOL];
expr[--position] = '\0';
printf("%s %s\n", "the original postfix expression is:", expr);
return 0;
623+-382/+*2$3+
Q3.
Write a program to convert an expression from infix to Postfix.
// mention all your assumptions and limitations of your program
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
# define MAXCOL 80
struct stack {
int top;
int items[MAXCOL];
};
return true;
else
return false;
if(empty(ps)){
exit(1);
return(ps->items[ps->top--]);
if(ps->top == MAXCOL-1){
exit(1);
ps->items[++ps->top] = x;
push(ps, item);
return item;
return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
}
switch(op1){
case '$':
case '/':
case '/':
case '*':
case '+':
case '-':
return true;
case '$':
return false;
case '+':
case '+':
case '-':
return true;
case '$':
case '/':
case '*':
return false;
}
float postfix(char infix[], char postr[]){
int position;
int outpos = 0;
char topsymb;
char symb;
opstk.top = -1;
if(isOperand(symb))
postr[outpos++] = symb;
else {
topsymb = pop(&opstk);
postr[outpos++] = topsymb;
push(&opstk, symb);
while(!empty(&opstk))
postr[outpos++] = pop(&opstk);
postr[outpos] ='\0';
int main()
printf("Enter expression\n");
char infix[MAXCOL];
char postr[MAXCOL];
int pos = 0;
while((infix[pos++] = getchar()) != '\n');
infix[--pos] = '\0';
postfix(infix, postr);
return 0;
A+B*C-D