Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. //zad2 14/15
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct node {
  8. int val;
  9. node *next;
  10. };
  11. struct TwoLists {
  12. node *odd;
  13. node *even;
  14. };
  15. void _CreateList ( node *&first);//tworzy listê
  16. void _CoutList (node *first);//wypisuje listê
  17.  
  18. bool Odd (int val){
  19. if(val&1==1) return true;
  20. else return false;
  21. }
  22.  
  23. TwoLists split (node *list){
  24. node *head_odd=NULL;
  25. node *head_even=NULL;
  26. node *prev=NULL;
  27. while (list!=NULL){
  28. prev=list;
  29. list=list->next;
  30. if(Odd(prev->val)==true){
  31. prev->next=head_odd;
  32. head_odd=prev;
  33. }
  34. else{
  35. prev->next=head_even;
  36. head_even=prev;
  37. }
  38. }
  39. TwoLists res;
  40. res.even=head_even;
  41. res.odd=head_odd;
  42. return res;
  43.  
  44. }
  45.  
  46. int main()
  47. {
  48. node *h=NULL;
  49. _CreateList(h);
  50. TwoLists res=split(h);
  51. _CoutList(res.even);
  52. cout<<endl;
  53. _CoutList(res.odd);
  54.  
  55. return 0;
  56. }
  57. void _CreateList ( node *&first){
  58. int len;
  59. cin>>len;
  60. node *p;
  61. for(int i=0;i<len;i++){
  62. int x;
  63. cin>>x;
  64. p= new node;
  65. p->val=x;
  66. p->next=first;
  67. first=p;
  68. }
  69. }
  70. void _CoutList (node *first){
  71. while ( first !=NULL)
  72. {
  73. cout<<first->val<<" ";
  74. first=first->next;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement