fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node{
  5. int data;
  6. struct node *next;
  7. };
  8. struct node *h;
  9. void push(struct node** head,int n){
  10. struct node* new_node = (struct node*) malloc(sizeof(struct node));
  11. new_node->data = n;
  12. new_node->next = *head;
  13.  
  14. *head = new_node;
  15. }
  16.  
  17. void display(struct node *head){
  18. while(head != NULL){
  19. printf("%d ",head->data);
  20. head = head->next;
  21. }
  22. }
  23.  
  24. void swap(struct node* head){
  25. if(head == NULL || head->next == NULL)
  26. return;
  27.  
  28. struct node *temp = head,*temp1 = head->next,*ptr,*start = NULL;
  29. // printf("**%d**\n",temp1->data);
  30. while(temp != NULL&&temp1!=NULL){
  31. temp->next = temp1->next;
  32. temp1->next = temp;
  33.  
  34. if(start == NULL)
  35. {
  36. start = temp1;
  37. //printf("**%d**\n",start->data);
  38. }
  39. else
  40. ptr->next = temp1;
  41. ptr = temp;
  42.  
  43. temp = temp->next;
  44. if(temp!=NULL)
  45. temp1 = temp->next;
  46. //getchar();
  47. }
  48. //display(start);
  49. h=start;
  50. }
  51.  
  52. int main(){
  53. struct node *start = NULL;
  54. struct node *head;
  55.  
  56. push(&start,10);
  57. push(&start,9);
  58. push(&start,8);
  59. push(&start,7);
  60. push(&start,6);
  61. push(&start,5);
  62. push(&start,4);
  63. push(&start,3);
  64. push(&start,2);
  65. push(&start,1);
  66. display(start);
  67. printf("\n");
  68.  
  69. swap(start);
  70. start=h;
  71. display(start);
  72. //getchar();
  73. return 0;
  74. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 10 
2 1 4 3 6 5 8 7 10 9