Write a program to reverse a linked list?
Answer Posted / murali
/* Error Checking is not done */
#include <stdio.h>
typedef struct node {
char ch;
struct node *next;
} list;
list* addNode(const list *start, char ch) {
list *ll;
ll = (list *)start;
while( ll->next != NULL ) { ll = ll->next; }
ll->next = (list *) malloc(sizeof(list));
ll->next->ch = ch;
ll->next->next = NULL;
return ll->next;
}
void printList(const list *start) {
list *ll;
ll = (list *)start;
while ( ll->next != NULL ) {
printf(" %c --> ", ll->ch );
ll = ll->next;
}
printf(" %c --> ", ll->ch );
printf( " NULL ");
}
void reverse(list *a, list *b) {
if( b->next != NULL )
reverse(b, b->next);
b->next = a;
a->next = NULL;
}
int main() {
list *end;
list *start = (list *) malloc(sizeof(list));
start->ch = 'A';
start->next = NULL;
addNode(start, 'B');
addNode(start, 'C');
addNode(start, 'D');
addNode(start, 'E');
end = addNode(start, 'F');
printList(start);
printf("\n");
reverse(start, start->next);
printList(end);
printf("\n");
return 0;
}
| Is This Answer Correct ? | 9 Yes | 5 No |
Post New Answer View All Answers
Show the declaration for a static member variable.
What will the line of code below print out and why?
Does a derived class inherit or doesn't inherit?
In inline " expression passed as argument are evalauated once " while in macro "in some cases expression passed as argument are evaluated more than once " --> i am not getting it plz help to make me understand....
What are c++ stream classes?
What is a block in c++?
Is linux written in c or c++?
What happens if a pointer is deleted twice?
What happens when the extern "c" char func (char*,waste) executes?
What do you mean by translation unit in c++?
What are the uses of static class data?
What is lambda in c++?
Difference between struct and class in terms of access modifier.
What is data structure in c++?
What is general form of pure virtual function? Explain?