Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<cstdlib>
  6. #include<iostream>
  7.  
  8. using namespace std;
  9.  
  10. struct elem
  11. {
  12. int a;
  13. elem *next;
  14. };
  15. elem *head = NULL;
  16. elem *head2 = NULL;
  17. void push(int a)
  18. {
  19. elem *p = new elem;
  20. p->a = a;
  21. p->next = head;
  22. head = p;
  23. }
  24. int pop()
  25. {
  26. if (head == NULL)return NULL;
  27. int x = head->a;
  28. elem *p = head;
  29. head = head->next;
  30. delete p;
  31. return x;
  32. }
  33. int pop2()
  34. {
  35. if (head2 == NULL)return NULL;
  36. int x = head2->a;
  37. elem *p = head2;
  38. head2 = head2->next;
  39. delete p;
  40. return x;
  41. }
  42. bool empty()
  43. {
  44. if (head == NULL)return true;
  45. return false;
  46. }
  47. bool empty2()
  48. {
  49. if (head2 == NULL)return true;
  50. return false;
  51. }
  52. void zamienPierwszeDwaParzyste()
  53. {
  54. if (head == NULL || head->next == NULL)
  55. {
  56. cout << "Stos ma mniej niz dwa elementy." << endl;
  57. return;
  58. }
  59. int x=0;
  60. elem *p1=NULL, *p2=NULL, *h1=head;
  61. for (; h1 != NULL; h1 = h1->next)
  62. {
  63. if ((h1->a) % 2 == 0)
  64. {
  65. if (x == 0)
  66. {
  67. p1 = h1;
  68. x = 1;
  69. }
  70. else
  71. {
  72. p2 = h1;
  73. x = 2;
  74. break;
  75. }
  76. }
  77. }
  78. if (x != 2)
  79. {
  80. cout << "Nie znaleziono dwoch elementow parzystych." << endl;
  81. return;
  82. }
  83. swap(p1->a, p2->a);
  84. cout << "Zamieniono!" << endl;
  85. }
  86. bool przerzucSpelniajaceNaDrugi()
  87. {
  88. //warunek: klucz ma byc nieparzysty
  89. head2 = NULL;
  90. if (head == NULL)return false;
  91. elem *pom, *h1=head;
  92. while (head != NULL && head->a % 2 == 1)
  93. {
  94. pom = head->next;
  95. head->next = head2;
  96. head2 = head;
  97. head = pom;
  98. }
  99. for (; h1->next != NULL; h1 = h1->next)
  100. {
  101. if (h1->next->a % 2 == 1)
  102. {
  103. pom = h1->next;
  104. h1->next = pom->next;
  105. pom->next = head2;
  106. head2 = pom;
  107. }
  108. }
  109. return true;
  110. }
  111.  
  112. void wypiszStos(elem *p)
  113. {
  114. while (p != NULL)
  115. {
  116. cout << p->a << endl;
  117. p = p->next;
  118. }
  119. }
  120.  
  121. int main()
  122. {
  123. push(5);
  124. push(4);
  125. push(10);
  126. push(3);
  127. push(1);
  128. zamienPierwszeDwaParzyste();
  129. wypiszStos(head);
  130. if (przerzucSpelniajaceNaDrugi())
  131. {
  132. cout << "Przerzucono!" << endl;
  133. cout << "Pierwszy stos:" << endl;
  134. wypiszStos(head);
  135. cout << "Drugi stos:" << endl;
  136. wypiszStos(head2);
  137. }
  138. system("pause");
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement