#include <stdio.
h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct ToLista {
int Liczba;
struct ToLista *Nastepny;
struct ToLista *Poprzedni;
};
struct ToLista* nowyToLista(int liczba) {
struct ToLista* temp = (struct ToLista*)malloc(sizeof(struct ToLista));
temp->Liczba = liczba;
temp->Nastepny = NULL;
temp->Poprzedni = NULL;
return temp;
}
void dodajNaKoniec(struct ToLista** glowa, int liczba) {
struct ToLista* nowy = nowyToLista(liczba);
if (*glowa == NULL) {
*glowa = nowy;
return;
}
struct ToLista* temp = *glowa;
while (temp->Nastepny != NULL) {
temp = temp->Nastepny;
}
temp->Nastepny = nowy;
nowy->Poprzedni = temp;
}
void dodajElement(struct ToLista** glowa, int liczba) {
struct ToLista* temp = *glowa;
while (temp != NULL) {
if (temp->Liczba == liczba) {
return;
}
temp = temp->Nastepny;
}
dodajNaKoniec(glowa, liczba);
}
void wyswietl(struct ToLista* glowa) {
struct ToLista* temp = glowa;
printf("{");
while (temp != NULL) {
printf("%d", temp->Liczba);
if (temp->Nastepny != NULL) printf(", ");
temp = temp->Nastepny;
}
printf("}\n");
}
struct ToLista* sumaZbiorow(struct ToLista* zbior1, struct ToLista* zbior2) {
struct ToLista* wynik = NULL;
struct ToLista* temp = zbior1;
while (temp != NULL) {
dodajElement(&wynik, temp->Liczba);
temp = temp->Nastepny;
}
temp = zbior2;
while (temp != NULL) {
dodajElement(&wynik, temp->Liczba);
temp = temp->Nastepny;
}
return wynik;
}
struct ToLista* iloczynZbiorow(struct ToLista* zbior1, struct ToLista* zbior2) {
struct ToLista* wynik = NULL;
struct ToLista* temp1 = zbior1;
while (temp1 != NULL) {
struct ToLista* temp2 = zbior2;
while (temp2 != NULL) {
if (temp1->Liczba == temp2->Liczba) {
dodajElement(&wynik, temp1->Liczba);
}
temp2 = temp2->Nastepny;
}
temp1 = temp1->Nastepny;
}
return wynik;
}
struct ToLista* roznicaZbiorow(struct ToLista* zbior1, struct ToLista* zbior2) {
struct ToLista* wynik = NULL;
struct ToLista* temp1 = zbior1;
while (temp1 != NULL) {
struct ToLista* temp2 = zbior2;
int znaleziono = 0;
while (temp2 != NULL) {
if (temp1->Liczba == temp2->Liczba) {
znaleziono = 1;
break;
}
temp2 = temp2->Nastepny;
}
if (!znaleziono) {
dodajElement(&wynik, temp1->Liczba);
}
temp1 = temp1->Nastepny;
}
return wynik;
}
int zawiera(struct ToLista* zbior1, struct ToLista* zbior2) {
struct ToLista* temp2 = zbior2;
while (temp2 != NULL) {
struct ToLista* temp1 = zbior1;
int znaleziono = 0;
while (temp1 != NULL) {
if (temp1->Liczba == temp2->Liczba) {
znaleziono = 1;
break;
}
temp1 = temp1->Nastepny;
}
if (!znaleziono) return 0;
temp2 = temp2->Nastepny;
}
return 1;
}
int wynikanie(struct ToLista* zbior1, struct ToLista* zbior2) {
return zawiera(zbior2, zbior1);
}
int main() {
struct ToLista* ZbiorA = NULL;
struct ToLista* ZbiorB = NULL;
dodajElement(&ZbiorA, 1);
dodajElement(&ZbiorA, 2);
dodajElement(&ZbiorA, 4);
dodajElement(&ZbiorA, 6);
dodajElement(&ZbiorB, 3);
dodajElement(&ZbiorB, 4);
dodajElement(&ZbiorB, 6);
dodajElement(&ZbiorB, 8);
printf("Zbior A: ");
wyswietl(ZbiorA);
printf("Zbior B: ");
wyswietl(ZbiorB);
printf("Suma: ");
wyswietl(sumaZbiorow(ZbiorA, ZbiorB));
printf("Iloczyn: ");
wyswietl(iloczynZbiorow(ZbiorA, ZbiorB));
printf("Różnica A - B: ");
wyswietl(roznicaZbiorow(ZbiorA, ZbiorB));
printf("Czy A zawiera się w B? %s\n", zawiera(ZbiorA, ZbiorB) ? "Tak" : "Nie");
printf("Czy A wynika z B? %s\n", wynikanie(ZbiorA, ZbiorB) ? "Tak" : "Nie");
return 0;
}