Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct lista {
- int x;
- lista* next;
- };
- lista* dodaj (lista* head, int liczba)
- {
- lista* temp = new lista;
- temp->x = liczba;
- temp->next = NULL;
- if (head == NULL)
- {
- head = temp;
- } else if (head->next == NULL)
- {
- head->next = temp;
- } else
- {
- lista* temp2 = head;
- while (temp2->next != NULL)
- {
- temp2 = temp2->next;
- }
- temp2->next = temp;
- }
- return head;
- }
- void wypisz (lista* head)
- {
- if (head != NULL)
- {
- while (head != NULL)
- {
- cout << head->x << endl;
- head = head->next;
- }
- cout << endl;
- }
- }
- lista* merge (lista* head1, lista* head2, char wariant)
- {
- lista* temp[2] = {NULL, NULL};
- temp[0] = new lista;
- temp[0]->x = head1->x;
- temp[0]->next = NULL;
- head1 = head1->next;
- lista* newList = temp[0];
- if (head1 != NULL)
- {
- while (head1 != NULL)
- {
- temp[1] = new lista;
- temp[1]->x = head1->x;
- temp[1]->next = NULL;
- temp[0]->next = temp[1];
- temp[0] = temp[1];
- temp[1] = NULL;
- head1 = head1->next;
- }
- }
- lista* temp1 = newList;
- lista* temp2 = NULL;
- bool czyBylo = false;
- while (temp1->next != NULL)
- {
- temp1 = temp1->next;
- }
- switch (wariant)
- {
- case 't':
- {
- while (head2 != NULL)
- {
- czyBylo = false;
- temp2 = newList;
- while (temp2 != NULL)
- {
- if (temp2->x == head2->x)
- {
- czyBylo = true;
- }
- temp2 = temp2->next;
- }
- if (czyBylo == false)
- {
- temp2 = new lista;
- temp2->x = head2->x;
- temp2->next = NULL;
- temp1->next = temp2;
- temp1 = temp1->next;
- }
- head2 = head2->next;
- }
- break;
- }
- case 'n':
- {
- while (head2 != NULL)
- {
- temp2 = new lista;
- temp2->x = head2->x;
- temp2->next = NULL;
- temp1->next = temp2;
- temp1 = temp1->next;
- head2 = head2->next;
- }
- break;
- }
- }
- return newList;
- }
- void usunWszystko (lista* &head)
- {
- }
- int main()
- {
- lista* heads[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
- char coRobimy;
- int ktoraLista;
- int liczba;
- char ifBreak;
- char czyPowtorzenia;
- int ktoreListy[3] = {NULL, NULL, NULL};
- do
- {
- cout << "Dodaj do listy (d), wyswietl liste (w), wyjdz (q): ";
- cin >> coRobimy;
- cout << endl;
- switch (coRobimy)
- {
- case 'd':
- {
- cout << "Do ktorej listy dodac? (0-9) ";
- do {
- cin >> ktoraLista;
- } while (ktoraLista < 0 || ktoraLista > 9);
- cout << endl << "Jaka wartosc dodac? ";
- cin >> liczba;
- heads[ktoraLista] = dodaj(heads[ktoraLista], liczba);
- cout << endl << "Dodano wartosc " << liczba << " do listy " << ktoraLista << endl;
- break;
- }
- case 'w':
- {
- cout << "Ktora liste wypisac? ";
- do {
- cin >> ktoraLista;
- } while (liczba < 0 || liczba > 9);
- cout << endl;
- wypisz(heads[ktoraLista]);
- break;
- }
- case 'm':
- {
- cout << "Ktore listy polaczyc? (0-9)";
- do {
- cin >> ktoreListy[0] >> ktoreListy[1];
- } while (ktoreListy[0] < 0 || ktoreListy[0] > 9 || ktoreListy[1] < 0 || ktoreListy[1] > 9);
- cout << endl;
- cout << "W ktore miejsce wpisac nowa liste? (0-9)";
- do {
- cin >> ktoreListy[2];
- } while (ktoreListy[2] < 0 || ktoreListy[2] > 9);
- cout << endl;
- if ( heads[ktoreListy[2]] != NULL)
- {
- cout << "Na liscie docelowej istnieja elementy, zostana one usuniete. Czy na pewno chcesz kontynuowac?? (t/n) ";
- do {
- cin >> ifBreak;
- } while (ifBreak != 't' && ifBreak != 'n');
- if (ifBreak == 'n')
- {
- break;
- } else
- {
- usunWszystko(heads[ktoreListy[2]]);
- }
- }
- cout << endl;
- cout << "Czy dodawac bez powtorzen? (t/n)";
- do {
- cin >> czyPowtorzenia;
- } while (czyPowtorzenia != 't' && czyPowtorzenia != 'n');
- heads[ktoreListy[2]] = merge (heads[ktoreListy[0]], heads[ktoreListy[1]], czyPowtorzenia);
- break;
- }
- case 'q':
- {
- break;
- }
- default:
- {
- cout << "Nie ma takiej operacji!" << endl;
- break;
- }
- }
- } while (coRobimy != 'q');
- return 0;
- }
Add Comment
Please, Sign In to add comment