[go: up one dir, main page]

0% found this document useful (0 votes)
27 views14 pages

Assignment 6

Uploaded by

Naman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

Assignment 6

Uploaded by

Naman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structures in C

ASSIGNMENT-6

Name: Naman Kumar

Course: BTECH(CSE)

Section: B1

Class Roll No.: 45

University Roll No.: 2319143


Question-1: Postfix evaluation
#include<stdio.h>
#include<string.h>
int SIZE=10;
int eval(int a ,int b,char ch)
{
switch(ch)
{
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
}
}
void push(int arr[], int *top, int x)
{
if (*top == SIZE - 1)
{
printf("Stack is full");
}
else
{
arr[++(*top)] = x;
}
}

int pop(int arr[], int *top)


{
if (*top == -1)
{
printf("Stack is empty");
return 0;
}
else
{
return arr[(*top)--];
}
}
int main()
{
char s[]= "17-6+11";
int stack[SIZE];
int top=-1;
int i=0;
int n=strlen(s);
while(i<n)
{
if(s[i]>='0'&&s[i]<='9')
{
s[i]=s[i]-'0';
push(stack,&top,s[i]);
}
else
{
int b=pop(stack,&top);
int a=pop(stack,&top);
int res=eval(a,b,s[i]);
push(stack,&top,res);

}
i++;
}
printf("%d",stack[top]);
return 0;
}
Question-2: Infix to prefix
#include<iostream>
#include<string.h>
using namespace std;
int SIZE=20;
int pre(char c)
{
switch(c)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default: return 0;
}
}

void push(char arr[], int *top, char x)


{
if (*top == SIZE - 1)
{
cout << "Stack is full" << endl;
}
else {
arr[++(*top)] = x;
}
}

char pop(char arr[], int *top)


{

if (*top == -1)
{
cout << "Stack is empty" << endl;
return 0;
}
else
{
return arr[(*top)--];
}
}
void reversestring(char str[],int n)
{
int start=0;
int end=n-1;
while(start<end)
{
swap(str[start],str[end]);
start++;
end--;
}

}
int main()
{
char infix[SIZE],prefix[SIZE];
char stack[SIZE];
int top=-1;
int j=0;
cout<<"Enter an infix ";
cin>>infix;
int len=strlen(infix);
reversestring(infix,len);
for(int i=0;i<len;i++)
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
}
for(int i=0;i<len;i++)
{
char c=infix[i];
if(c>='a'&&c<='z')
{
prefix[j]=c;
j++;
}
else if(c=='(')
{
push(stack,&top,c);
}
else if(c==')'){
while(top!=-1&& stack[top]!='(')
{
prefix[j]=pop(stack,&top);
j++;
}
pop(stack,&top);
}
else{
while(top!=-1&&pre(stack[top])>=pre(c))
{
prefix[j]=pop(stack,&top);
j++;
}
push(stack,&top,c);
}
}
while(top!=-1){
prefix[j]=pop(stack,&top);
j++;
}
prefix[j]='\0';

reversestring(prefix,j);
cout<<"Prefix: "<<prefix<<endl;
return 0;
}
Question-3: Postfix to prefix
#include<stdio.h>
#include<string.h>
int SIZE=20;
void push(char arr[][SIZE],int *top,char* x)
{
if(*top==SIZE-1)
{
printf("Stack is full");
}
else{
strcpy(arr[++(*top)],x);
}
}
char* pop(char arr[][SIZE], int *top)
{
if (*top == -1)
{
printf("Stack is empty\n");
return NULL;
}
else
{
return arr[(*top)--];
}
}

int isoperator(char c)
{
return(c=='+'||c=='-'||c=='*'||c=='/');
}
int main()
{
char postfix[SIZE],stack[SIZE][SIZE];
int top=-1;
printf("Postfix expression");
scanf("%s",postfix);
int len=strlen(postfix);
for(int i=0;i<len;i++){
if(!isoperator(postfix[i]))
{
char oper[2]={postfix[i],'\0'};
push(stack,&top,oper);
}
else
{
char oper1[SIZE],oper2[SIZE],result[SIZE];
int j=0;
char *temp1 = pop(stack, &top);
char *temp2 = pop(stack, &top);
strcpy(oper1, temp1);
strcpy(oper2, temp2);
result[0] = postfix[i];
result[1] = '\0';
strcat(result, oper2);
strcat(result, oper1);
push(stack, &top, result);
}
}
printf("Prefix expression is:%s",(pop(stack,&top)));
return 0;
}
Question-4: Prefix to postfix
#include <stdio.h>
#include <string.h>
int SIZE=100;
void push(char stack[SIZE][SIZE], int *top, const char *value)
{
if (*top >= SIZE - 1)
{
printf("Stack is full\n");
return;
}
strcpy(stack[++(*top)], value);
}

void pop(char stack[SIZE][SIZE], int *top, char *value)


{
if (*top == -1)
{
printf("Stack is empty\n");
return;
}
strcpy(value, stack[(*top)--]);
}
int isOperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int main()
{
char prefix[SIZE];
printf("Enter a prefix expression:");
scanf("%s",prefix);
char stack[SIZE][SIZE];
int top = -1;
char postfix[SIZE];
int length = strlen(prefix);
for (int i = length - 1; i >= 0; i--) \
{
if (isOperator(prefix[i]))
{
char oper1[SIZE],oper2[SIZE],temp[SIZE];
pop(stack,&top,oper1);
pop(stack,&top,oper2);
temp[0] = '\0';
strcat(temp,oper1);
strcat(temp,oper2);
int len = strlen(temp);
temp[len] = prefix[i];
temp[len + 1] = '\0';
push(stack,&top,temp);
}
else
{
char operand[2] = { prefix[i], '\0' };
push(stack,&top,operand);
}
}
strcpy(postfix,stack[top]);
printf("Postfix: %s", postfix);
return 0;
}
Question-5: Postfix to Infix
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 20
void push(char stack[SIZE][SIZE], int *top, const char *value)
{
if (*top >= SIZE - 1)
{
printf("Stack is full\n");
return;
}
strcpy(stack[++(*top)], value);
}
void pop(char stack[SIZE][SIZE], int *top, char *value)
{
if (*top == -1)
{
printf("Stack is empty\n");
return;
}
strcpy(value, stack[(*top)--]);
}
int isoperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int main()
{
char postfix[SIZE]="ab*c+";
char stack[SIZE][SIZE];
int top=-1;
char *value;
char op1[SIZE],op2[SIZE],result[SIZE];
int i=0;
while(postfix[i]!='\0'){
if(isalnum(postfix[i]))
{
char operand[2];
operand[0]=postfix[i];
operand[1]='\0';
push(stack,&top,operand);
}
else if(isoperator(postfix[i]))
{
pop(stack,&top,op2);
pop(stack,&top,op1);
result[0]='(';
strcpy(result+1,op1);
int len=strlen(result);
result[len]=' ';
result[len+1]=postfix[i];
result[len+2]=' ';
strcpy(result+len+3,op2);
result[len+3+strlen(op2)]=')';
result[len+3+strlen(op2)+1]='\0';
push(stack,&top,result);
}
i++;
}
printf("Infix is :%s",stack[top]);
return 0;
}
Question-6: Prefix to infix
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int SIZE=10;
void push(char stack[SIZE][SIZE], int *top, const char *value)
{
if (*top >= SIZE - 1)
{
printf("Stack is full\n");
return;
}
strcpy(stack[++(*top)], value);
}

void pop(char stack[SIZE][SIZE], int *top, char *value)


{
if (*top == -1)
{
printf("Stack is empty\n");
return;
}
strcpy(value, stack[(*top)--]);
}
int isoperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int main()
{
char prefix[SIZE]=" *-A/BC-/AKL";

char stack[SIZE][SIZE];
int top=-1;
char *value;
char op1[SIZE],op2[SIZE],result[SIZE];
int i=strlen(prefix)-1;
while(i>=0)
{
if(isalnum(prefix[i])){
char operand[2];
operand[0] = prefix[i];
operand[1] = '\0';
push(stack, &top, operand);
}
else if(isoperator(prefix[i]))
{
pop(stack,&top,op1);
pop(stack,&top,op2);
result[0]='(';
strcpy(result+1,op1);
int len=strlen(result);
result[len]=' ';
result[len+1]=prefix[i];
result[len+2]=' ';
strcpy(result+len+3,op2);
result[len + 3 + strlen(op2)] = ')';
result[len + 3 + strlen(op2) + 1] = '\0';
push(stack, &top, result);
}
i--;
}
printf("infix is :%s",stack[top]);
return 0;
}

You might also like