Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5. int value;
  6. Node* next;
  7. };
  8.  
  9. void dodawanie_przod(Node* &head, int x);
  10. void wypisz(Node* head);
  11. void scalaj(Node* head_1, Node* head_2, Node* &head_3);
  12. bool niema(Node* head, int x);
  13.  
  14. int main(){
  15.  
  16. Node* head_1 = NULL;
  17. Node* head_2 = NULL;
  18.  
  19. int x, i;
  20.  
  21. for(i=1; i<=10; i++){
  22. cin >> x;
  23. dodawanie_przod(head_1, x);
  24. }
  25.  
  26. wypisz(head_1);
  27. cout << endl;
  28.  
  29. for(i=1; i<=10; i++){
  30. cin >> x;
  31. dodawanie_przod(head_2, x);
  32. }
  33.  
  34. wypisz(head_2);
  35. cout << endl;
  36.  
  37. Node* head_3 = NULL;
  38.  
  39. scalaj(head_1, head_2, head_3);
  40. wypisz(head_3);
  41. }
  42.  
  43. void dodawanie_przod(Node* &head, int x){
  44. Node* p;
  45. p = new Node;
  46.  
  47. p->value = x;
  48. p->next = head;
  49. head = p;
  50. }
  51.  
  52. void wypisz(Node* head){
  53. if(head == NULL)
  54. cout << "lista pusta";
  55. else{
  56. Node* tmp;
  57. tmp = head;
  58. while(tmp != NULL){
  59. cout << tmp->value << " ";
  60. tmp = tmp->next;
  61. }
  62. }
  63. }
  64.  
  65. void scalaj(Node* head_1, Node* head_2, Node* &head_3){
  66. if(head_1 == NULL){
  67. wypisz(head_2);
  68. return;
  69. }
  70.  
  71. if(head_2 == NULL){
  72. wypisz(head_2);
  73. return;
  74. }
  75.  
  76. int pomocnicza;
  77. Node *tmp_1, *tmp_2, *tmp_3;
  78.  
  79. tmp_1 = head_1;
  80. tmp_2 = head_2;
  81. tmp_3 = head_3;
  82.  
  83. // zaczynam od lancucha head_1
  84. while(tmp_1 != NULL){
  85. pomocnicza = tmp_1->value;
  86. if(niema(head_2, pomocnicza)){
  87. Node* p;
  88. p = new Node;
  89. p->value = pomocnicza;
  90. p->next = head_3;
  91. head_3 = p;
  92. }
  93. tmp_1 = tmp_1->next;
  94. }
  95.  
  96. while(tmp_2 != NULL){
  97. pomocnicza = tmp_2->value;
  98. if(niema(head_1, pomocnicza)){
  99. Node* p;
  100. p = new Node;
  101. p->value = pomocnicza;
  102. p->next = head_3;
  103. head_3 = p;
  104. }
  105. tmp_2 = tmp_2->next;
  106. }
  107. }
  108.  
  109. bool niema(Node* head, int x){
  110. Node* tmp;
  111. tmp = head;
  112. while(tmp != NULL){
  113. if(tmp->value == x)
  114. return false;
  115. tmp = tmp->next;
  116. }
  117. return true;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement