fork(1) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. /* Link list node */
  5. struct node
  6. {
  7. int data;
  8. struct node* next;
  9. };
  10.  
  11. typedef struct node NODE;
  12. /* Function to reverse the linked list */
  13. NODE* reverse(NODE* head,NODE* prev)
  14. {
  15. NODE* temp=head;
  16. if(head==NULL)
  17. return prev;
  18. head=reverse(head->next,head);
  19. temp->next=prev;
  20. return head;
  21.  
  22. }
  23.  
  24. /* Function to push a node */
  25. void push(struct node** head_ref, int new_data)
  26. {
  27. /* allocate node */
  28. struct node* new_node =
  29. (struct node*) malloc(sizeof(struct node));
  30.  
  31. /* put in the data */
  32. new_node->data = new_data;
  33.  
  34. /* link the old list off the new node */
  35. new_node->next = (*head_ref);
  36.  
  37. /* move the head to point to the new node */
  38. (*head_ref) = new_node;
  39. }
  40.  
  41. /* Function to print linked list */
  42. void printList(struct node *head)
  43. {
  44. struct node *temp = head;
  45. while(temp != NULL)
  46. {
  47. printf("%d ", temp->data);
  48. temp = temp->next;
  49. }
  50. }
  51.  
  52. /* Drier program to test above function*/
  53. int main()
  54. {
  55. /* Start with the empty list */
  56. struct node* head = NULL;
  57.  
  58. push(&head, 20);
  59. push(&head, 4);
  60. push(&head, 15);
  61. push(&head, 85);
  62.  
  63. printList(head);
  64. head=reverse(head,NULL);
  65. printf("\n Reversed Linked list \n");
  66. printList(head);
  67. getchar();
  68. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
85  15  4  20  
 Reversed Linked list 
20  4  15  85