Docsity

Evaluating Postfix Expressions and Adding Polynomials Using Stacks and Linked Lists in C, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
A. A postfix expression of the formab+cd-*ab/ is to be evaluated
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];
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Evaluating Postfix Expressions and Adding Polynomials Using Stacks and Linked Lists in C and more Assignments Data Structures and Algorithms in PDF only on Docsity!

A. A postfix expression of the formab+cd-*ab/ is to be evaluated

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:

B. Write a program that adds two single variablepolynomials.

Each polynomial should be represented as a list with

linked list implementation.

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); }