Pi0trek

Untitled

Oct 5th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. //**************************************************************************************************
  6. class Kontenerek        //Tworze kontenerek który będzie częscią mojej listy
  7. {
  8. public:
  9.     int wartocsc;  
  10.     Kontenerek *nastepny;
  11.     Kontenerek *poprzedni;
  12.     Kontenerek();
  13. };
  14. Kontenerek::Kontenerek()
  15. {
  16.     nastepny = 0;
  17.     poprzedni = 0;
  18. }
  19. //**************************************************************************************************
  20. class Lista  
  21. {
  22. public:
  23.     Kontenerek *pierwszy;
  24.     void dodajElment(int wartosc);
  25.     void usunElement(int ktory);
  26.     Lista();//Konstruktor
  27. };
  28. Lista::Lista()
  29. {
  30.     pierwszy = 0;
  31. }
  32. void Lista::dodajElment(int wartosc)
  33. {
  34.     Kontenerek *nowaLiczba = new Kontenerek;
  35.     nowaLiczba->wartocsc = wartosc;
  36.     if (pierwszy == 0)
  37.     {
  38.         pierwszy = nowaLiczba;
  39.     }
  40.     else
  41.     {
  42.         Kontenerek *tempo = pierwszy;
  43.         while (tempo->nastepny != 0)  
  44.         {
  45.             tempo = tempo->nastepny;
  46.         }
  47.         tempo->nastepny = nowaLiczba;
  48.         nowaLiczba->poprzedni = tempo;
  49.     }
  50. }
  51.  
  52. void Lista::usunElement(int ktory)
  53. {
  54.     if (pierwszy->wartocsc == ktory)
  55.     {
  56.         pierwszy = pierwszy->nastepny;
  57.         pierwszy->poprzedni = 0;
  58.     }
  59.     else
  60.     {
  61.  
  62.         Kontenerek *temp = pierwszy;
  63.         while (temp != 0)
  64.         {          
  65.             if (ktory == temp->nastepny->wartocsc)
  66.             {
  67.                 break;
  68.             }
  69.             temp = temp->nastepny;
  70.         }
  71.         if (temp->nastepny->nastepny == 0)
  72.         {
  73.             temp->nastepny = 0;
  74.         }
  75.         else
  76.         {
  77.             temp->nastepny = temp->nastepny->nastepny;
  78.             temp->nastepny->poprzedni = temp;
  79.         }
  80.     }
  81. }
  82.  
  83. //**************************************************************************************************
  84. int main()
  85. {
  86.     Lista *listaLiczb = new Lista;
  87.     listaLiczb->dodajElment(12);
  88.     listaLiczb->dodajElment(14);
  89.     listaLiczb->dodajElment(11);
  90.     listaLiczb->dodajElment(21);
  91.     listaLiczb->dodajElment(18);
  92.  
  93.     cout << listaLiczb->pierwszy->wartocsc << endl;
  94.     cout << listaLiczb->pierwszy->nastepny->wartocsc << endl;
  95.     cout << listaLiczb->pierwszy->nastepny->nastepny->wartocsc << endl;
  96.     cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->wartocsc << endl;
  97.     cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->nastepny->wartocsc << endl;
  98.  
  99.     listaLiczb->usunElement(12);
  100.     cout << endl;
  101.  
  102.     cout << listaLiczb->pierwszy->wartocsc << endl;
  103.     cout << listaLiczb->pierwszy->nastepny->wartocsc << endl;
  104.     cout << listaLiczb->pierwszy->nastepny->nastepny->wartocsc << endl;
  105.     cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->wartocsc << endl;
  106.  
  107.     cout << endl;
  108.     cout << listaLiczb->pierwszy->nastepny->poprzedni->wartocsc << endl;
  109.     cout << listaLiczb->pierwszy->nastepny->nastepny->poprzedni->wartocsc << endl;
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment