0% found this document useful (0 votes)
54 views12 pages

Q1. Implement Stack Using Array. Solution

The document contains solutions to 5 questions related to data structures and algorithms. It implements: 1) A stack using an array 2) Conversion of infix to postfix notation using a stack 3) Conversion of infix to prefix notation using a stack 4) Evaluation of a postfix notation expression using a stack 5) Evaluation of a prefix notation expression using a stack For each question, the code solution is provided along with sample input/output.

Uploaded by

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

Q1. Implement Stack Using Array. Solution

The document contains solutions to 5 questions related to data structures and algorithms. It implements: 1) A stack using an array 2) Conversion of infix to postfix notation using a stack 3) Conversion of infix to prefix notation using a stack 4) Evaluation of a postfix notation expression using a stack 5) Evaluation of a prefix notation expression using a stack For each question, the code solution is provided along with sample input/output.

Uploaded by

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

Name-Alisha Anum Reg. no.

- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

Q1. Implement stack using array.


Solution:
//stack using array
#include<stdio.h>
#include<conio.h>
int main()
{
//Declaring stack array which can store maximum 5 elements
int stack[5];
int top=-1,item,choice,temp;
do
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
if(top==4)
printf("\nStack Overflow");
else
{
printf("\nEnter the item you wish to push");
scanf("%d",&item);
stack[++top]=item;
}
}
break;

case 2:
{
if(top==-1)
{
printf("\nStack Underflow");
}
else
{
printf("\nPopped item is: %d",stack[top]);
top=top-1;
}
}
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

break;

case 3:
if(top==-1)
printf("Stack is empty.Nothing to dislay");
else
{
temp=top;
while(temp>=0)
{
printf("\t %d",stack[temp]);
temp--;
}
}
break;
}//switch closes
}while(choice<4);
getch();
}

OUTPUT:

1.Push
2.Pop
3.Display
4.Exit
Enter your choice1

Enter the item you wish to push10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice1

Enter the item you wish to push20

1.Push
2.Pop
3.Display
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

4.Exit
Enter your choice3
20 10
1.Push
2.Pop
3.Display
4.Exit
Enter your choice2

Popped item is: 20


1.Push
2.Pop
3.Display
4.Exit
Enter your choice2

Popped item is: 10


1.Push
2.Pop
3.Display
4.Exit
Enter your choice2

Stack Underflow
1.Push
2.Pop
3.Display
4.Exit
Enter your choice3
Stack is empty.Nothing to dislay
1.Push
2.Pop
3.Display
4.Exit
Enter your choice4

--------------------------------
Process exited after 23.2 seconds with return value 13
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

Q.2 Write a program to convert infix to postfix notation.


Solution:
#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

OUTPUT:

Enter the expression : a+b/c*d

abc/d*+
--------------------------------
Process exited after 9.591 seconds with return value 0
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

Q.3 Write a program to convert infix to prefix notation.


Solution:
//3.program for convert infix to prefix notation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define size 50
char stack[size];
int top=-1;
void push (char item)
{
if(top>=size-1)
printf("\nStack Overflow!!\a\a\a");
else
{
top++;
stack[top]=item;
}//else
}//push
char pop()
{
char item;
item= stack[top];
top--;
return(item);
}
int is_operator(char symbol)
{
if(symbol=='^'||symbol=='*'||symbol=='/'||symbol=='+
'||symbol=='-')
return 1;
else
return 0;
}//is_operator
int precedence(char symbol)
{
if(symbol=='^')
return 3;
else if(symbol=='*'||symbol=='/')
return 2;
else if(symbol=='+'||symbol=='-')
return 1;
else
return 0;
}//precedence
int main()
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

{
char a[size],infix[size],postfix[size],item,temp;
int i=0,j=0;
printf("\nEnter Infix Notation::");
gets(infix);
strrev(infix);
while(infix[i]!='\0')
{
item=infix[i];
if (item==')')
{
push(item);
}
else if(item>='A'&&item<='Z'||item>='a'&&item<='z')
{
postfix[j]=item;
j++;
}
else if(is_operator(item)==1)
{
temp=pop();
while((is_operator(temp)==1)&&(precedence(temp)>=p
recedence(item)))
{
postfix[j]=temp;
j++;
temp=pop();
}//while
push(temp);
push(item);
}//else if
else if(item=='(')
{
temp=pop();
while(temp!=')')
{
postfix[j]=temp;
j++;
temp=pop();
}//while
}//else if
else
{
printf("\n Invalid arithmetic expression");
getch();
exit(0);
}//else
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

i++;
}//first while
while(top>-1)
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
strrev(postfix);
printf("\n Arithmetic expression In Prefix
Notation):\n");
puts(postfix);
getch();
return 0;
}//main.

Example:
Enter Infix Notation::a*b+c*d
Arithmetic expressions In Prefix Notation):+*ab*cd
--------------------------------
Process exited after 11.97 seconds with return value 0
Press any key to continue . . .
Enter Infix Notation::A*B+C*D
Arithmetic expression In Prefix Notation :)
+*AB*CD
--------------------------------
Process exited after 61.54 seconds with return value 0
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

Q4. Write a program to evaluate postfix notation.


Solution:
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

OUTPUT:

Enter the expression :: 523+*

The result of expression 523+* = 25

--------------------------------
Process exited after 7.959 seconds with return value 0
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

Q5. Write a program to evaluate prefix notation


Solution:
//Program to Evaluate Prefix Expression
#include<stdio.h>
#include<conio.h>
#include<string.h>

int s[50];
int top=0;

void push(int ch);


int pop();

int main()
{
int a,b,c,i;
char prefix[50];
clrscr();

printf("\nEnter the prefix string in figures(1 digit nos);");


gets(prefix);

//for(i=0;i<strlen(prefix);i++)
for(i=strlen(prefix)-1;i>=0;i--)
{
if(prefix[i]=='+')
{
c=pop()+pop();
push(c);
}
else if(prefix[i]=='-')
{
a=pop();
b=pop();
c=b-a;
push(c);
}
else if(prefix[i]=='*')
{ a=pop();
b=pop();
c=b*a;
push(c);
}
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002

else if(prefix[i]=='/')
{
a=pop();
b=pop();
c=b/a;
push(c);
}
else
{
push(prefix[i]-48);
//printf("\n INT=%d - CHAR=%d",prefix[i]-48,c);
}
}
printf("\nFinal ans = %d",pop());

getch();
return 0;
}

void push(int ch)


{
top++;
s[top]=ch;
}

int pop()
{
int ch;
ch=s[top];
top=top-1;
return(ch);
}

OUTPUT:

Enter the prefix string in figures(1 digit nos);*+245

Final ans = 30
--------------------------------
Process exited after 30.09 seconds with return value 0
Press any key to continue . . .

You might also like