|
INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C
DETAILS
Question: I want C program code for : Reverse the links of a linked list by traversing only once
Input: The input consists of the information in each node of the linked list.
Output: The program displays the information in the linked list after the links are reversed.
Sample Input: Enter the information in the linked list (Enter -1 to exit): 10 20 30 40 50 -1
Sample Output: After the links are reversed Information in the linked list: 50 40 30 20 10
Answer: #include <stdio.h> #include <malloc.h> #include <conio.h>
struct Node { int a; Node *next; };
Node* ReverseList( Node ** List );
void main() {
struct Node *root,*n,*p;
root=(Node*)malloc(sizeof(Node)); root->a=10; root->next=NULL; n=root; p=(Node*)malloc(sizeof(Node)); p->a=20; p->next=NULL; n->next=p; n=p; p=(Node*)malloc(sizeof(Node)); p->a=30; p->next=NULL; n->next=p; n=p; p=(Node*)malloc(sizeof(Node)); p->a=40; p->next=NULL; n->next=p; n=p; p=(Node*)malloc(sizeof(Node)); p->a=50; p->next=NULL; n->next=p; n=p; Node *rlist = ReverseList(&root); int i=1; while(rlist!=NULL){ printf("element %d =%dn",i++,rlist->a); rlist=rlist->next; } n=root; while(n!=NULL){ p=n->next; free(n); n=p; } }
Node* ReverseList( Node ** List ) {
Node *temp1 = *List;
Node * temp2 = NULL;
Node * temp3 = NULL;
while ( temp1 )
{
*List = temp1; //set the head to last node
temp2= temp1->next; // save the next ptr in temp2
temp1->next = temp3; // change next to privous
temp3 = temp1;
temp1 = temp2;
}
return *List;
}
|
|
|
Category |
C Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8138 users |
Added on |
7/17/2011 |
Views |
70166 |
Rate it! |
|
|
Question:
I want C program code for : Reverse the links of a linked list by traversing only once
Input: The input consists of the information in each node of the linked list.
Output: The program displays the information in the linked list after the links are reversed.
Sample Input: Enter the information in the linked list (Enter -1 to exit): 10 20 30 40 50 -1
Sample Output: After the links are reversed Information in the linked list: 50 40 30 20 10
Answer:
#include <stdio.h> #include <malloc.h> #include <conio.h>
struct Node { int a; Node *next; };
Node* ReverseList( Node ** List );
void main() {
struct Node *root,*n,*p;
root=(Node*)malloc(sizeof(Node)); root->a=10; root->next=NULL; n=root; p=(Node*)malloc(sizeof(Node)); p->a=20; p->next=NULL; n->next=p; n=p; p=(Node*)malloc(sizeof(Node)); p->a=30; p->next=NULL; n->next=p; n=p; p=(Node*)malloc(sizeof(Node)); p->a=40; p->next=NULL; n->next=p; n=p; p=(Node*)malloc(sizeof(Node)); p->a=50; p->next=NULL; n->next=p; n=p; Node *rlist = ReverseList(&root); int i=1; while(rlist!=NULL){ printf("element %d =%dn",i++,rlist->a); rlist=rlist->next; } n=root; while(n!=NULL){ p=n->next; free(n); n=p; } }
Node* ReverseList( Node ** List ) {
Node *temp1 = *List;
Node * temp2 = NULL;
Node * temp3 = NULL;
while ( temp1 )
{
*List = temp1; //set the head to last node
temp2= temp1->next; // save the next ptr in temp2
temp1->next = temp3; // change next to privous
temp3 = temp1;
temp1 = temp2;
}
return *List;
} Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character
becomes the last character of the file and so on)
Input: The program takes the file name whose content should be reversed.
Output: The program reverses the contents of the file.
|
View Answer
|
|
What is page thrashing?
|
View Answer
|
|
When should the register modifier be used? Does it really help?
|
View Answer
|
|
When should a type cast be used?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C Interview Questions & Answers - Exam Mode /
Learning Mode
|