fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node{
  5. int data;
  6. struct node *next;
  7. };
  8.  
  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. while(temp1 != NULL){
  30. temp->next = temp1->next;
  31. temp1->next = temp;
  32.  
  33. if(start == NULL)
  34. start = temp1;
  35. else
  36. ptr->next = temp1;
  37. ptr = temp;
  38.  
  39. temp = temp->next;
  40. temp1 = temp->next;
  41. display(start);
  42. //getchar();
  43. }
  44. }
  45.  
  46. int main(){
  47. struct node *start = NULL;
  48. struct node *head;
  49.  
  50. push(&start,10);
  51. push(&start,9);
  52. push(&start,8);
  53. push(&start,7);
  54. push(&start,6);
  55. push(&start,5);
  56. push(&start,4);
  57. push(&start,3);
  58. push(&start,2);
  59. push(&start,1);
  60. display(start);
  61. printf("\n");
  62.  
  63. swap(start);
  64. //display(start);
  65. //getchar();
  66. return 0;
  67. }
Runtime error #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Standard output is empty