Write a nonrecursive routine to reverse a singly linked
list in O(N) time.
Answers were Sorted based on User's Feedback
Answer / sandeep
node * reverse(node * list)
{
node *p, *q, *r;
p = list;
q = p->next;
while(q->next != NULL)
{
q = p->next;
r = q->next;
p->next = r;
q->next = p;
p = p->next;
}
q->next = p;
p->next = NULL;
return q`;
}
Is This Answer Correct ? | 26 Yes | 7 No |
Answer / hasan ali mirza
list reverse(list L)
{
stack S;
position Lpos = first(L);
while(Lpos->element != NULL)
{
push(Lpos->element, S);
Lpos = Lpos->next;
}
makeEmpty(L);
Lpos = first(L);
while(isempty(S) == FALSE)
{
insert(pop(S), Lpos);
Lpos = Lpos->next;
}
return L;
}
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / 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 |
for us visa interiew which file i have to carry(for example :folder file,gip file or other).please give me the answer .becoze the us consulate asks my documents, tommorow morning my interview so please grant me answer
1)what to do in emergency situation of having total blackout (on a ship) 2)what to do in emergency situation of flooding in the engine room 3)under what conditions would engine room bilges be pumped overboard? 4)why is high pressure required for the injection of fuel into the cylinder of diesel engine. 5)why is doubled walled pipes employed for high pressure fuel lines? 6)reasons for overheating of main engine 7)how to detect choked fuel valve,leaky piston rings and their remedies during operation in diesel engine? 8)effects of insufficient or excessive cylinder lubrication? 9)causes of a diesel engine piston overheating.
what in 4th and 5th normal form ??
what is recursion in c language?
hi all I got two exam call letter one from maha ganeco & other from federal bank both at same time & same date which should i choose (note I am eng. grad.)
Anyone have thesis paper about "Electrical Load Forecasting" . Or the web addresses where people usually upload their thesis paper except IEEE.
why dijkstra algorithm not used for negative weighted edges graph??
Write the fetch cycle and execute cycle for following instructions:JMPNZ (jump to the given address if the accumulator not equal to zero) RET(return from a subroutine) ADB (add the contents of register B to the accumulator and save result in the accumulator).
how to generate linked implementation of sparse matrix?
project plan for bug tracking system?
which is your dream company and why?
Can you connect Active Directory to other 3rd-party Directory Services? Name a few options.