program to Reverse a linked list
Answers were Sorted based on User's Feedback
Answer / shruti
reverse a linked list -> by creating a new list.
consider home as the stationary pointer of the original list
adn start as the stationary pointer of the new linked list..
the node structure is as follows:
struct node
{
int data;
struct node * next;
}
struct node * reverse(struct node *home , struct node *
start)
temp = home;
while(temp != NULL)
{
p = (struct node *) malloc (sizeof(struct node));
p -> data = temp -> data;
p -> next = NULL;
if(start == NULL)
start = p;
else
{
p -> next = start;
start = p;
}
temp = temp -> next;
return start;
}
Is This Answer Correct ? | 42 Yes | 16 No |
Answer / gaurav jain
void reverse(struct node **kbr)
{
struct node *temp,*p,*q;
temp=*kbr;
p=temp->next;
q=temp;
while(p!=NULL)
{
temp=p;
p=p->next;
temp->next=q;
q=temp;
}
(*kbr)->next=NULL;
*kbr=q;
}
Is This Answer Correct ? | 24 Yes | 16 No |
Answer / ashish rajta
void reverse(list *l)
{
lnode *temp;
temp=l->first;
while(temp->next != NULL)
{
temp=temp->next;
}
while(temp !=NULL)
{
printf("%d",temp->info);
temp=temp->prev;
}
}
Is This Answer Correct ? | 15 Yes | 12 No |
Answer / ajay george
void rev(node *nPtr, node *prevPtr)
{
if(nPtr->next==NULL)
{
head=nPtr;
}
else
{
rev(nPtr->next, nPtr);
}
nPtr->next=prevPtr;
prevPtr->next=NULL;
}
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / mbm
/* Reverse linked list by recursion */
if(head)
head = _ReverseLinkedList(NULL, head, head->next);
NodeStr *_ReverseLinkedList(
NodeStr *preNode,
NodeStr *node1,
NodeStr *node2
)
{
NodeStr *next_node;
if(!node2)
return node1;
next_node = node2->next;
node2->next = node1;
node1->next = preNode;
return _ReverseLinkedList(node1, node2, next_node);
}
Is This Answer Correct ? | 11 Yes | 11 No |
Answer / raj
void linkreverse()
{
node *p=head,*q=head->next,*r=q->next,*temp;
p->next=NULL;
temp=p;
while(p!=tail)
{
q->next=p;
p=q;
if(r!=NULL)
{
q=r;
r=q->next;
}
}
head=p;
tail=temp;
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / cleonjoys
void reverse(Node_t *nPtr, Node_t *prevPtr){
if(nPtr->next != NULL){
reverse(nPtr->next, nPtr);
}
else{
headp = nPtr; //header is a global pointer, i know
keeping header is security hole.
}
nPtr->next = prevPtr;
}
Is This Answer Correct ? | 7 Yes | 8 No |
Answer / bragaadeesh
Program to reverse a singly list ITERATIVELY,
http://www.technicalypto.com/2010/01/java-program-to-reverse-singly-linked.html
Program to reverse a linked list RECURSIVELY
http://www.technicalypto.com/2010/03/reverse-singly-linked-list-recursively.html
Is This Answer Correct ? | 4 Yes | 7 No |
Answer / don
int rec_rev(node *p) {
node *last_prev,*last,*temp,*q;
if (p->next==NULL)
return 1;
else {
last_prev=find_last(); // finds last but 1 element
last=last_prev->next;
last_prev->next==NULL;
if(p==head) {
temp=head;
head=last;
head->next=temp;
p=temp;
rec_rev(p);
}
else {
q=find_prev(p);
last->next=q->next;
q->next=last;
q->next->next=p;
rec_rev(p);
}
}
]
Is This Answer Correct ? | 3 Yes | 7 No |
3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.
void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(“%d”,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(“%d”,*cptr); }
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.
void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }
void main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }
source code for delete data in array for c
What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }
main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); }
There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }
void main() { static int i=5; if(--i){ main(); printf("%d ",i); } }
main() { while (strcmp(“some”,”some\0”)) printf(“Strings are not equal\n”); }
main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }