Write a C Language program to reverse a linked list

Category > CPLUSPLUS || Published on : Sunday, May 17, 2015 || Views: 4592 || Write a program to reverse a linked list program to reverse a linked list c program to reverse a linked list


Write a program to reverse a linked list


 node *fresh; 
 fresh = (node *) malloc(sizeof(node)); 
 fresh->month = d; 
 fresh->next = record->next; 
 record->next = fresh; 
 } 
 
 void print(node * record) 
 {
 node *temp; 
 temp = (node *) malloc(sizeof(node)); 
 for (temp = record->next; temp; temp = temp->next) 
 printf(" %d", temp->month); 
 }

 void reverse(node * record)
 {
 node *temp;
 node *temp1;
 node *temp2;
 temp = (node *) malloc(sizeof(node));
 temp1 = (node *) malloc(sizeof(node));
 temp2 = (node *) malloc(sizeof(node));
 temp = record;
 temp1 = temp->next;
 temp2 = temp1->next;
 temp->next->next = NULL;
 while (temp2 != NULL) {
 temp = temp1;
 temp1 = temp2;
 temp2 = temp1->next;
 temp1->next = temp;
 }
 record->next = temp1;
 } 

 int main(void) 
 { 
 node *start; 
 node *start1; 
 start = (node *) malloc(sizeof(node)); 
 init(start); 
 int i = 0;
 for (i = 10; i >= 0; i--) 
 addnode(start, i); 
 print(start); 
 reverse(start); 
 printf("n"); 
 print(start); 
 return 0; 
 }

so In this tutorial we learn How to  program to reverse a linked list. Happy Coding!!!!!