You are on page 1of 6

STACK ADT

1. Students of a Programming class arrive to submit assignments.


Their register numbers are stored in a LIFO list in the order in
which the assignments are submitted. Write a program using array
to display the register number of the ten students who submitted
first.

Register number of the ten students who submitted first will be at


the bottom of the LIFO list. Hence pop out the required number of
elements from the top so as to retrieve and display the first 10
students.
Low Level: Display the register number of the last submitted record
Middle Level: Display the register number of the ten students who submitted
first

Infix to Postfix conversion


#include <stdio.h>
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */

void push(char elem) { /* Function for PUSH operation */


s[++top] = elem;
}

char pop() { /* Function for POP operation */


return(s[top--]);
}

int pr(char elem) { /* Function for precedence */


switch(elem) {

case'(':
return 0;
case'+':
case'-':
return 1;
case'*':
case'/':
return 2;
}
}

main() { /* Main Program */


char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s", infx);
while((ch = infx[i++]) != '\0') {
if (ch == '(')
push(ch);
else if(isalnum(ch))
pofx[k++] = ch;
else if(ch == ')') {
while(s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
} else{ /* Operator */
while(pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while(top != -1) /* Pop from stack till empty */
pofx[k++] = pop();
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",
infx, pofx);
}

Infix To Prefix

#include <stdio.h>
#define SIZE 50 /* Size of Stack */
#include <string.h>
#include <ctype.h>
char s[SIZE];
int top = -1;/* Global declarations */

void push(char elem)


{/* Function for PUSH operation */
s[++top]=elem;
}

char pop()
{/* Function for POP operation */
return(s[top--]);

int pr(char elem)


{/* Function for precedence */
switch(elem)
{

case')':return 0;
break;
case'+':
case'-':return 1;
case'*':
case'/':return 2;

}
}

main()
{/* Main Program */
char infx[50],prfx[50],ch,elem;
int i=0,k=0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s",infx);

strrev(infx);
//printf("%s",infx);
while((ch=infx[i++])!='\0')
{
if(ch==')')
push(ch);
else
if(isalnum(ch))
prfx[k++]=ch;
else
if(ch=='(')
{
while( s[top]!=')')
prfx[k++]=pop();
elem=pop();/* Remove ) */
}
else
{/* Operator */
while(pr(s[top])>pr(ch))
prfx[k++]=pop();
push(ch);
}
}
while( top !=-1)/* Pop from stack till empty */
prfx[k++]=pop();
strrev(prfx);
//printf("%s",prfx);
strrev(infx);
printf("\n\nGiven Infix Expn: %s Prefix Expn: %s\
n",infx,prfx);
}

Postfix Evaluation
#include <stdio.h>
#include <ctype.h>
main()
{
float stack[50];
int i=0, top=-1;
int val;
char a[30],ch;
printf("enter the postfix expression");
scanf("%s", a);
while ((ch=a[i++])!='\0')
{
if( isalpha(ch))
{ printf("%c",ch);
printf(" enter the value");
scanf("%d", &val);
stack[++top]=val; }
else if(ch=='+' || ch =='-' || ch =='*' ||
ch =='/')
{
if(ch =='+')
{ printf("in =");
stack[top-1]=stack[top-1] +
stack[top];
top--;
}

if (ch =='-')
{
stack[top-1]=stack[top-1] -
stack[top];
top--;
}
if(ch =='*')
{
stack[top-1]=stack[top-1] *
stack[top];
top--;
}
if(ch =='/')
{
stack[top-1]=stack[top-1] /
stack[top];
top--;
}

}
}

printf("%f", stack[top]);
}

Prefix Evaluation

#include <stdio.h>
#include <ctype.h>
#include <string.h>
main()
{
float stack[50];
int i=0, top=-1;
int val;
char a[30],ch1;
printf("enter the prefix expresion");
scanf("%s", a);
strrev(a);
printf("%s",a);
while((ch1=a[i++])!='\0')
{printf("%c",ch1);
if( isalpha(ch1))

{
printf(" enter the value");
scanf("%d", &val);
stack[++top]=val;}
else if(ch1=='+' || ch1 =='-' || ch1 =='*' ||
ch1=='/')
{
if(ch1 =='+')
{
stack[top-1]=stack[top] +
stack[top-1];
top--;
}

if(ch1 =='-')
{
stack[top-1]=stack[top] +
stack[top-1];
top--;
}
if(ch1 =='*')
{
stack[top-1]=stack[top] +
stack[top-1];
top--;
}
if(ch1 =='/')
{
stack[top-1]=stack[top] +
stack[top-1];
top--;
}

}
}
printf("%f", stack[top]);
}

2.Most of the bugs in scientific and engineering applications are due


to improper usage of precedence order in arithmetic expressions.
Thus it is necessary to use an appropriate notation that would
evaluate the expression without taking into account the precedence
order and parenthesis.
a) Write a program to convert the given arithmetic expression into
i) Reverse Polish notation (postfix)
ii) Polish notation (prefix)
b) Evaluate the above notations with necessary input.

High Level: Develop a menu driven program to implement all conversions and
evaluations in a single program with different inputs

You might also like