Pi0trek

Lista

Oct 6th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. //**************************************************************************************************
  6. class Kontenerek       
  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 == 0)
  55.     {
  56.         return ;
  57.     }
  58.     else
  59.     {
  60.         if (pierwszy->wartocsc == ktory)
  61.         {
  62.             pierwszy = pierwszy->nastepny;
  63.             pierwszy->poprzedni = 0;
  64.         }
  65.         else
  66.         {
  67.  
  68.             Kontenerek *temp = pierwszy;
  69.             while (temp->nastepny != 0)
  70.             {
  71.                 if (ktory == temp->nastepny->wartocsc)
  72.                 {
  73.                     break;
  74.                 }
  75.                 temp = temp->nastepny;
  76.             }
  77.             if (temp->nastepny->nastepny == 0)
  78.             {
  79.                 temp->nastepny = 0;
  80.             }
  81.             else
  82.             {
  83.                 temp->nastepny = temp->nastepny->nastepny;
  84.                 temp->nastepny->poprzedni = temp;
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90. //**************************************************************************************************
  91. int main()
  92. {
  93.     Lista *listaLiczb = new Lista;
  94.     listaLiczb->dodajElment(12);
  95.     listaLiczb->dodajElment(14);
  96.     listaLiczb->dodajElment(11);
  97.     listaLiczb->dodajElment(21);
  98.     listaLiczb->dodajElment(18);
  99.  
  100.     cout << listaLiczb->pierwszy->wartocsc << endl;
  101.     cout << listaLiczb->pierwszy->nastepny->wartocsc << endl;
  102.     cout << listaLiczb->pierwszy->nastepny->nastepny->wartocsc << endl;
  103.     cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->wartocsc << endl;
  104.     cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->nastepny->wartocsc << endl;
  105.  
  106.     listaLiczb->usunElement(12);
  107.     cout << endl;
  108.  
  109.     cout << listaLiczb->pierwszy->wartocsc << endl;
  110.     cout << listaLiczb->pierwszy->nastepny->wartocsc << endl;
  111.     cout << listaLiczb->pierwszy->nastepny->nastepny->wartocsc << endl;
  112.     cout << listaLiczb->pierwszy->nastepny->nastepny->nastepny->wartocsc << endl;
  113.  
  114.     cout << endl;
  115.     cout << listaLiczb->pierwszy->nastepny->poprzedni->wartocsc << endl;
  116.     cout << listaLiczb->pierwszy->nastepny->nastepny->poprzedni->wartocsc << endl;
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment