Link list in reverse order.
Answers were Sorted based on User's Feedback
/*
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 |
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 |
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 |
//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 |
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 |
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 |
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 |
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 |
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }
Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)
3 Answers Disney, Google, ZS Associates,
Write a C function to search a number in the given list of numbers. donot use printf and scanf
void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }
main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above
#define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); }
pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"
WAP to display 1,2,3,4,5........N
main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); }
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.
Is the following code legal? struct a { int x; struct a *b; }
write a c-program to find gcd using recursive functions