Write a nonrecursive routine to reverse a singly linked
list in O(N) time.
Answer Posted / hasan ali mirza
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int preceed(char c);
void push(char c);
char pop();
char stk[30];
int tos=-1;
int main()
{
int i,j=0,n,u,v;
char infix[30],postfix[30],c;
printf("Enter the infix expression:");
scanf("%s",&infix);
n=strlen(infix);
for(i=0;i<n;i++)
{
if((infix[i]>='a'&&infix[i]<='z')||(infix[i]>='A'&&infix[i]<='Z'))
{
postfix[j]=infix[i];
j++;
}
else if(infix[i]=='^'||infix[i]=='*'||infix[i]=='/'||infix[i]=='+'||infix[i]=='-')
{
u=preceed(stk[tos]);
v=preceed(infix[i]);
while(u>=v&&stk[tos]!='(')
{
postfix[j]=pop();
j++;
u=preceed(stk[tos]);
}
push(infix[i]);
}
else if(infix[i]=='(')
{
push(infix[i]);
}
else if(infix[i]==')')
{
c=pop();
while(c!='(')
{
postfix[j]=c;
j++;
c=pop();
}
}
else
{
printf("Equation has error\n");
exit(0);
}
}
while(tos!=-1)
{
postfix[j]=pop();
j++;
}
postfix[j]=='\0';
printf("The equation in postfix:%s",postfix);
}
void push(char c)
{
tos++;
stk[tos]=c;
}
char pop()
{
char val;
val=stk[tos];
tos--;
return(val);
}
int preceed(char c)
{
int v;
switch(c)
{
case '^':v=3;
break;
case '*':
case '/':v=2;
break;
case '+':
case '-':v=1;
break;
default:v=0;
break;
}
return(v);
}
| Is This Answer Correct ? | 2 Yes | 2 No |
Post New Answer View All Answers
Are bridges more stable on concrete or on soil - why?
synchronous and asynchronous transmission
can u give me the information about the questions asked by the bally in campus
consiteunts of indian railways
What is the use of generics? when was it added to jdk?
how can i write a program in c of SPARCE MATRIX(data structure)?
what is compiler
PREVIOUS AND NEW companies positive and negative points AFTER LIVING
how do we calculate physical address if logical address is given in the question?
How will a tester be sure that He’s covered the entire functionality in his Test case and not missed out anything?
when there is a parametrized constructor, and an object is created with no arguments. will the default constructor be called?
why one should join accenture?
5. Can you work well under deadlines or pressure?
which topics are come from quizs
char *a[2]; int const *p; int *const p; struct new { int a;int b; *var[5] (struct new)} Describe the statements in the above given construct ?