





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






after accepting the values of a, b, c and d. The value should be accepted only once and the same value is to be used for repeated occurrence of same symbol in the expression. Formulate the problem and write a C program to solve the problem by using stack Ans: #include<stdio.h> #define MAXSIZE 50 typedef struct stack { int data[MAXSIZE]; int top; }STACK; void initstack(STACK *ps) { ps->top=-1; } void push(STACK *ps,int num) { ps->top++; ps->data[ps->top]=num; } int pop(STACK *ps) { int num; num=ps->data[ps->top]; ps->top--; return num; } int peek(STACK *ps) { return ps->data[ps->top]; }
int IsEmpty(STACK *ps) { if(ps->top==-1) return 1; else return 0; } int IsFull(STACK ps) { if(ps->top==MAXSIZE-1) return 1; else return 0; } #include<stdio.h> #include "stack.h" #include<math.h> int evaluate(char postfix[20]) { STACK s; int value,i,opnd1,opnd2; initstack(&s); for(i=0;postfix[i]!='\0';i++) { switch(postfix[i]) { case '+':opnd2=pop(&s); opnd1=pop(&s); push(&s,opnd1+opnd2); break; case '-':opnd2=pop(&s); opnd1=pop(&s); push(&s,opnd1-opnd2); break; case '':opnd2=pop(&s);
Output:
Ans: #include<stdio.h> #include<stdlib.h> #define POLY1 (POLY *)malloc(sizeof(POLY)) typedef struct node { int coef,exp; struct node *next; }POLY; void create(POLY *head) { POLY temp=head,newnode; int i,n; printf("\n how many nodes"); scanf("%d",&n); printf("enter the terms in descending order of power"); for(i=1;i<=n;i++) { newnode=POLY1; newnode->next=NULL; printf("\nEnter coeff and exponent"); scanf("%d%d",&newnode->coef,&newnode->exp); temp->next=newnode; temp=newnode; }
newnode->exp=t1->exp; newnode->coef=t1->coef+t2->coef; t1=t1->next; t2=t2->next; } t3->next=newnode; t3=newnode; } while(t1) { newnode=POLY1; newnode->next=NULL; newnode->exp=t1->exp; newnode->coef=t1->coef; t3->next=newnode; t3=newnode; t1=t1->next; } while(t2) { newnode=POLY1; newnode->next=NULL; newnode->exp=t2->exp; newnode->coef=t2->coef; t3->next=newnode; t3=newnode; t2=t2->next; } } void main() { POLY p1,p2,*p3; p1=POLY1;
p1->next=NULL; p2=POLY1; p2->next=NULL; p3=POLY1; p3->next=NULL; create(p1); display(p1); create(p2); display(p2); add(p1,p2,p3); printf("\n addition is"); display(p3); }