fork download
  1. #include<iostream>
  2. using namespace std;
  3. class node{
  4. public:
  5. node *next;
  6. int data;
  7. node(int d){
  8. next = NULL;
  9. data = d;
  10. }
  11. };
  12. void insertAtTail(node *&head, int data){
  13. if(head==NULL){
  14. head= new node(data);
  15. return;
  16. }
  17. node *temp = head;
  18. while(temp->next!=NULL){
  19. temp = temp->next;
  20. }
  21. temp->next = new node(data);
  22. return;
  23. }
  24.  
  25. void print(node *head){
  26. while(head !=NULL){
  27. cout<< head->data<<" ";
  28. head = head->next;
  29. }
  30. }
  31.  
  32. node *even_after_odd(node *head){
  33. node *temp = head;
  34. node *a_head = NULL, *a_tail =NULL, *b_head = NULL, *b_tail = NULL;
  35.  
  36. while(temp!= NULL){
  37. if(temp->data%2!=0){
  38. if(a_head==NULL){
  39. a_head=a_tail=temp;
  40. }
  41. else{
  42. a_tail->next = temp;
  43. a_tail = temp;
  44. }
  45. }
  46. else if(temp->data%2==0){
  47. if(b_head==NULL){
  48. b_head=b_tail=temp;
  49. }
  50. else{
  51. b_tail->next = temp;
  52. b_tail = temp;
  53. }
  54. }
  55. temp = temp->next;
  56. }
  57.  
  58. a_tail->next = b_head;
  59. b_tail->next = NULL;
  60. return a_head;
  61. }
  62.  
  63.  
  64. int main(){
  65. node *head = NULL;
  66. int n,data;
  67. cin>>n;
  68. for(int i=0;i<n;i++){
  69. cin>>data;
  70. insertAtTail(head,data);
  71. }
  72. node *ans = even_after_odd(head);
  73. print(ans);
  74. return 0;
  75. }
  76.  
Runtime error #stdin #stdout 0s 4292KB
stdin
Standard input is empty
stdout
Standard output is empty