Link list in reverse order.

Answers were Sorted based on User's Feedback



Link list in reverse order...

Answer / shruti

/*
the structure is as follows:
struct node
{
int data;
struct node *next;
}
*/

struct node * reverse (struct node *home , struct node *rev)
{
struct node temp , *p;

if(home != NULL)
{
temp = home;
while(temp != NULL)
{
//this part will create a new node with the name p.
p = myalloc;
p -> data = temp -> data;
p -> next = NULL;

if(rev == NULL)
rev = p;
else
{
p -> next = rev;
rev = p;
}
temp = temp -> next;
}
}
return rev;
}

Is This Answer Correct ?    5 Yes 0 No

Link list in reverse order...

Answer / lijun li

#include <stdio.h>

typedef struct list
{
int data;
struct list * next;
} LIST;

LIST* reverse(LIST *head)
{
LIST* temp;
LIST* temp1;

if (head == NULL || head->next == NULL)
return head;
else
{
temp = head->next;
head->next = NULL;
while(temp)
{
temp1 = temp->next;
temp->next = head;
head = temp;
temp = temp1;
}

return head;
}
}

int main(int argc, char ** argv)
{
int i=0;
LIST* head = NULL;
LIST* node = NULL;
int count;

if (argc < 2) printf("usage: a.out <count>");
else count = atoi(argv[1]);

for (i=0;i<count;i++)
{
node = (LIST*)calloc(sizeof(LIST));
node->data = i;
node->next = head;

head = node;
}

node = head;
while(node)
{
printf("before %d\n", node->data);
node = node->next;
}

head = reverse(head);
printf("after head=%d\n", head->data);
node = head;
while(node)
{
printf("after %d\n", node->data);
node = node->next;
}
}

Is This Answer Correct ?    6 Yes 1 No

Link list in reverse order...

Answer / shruti

**Liked list as a stack = linked list in reverse order.

/*
the structure is as follows:
struct node
{
int data;
struct node *next;
}
*/

struct node * reverse (struct node *home , struct node *rev)
{
struct node temp , *p;

if(home != NULL)
{
temp = home;
while(temp != NULL)
{
//this part will create a new node with the name p.
p = myalloc;
p -> data = temp -> data;
p -> next = NULL;

if(rev == NULL)
rev = p;
else
{
p -> next = rev;
rev = p;
}
temp = temp -> next;
}
}
return rev;
}

Is This Answer Correct ?    2 Yes 0 No

Link list in reverse order...

Answer / shruti

//Go through the code, incase any issues feel free to
revert.

Copying a link list:

//home is the stationary pointer of the main linked list
which is to be copied.

//head is the stationary pointer of the new linked list
which has to be created..

//temp and temp1 are the moving pointers attached to the
respective linked list...


struct node *copy(struct node *home , struct node *head)
{
struct node *temp , *temp1 , *p;

temp = home;
while(temp != NULL)
{
p = (struct node *) malloc (sizeof(struct node));
p -> data = temp -> data;

if(head == NULL)
head = p;

else
{
temp1 = head;
while(temp1 -> next != NULL)
temp1 = temp1 -> next;

temp1 -> next = p;
}
temp = temp -> next;
}
return head;
}

Is This Answer Correct ?    1 Yes 0 No

Link list in reverse order...

Answer / ashitosh

node *reverse(node *head)
{ node *p,*q,*r;
p=NULL;
q=head;
r=q->next;
while(q!=NULL)
{
q->next=p;
p=q;
q=r;
if(q!=NULL)
r=q->next;
}
return(p);
}

Is This Answer Correct ?    1 Yes 0 No

Link list in reverse order...

Answer / raghuram.a

node *reverse(node *first)


{
node *cur,*temp;


cur=NULL;

while(first!=NULL)


{temp=first;

first=first->link;

temp->link=cur;

cur=temp;
}

return cur;

}

Is This Answer Correct ?    5 Yes 5 No

Link list in reverse order...

Answer / aqib

write a link list program to insert integer number and then
display its sum as well. Your program should display the
address as well as the result
write a program to reverse a link list so that the last
element becomes the first one and so on
copy one link list to another list

Is This Answer Correct ?    0 Yes 2 No

Link list in reverse order...

Answer / sameera.adusumilli

recursive reverse(ptr)
if(ptr->next==NULL)
return ptr;
temp=reverse(ptr->next);
ptr=ptr->next;
return ptr;
end

Is This Answer Correct ?    3 Yes 8 No

Post New Answer

More C Code Interview Questions

#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }

1 Answers  


Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,


You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.

3 Answers  


main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(ā€œ%dā€ ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(ā€œ%d ā€ ,*p); p++; } }

1 Answers  


Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,






main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


main() { main(); }

1 Answers  


program to find magic aquare using array

4 Answers   HCL,


Give a oneline C expression to test whether a number is a power of 2?

25 Answers   EA Electronic Arts, Google, Motorola,


void main() { int i=5; printf("%d",i++ + ++i); }

3 Answers  


why is printf("%d %d %d",i++,--i,i--);

4 Answers   Apple, Cynity, TCS,


Categories