Facebook
From Rude Peafowl, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 55
  1. #include <ctime>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include <time.h>
  8.  
  9. using namespace std;
  10.  
  11. struct Selement
  12. {
  13.   char litera;
  14.   Selement* next;
  15. };
  16.  
  17. void dodajNaKoniec(Selement* &glowa, Selement* nowy)
  18. {
  19.     if (nowy==nullptr)
  20.     {
  21.         return; //nie ma czego dodać
  22.     }
  23.     if (glowa==nullptr)
  24.     {
  25.         glowa = nowy; //pierwszy element
  26.         glowa->next = nullptr; //na wszelki wypadek
  27.     }
  28.     else
  29.     {
  30.         Selement* temp = glowa; //szukamy konca listy
  31.         while(temp->next != nullptr)
  32.         {
  33.             temp = temp->next;
  34.         } //temp ostatni element listy
  35.         temp->next = nowy; //dodajemy  na koniec listy
  36.     }
  37. }
  38.  
  39. Selement* nowyElement(bool &jestNowy)
  40. {
  41.     Selement* nowy = nullptr;
  42.     char literaLos = 'a' + rand()%('z'-'a'+1);
  43.     if (literaLos !='a')
  44.     {
  45.         nowy = new Selement;
  46.         nowy->litera = literaLos;
  47.         nowy->next = nullptr;
  48.         jestNowy = true;
  49.     }
  50.     else
  51.     {
  52.         jestNowy = false;
  53.     }
  54.     return nowy;
  55. }
  56.  
  57. void drukowanieListy (Selement* reader, string naglowek)
  58. {
  59.     cout << naglowek <<  endl;
  60.  
  61.     while(reader!=nullptr)
  62.     {
  63.         cout <<reader->litera << ';';
  64.         reader = reader->next;
  65.     }
  66.     cout << endl;
  67. }
  68.  
  69. void usuwanieListy(Selement* &glowa)
  70. {
  71.     while(glowa!=nullptr)
  72.     {
  73.         Selement* toDel = glowa;
  74.         glowa = glowa->next;
  75.         delete toDel;
  76.     }
  77. }
  78.  
  79. int rozmiarListy(Selement* glowa)
  80. {
  81.     int licznik = 0;
  82.     for(Selement* re = glowa; re!= nullptr; re = re->next)
  83.     {
  84.         licznik++;
  85.     }
  86.     return licznik;
  87. }
  88.  
  89. bool dodajWPozycji(Selement* &glowa, Selement* nowy, int pozycja)
  90. {
  91.     if(nowy== nullptr)
  92.     {
  93.         return false; // nie ma  czego dodawać
  94.     }
  95.     int rozmiar = rozmiarListy(glowa);
  96.     //wiemy ile jest elementów listy
  97.     if(pozycja - 1 > rozmiar || pozycja < 1)
  98.     {
  99.         return false;
  100.     }
  101.     if(pozycja == 1)
  102.     {
  103.         //dodać na początku
  104.         nowy->next = glowa;
  105.         glowa =  nowy;
  106.     }
  107.     else
  108.     {
  109.         Selement* temp = glowa;
  110.         int licznik = 1;
  111.         while (licznik < pozycja - 1)
  112.         {
  113.             temp = temp->next;
  114.             licznik++;
  115.         }
  116.         nowy->next = temp->next; //przepisywanie następnika
  117.         temp->next = nowy; //podstawianie nowego elementu
  118.     }
  119.     return true; //gdy wszystko jest dobrze
  120. }
  121.  
  122. int zamianaLiterki(Selement* &glowa, char literkaPrzed, char literkaPo)
  123. {
  124.     int licznik = 0;
  125.     while (glowa!= nullptr)
  126.     {
  127.         if(glowa->litera==literkaPrzed)
  128.         {
  129.             glowa->litera = literkaPo;
  130.             licznik++;
  131.         }
  132.         glowa = glowa->next;
  133.     }
  134.     return licznik;
  135. }
  136.  
  137. int main()
  138. {
  139.     srand(time(NULL));
  140.     Selement* glowa = nullptr;
  141.     Selement* nowy = nullptr;
  142.  
  143.     bool jestNowy = true;
  144.     nowy = nowyElement(jestNowy);
  145.     while(jestNowy) //nie dodamy elementu nowy gdy wylosowano a
  146.     {
  147.         dodajNaKoniec(glowa,nowy);
  148.         nowy =nowyElement(jestNowy);
  149.     }
  150.  
  151.     drukowanieListy(glowa, "Zawartość listy:");
  152.  
  153.     nowy = new Selement;
  154.     nowy->litera = '#';
  155.     nowy->next = nullptr;
  156.  
  157.     dodajWPozycji(glowa, nowy, 4);
  158.  
  159.     drukowanieListy(glowa, "Zawartość listy po wstawieniu nowego elementu:");
  160.  
  161.     nowy = new Selement;
  162.     nowy->litera = '$';
  163.     nowy->next = nullptr;
  164.  
  165.     dodajWPozycji(glowa, nowy, 5);
  166.  
  167.     drukowanieListy(glowa, "Zawartość listy po wstawieniu nowego elementu:");
  168.  
  169.     int counter = zamianaLiterki(glowa, 'b', 'v');
  170.     cout << endl << counter << endl;
  171.  
  172.     drukowanieListy(glowa, "Zawartość listy po zmienie literki:");
  173.  
  174.     usuwanieListy(glowa);
  175.  
  176. }
  177.