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 |
how to hack email id & passward
how to find out the name of the users who are currently working starting with the same characters in unix os
Hi, what is the meaning of DOCUMENTUM,how and what testers have to do with it?
1.orders for a computer are summarized by the additional features and are requested as follows; Proportion of order No features 0.3 One feature only 0.5 More than one feature 0.2 find; 1. what is the probability that an order requests at least one feature? 2. what is the probability that an order does not request more than one feature?
is there increase in the pressure of water or the density of water if we go deep into the ocean.(but the density of water is 1000kg/cubic meter)?
Explain Belady's Anomaly.
defference between menu and context menu in vb.net?
What is success factors online trainings?
What are the advantages and disadvantages of Automicity, Consistency,Isolation & Durability?
what is the print server
Can anyone send me iocl written test paper fro computer engineers
what are the various technique used for inter process communication?