Guest User

Untitled

a guest
May 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct lista {
  8.     int x;
  9.     lista* next;
  10. };
  11.  
  12. lista* dodaj (lista* head, int liczba)
  13. {
  14.  
  15.     lista* temp = new lista;
  16.                            
  17.     temp->x = liczba;
  18.     temp->next = NULL;
  19.  
  20.     if (head == NULL)
  21.     {
  22.         head = temp;
  23.  
  24.     } else if (head->next == NULL)  
  25.     {
  26.         head->next = temp;
  27.  
  28.     } else
  29.     {
  30.        
  31.         lista* temp2 = head;
  32.        
  33.         while (temp2->next != NULL)
  34.         {
  35.             temp2 = temp2->next;
  36.         }
  37.  
  38.         temp2->next = temp;
  39.     }
  40.  
  41.     return head;
  42.    
  43. }
  44.  
  45. void wypisz (lista* head)
  46. {
  47.     if (head != NULL)
  48.     {
  49.         while (head != NULL)
  50.         {
  51.             cout << head->x << endl;
  52.             head = head->next;
  53.         }
  54.  
  55.         cout << endl;
  56.     }
  57. }
  58.  
  59. lista* merge (lista* head1, lista* head2, char wariant)
  60. {
  61.  
  62.     lista* temp[2] = {NULL, NULL};
  63.     temp[0] =  new lista;
  64.     temp[0]->x = head1->x;
  65.     temp[0]->next = NULL;
  66.     head1 = head1->next;
  67.  
  68.     lista* newList = temp[0];
  69.  
  70.     if (head1 != NULL)
  71.     {
  72.     while (head1 != NULL)
  73.     {
  74.         temp[1] = new lista;
  75.         temp[1]->x = head1->x;
  76.         temp[1]->next = NULL;
  77.  
  78.         temp[0]->next = temp[1];
  79.         temp[0] = temp[1];
  80.         temp[1] = NULL;
  81.  
  82.         head1 = head1->next;
  83.     }
  84.     }
  85.     lista* temp1 = newList;
  86.     lista* temp2 = NULL;
  87.     bool czyBylo = false;
  88.     while (temp1->next != NULL)
  89.     {
  90.         temp1 = temp1->next;
  91.     }
  92.  
  93.     switch (wariant)
  94.     {
  95.  
  96.     case 't':
  97.         {
  98.             while (head2 != NULL)
  99.             {
  100.                 czyBylo = false;
  101.                 temp2 = newList;
  102.                 while (temp2 != NULL)
  103.                 {
  104.                     if (temp2->x == head2->x)
  105.                     {
  106.                         czyBylo = true;
  107.                     }
  108.  
  109.                     temp2 = temp2->next;
  110.                 }
  111.  
  112.                 if (czyBylo == false)
  113.                 {
  114.                     temp2 = new lista;
  115.                     temp2->x = head2->x;
  116.                     temp2->next = NULL;
  117.                     temp1->next = temp2;
  118.                     temp1 = temp1->next;
  119.                 }
  120.  
  121.                 head2 = head2->next;
  122.             }
  123.  
  124.             break;
  125.         }
  126.  
  127.     case 'n':
  128.         {
  129.             while (head2 != NULL)
  130.             {
  131.  
  132.                 temp2 = new lista;
  133.                 temp2->x = head2->x;
  134.                 temp2->next = NULL;
  135.                 temp1->next = temp2;
  136.                 temp1 = temp1->next;
  137.  
  138.                 head2 = head2->next;
  139.             }
  140.  
  141.             break;
  142.         }
  143.     }
  144.    
  145.     return newList;
  146.  
  147. }
  148.  
  149. void usunWszystko (lista* &head)
  150. {
  151.  
  152. }
  153.  
  154.  
  155. int main()
  156. {
  157.     lista* heads[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  158.     char coRobimy;
  159.     int ktoraLista;
  160.     int liczba;
  161.  
  162.     char ifBreak;
  163.     char czyPowtorzenia;
  164.  
  165.     int ktoreListy[3] = {NULL, NULL, NULL};
  166.  
  167.     do
  168.     {
  169.  
  170.     cout << "Dodaj do listy (d), wyswietl liste (w), wyjdz (q): ";
  171.     cin >> coRobimy;
  172.     cout << endl;
  173.  
  174.     switch (coRobimy)
  175.     {
  176.  
  177.     case 'd':
  178.         {
  179.             cout << "Do ktorej listy dodac? (0-9) ";
  180.  
  181.             do {
  182.                 cin >> ktoraLista;
  183.             } while (ktoraLista < 0  || ktoraLista > 9);
  184.  
  185.             cout << endl << "Jaka wartosc dodac? ";
  186.             cin >> liczba;
  187.  
  188.             heads[ktoraLista] = dodaj(heads[ktoraLista], liczba);
  189.             cout << endl << "Dodano wartosc " << liczba << " do listy " << ktoraLista << endl;
  190.  
  191.             break;
  192.         }
  193.  
  194.     case 'w':
  195.         {
  196.             cout << "Ktora liste wypisac? ";
  197.  
  198.             do {
  199.                 cin >> ktoraLista;
  200.             } while (liczba < 0 || liczba > 9);
  201.             cout << endl;
  202.             wypisz(heads[ktoraLista]);
  203.             break;
  204.         }
  205.  
  206.     case 'm':
  207.         {
  208.             cout << "Ktore listy polaczyc? (0-9)";
  209.             do {
  210.                 cin >> ktoreListy[0] >> ktoreListy[1];
  211.             } while (ktoreListy[0] < 0 || ktoreListy[0] > 9 || ktoreListy[1] < 0 || ktoreListy[1] > 9);
  212.            
  213.             cout << endl;
  214.  
  215.             cout << "W ktore miejsce wpisac nowa liste? (0-9)";
  216.             do {
  217.                 cin >> ktoreListy[2];
  218.             } while (ktoreListy[2] < 0 || ktoreListy[2] > 9);
  219.            
  220.             cout << endl;
  221.  
  222.             if ( heads[ktoreListy[2]] != NULL)
  223.             {
  224.                 cout << "Na liscie docelowej istnieja elementy, zostana one usuniete. Czy na pewno chcesz kontynuowac?? (t/n) ";
  225.                     do {
  226.                         cin >> ifBreak;
  227.                     } while (ifBreak != 't' && ifBreak != 'n');
  228.  
  229.                 if (ifBreak == 'n')
  230.                 {
  231.                     break;
  232.                 } else
  233.                 {
  234.                     usunWszystko(heads[ktoreListy[2]]);
  235.                 }
  236.             }
  237.  
  238.             cout << endl;
  239.  
  240.             cout << "Czy dodawac bez powtorzen? (t/n)";
  241.                 do {
  242.                         cin >> czyPowtorzenia;
  243.                     } while (czyPowtorzenia != 't' && czyPowtorzenia != 'n');
  244.  
  245.                 heads[ktoreListy[2]] = merge (heads[ktoreListy[0]], heads[ktoreListy[1]], czyPowtorzenia);
  246.  
  247.  
  248.             break;
  249.         }
  250.  
  251.     case 'q':
  252.         {
  253.  
  254.             break;
  255.         }
  256.  
  257.     default:
  258.         {
  259.             cout << "Nie ma takiej operacji!" << endl;
  260.             break;
  261.         }
  262.     }
  263.  
  264.     } while (coRobimy != 'q');
  265.  
  266.  
  267.  
  268.  
  269.     return 0;
  270. }
Add Comment
Please, Sign In to add comment