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
How can I learn dev c++ programming?
What's the order in which the local objects are destructed?
What is pointer with example?
how can i access a direct (absolute, not the offset) memory
address?
here is what i tried:
wrote a program that ask's for an address from the user,
creates a FAR pointer to that adress and shows it. then the
user can increment/decrement the value in that address by
pressing p(inc+) and m(dec-).
NOW, i compiled that program and opened it twice (in 2
different windows) and gave twice the same address to it.
now look what happen - if i change the value in
one "window" of the program, it DOES NOT change in the
other! even if they point to the same address in the memory!
here is the code snippet:
//------------------------------------------------------
#include
What is the main function c++?
When can I use a forward declaration?
why is iostream::eof inside a loop condition considered wrong?
What are c++ manipulators?
Ask to write virtual base class code?
Explain the scope of resolution operator.
What is the prototype of printf function?
When to use “const” reference arguments in a function?
Define pre-condition and post-condition to a member function in c++?
What is the difference between the indirection operator and the address of oper-ator?
List the advantages of inheritance.