fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <cstdio>
  4.  
  5. struct node
  6. {
  7. int i;
  8. node * nxt;
  9. }*head,*a;
  10. int main()
  11. {
  12. int n;
  13. // cout<<"yes";
  14. head=new node;
  15. head->i=10;
  16. a=head;
  17. for(int j=0;j<9;j++)
  18. {
  19. node *ptr=new node;
  20. // cout<<"\n enter value :";
  21. cin>>ptr->i;
  22. a->nxt=ptr;
  23. a=ptr;
  24. }
  25. a->nxt=NULL;
  26. a=head;
  27. cout<<"\n before....";
  28. for(int j=0;j<10;j++)
  29. {
  30. cout<<a->i<<"->";
  31. a=a->nxt;
  32. }
  33. cout<<"\n after...";
  34. node *s,*t,*u;
  35. a=head;
  36. while(a->nxt!=NULL)
  37. {
  38. // cout<<"yes";
  39. u=a->nxt;
  40. //cout<<"yes";
  41. s=(a->nxt)->nxt;
  42. if(a==NULL||u==NULL||s==NULL)
  43. break;
  44. t=s->nxt;
  45. //cout<<u->i<<s->i<<t->i<<endl;
  46. a->nxt=t;
  47. u->nxt=t->nxt;
  48. t->nxt=s;
  49. s->nxt=u;
  50. a->nxt=t;
  51. a=u;
  52. //cout<<u->i<<s->i<<t->i<<endl;
  53.  
  54. };
  55. a=head;
  56. while(a!=NULL)
  57. {
  58. cout<<a->i<<"->";
  59. a=a->nxt;
  60. }
  61. cin>>n;
  62. }
  63.  
Success #stdin #stdout 0s 2988KB
stdin
1 2 3 4 5 6 7 8 9
stdout
 before....10->1->2->3->4->5->6->7->8->9->
 after...10->3->2->1->6->5->4->9->8->7->