Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node{
- int value;
- Node* next;
- };
- void dodawanie_przod(Node* &head, int x);
- void wypisz(Node* head);
- void scalaj(Node* head_1, Node* head_2, Node* &head_3);
- bool niema(Node* head, int x);
- int main(){
- Node* head_1 = NULL;
- Node* head_2 = NULL;
- int x, i;
- for(i=1; i<=10; i++){
- cin >> x;
- dodawanie_przod(head_1, x);
- }
- wypisz(head_1);
- cout << endl;
- for(i=1; i<=10; i++){
- cin >> x;
- dodawanie_przod(head_2, x);
- }
- wypisz(head_2);
- cout << endl;
- Node* head_3 = NULL;
- scalaj(head_1, head_2, head_3);
- wypisz(head_3);
- }
- void dodawanie_przod(Node* &head, int x){
- Node* p;
- p = new Node;
- p->value = x;
- p->next = head;
- head = p;
- }
- void wypisz(Node* head){
- if(head == NULL)
- cout << "lista pusta";
- else{
- Node* tmp;
- tmp = head;
- while(tmp != NULL){
- cout << tmp->value << " ";
- tmp = tmp->next;
- }
- }
- }
- void scalaj(Node* head_1, Node* head_2, Node* &head_3){
- if(head_1 == NULL){
- wypisz(head_2);
- return;
- }
- if(head_2 == NULL){
- wypisz(head_2);
- return;
- }
- int pomocnicza;
- Node *tmp_1, *tmp_2, *tmp_3;
- tmp_1 = head_1;
- tmp_2 = head_2;
- tmp_3 = head_3;
- // zaczynam od lancucha head_1
- while(tmp_1 != NULL){
- pomocnicza = tmp_1->value;
- if(niema(head_2, pomocnicza)){
- Node* p;
- p = new Node;
- p->value = pomocnicza;
- p->next = head_3;
- head_3 = p;
- }
- tmp_1 = tmp_1->next;
- }
- while(tmp_2 != NULL){
- pomocnicza = tmp_2->value;
- if(niema(head_1, pomocnicza)){
- Node* p;
- p = new Node;
- p->value = pomocnicza;
- p->next = head_3;
- head_3 = p;
- }
- tmp_2 = tmp_2->next;
- }
- }
- bool niema(Node* head, int x){
- Node* tmp;
- tmp = head;
- while(tmp != NULL){
- if(tmp->value == x)
- return false;
- tmp = tmp->next;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement