Advertisement
Karolina99

Array list

Nov 17th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //lista w reprezentacji tablicowej
  6.  
  7. class ArrayList
  8. {
  9. private:
  10.     int* elems;                 //tablica przechowujaca elementy listy
  11.     int first_elem;                 //pozycja pierwszego elementu
  12.     int last_elem;                  //pozycja ostatniego elementu
  13.     int list_capacity;              //maksymalny rozmiar tablicy
  14.     int list_size;                  //liczba elementów w tablicy
  15.  
  16. public:
  17.     ArrayList(int c);                           //c - maksymalny rozmiar tablicy, konstruktor
  18.     bool empty();               //zwraca prawdę jeśli lista pusta, fałsz w przeciwnym przypadku
  19.     bool full();                //zwraca prawdę jeśli lista jest pełna, fałsz w przeciwnym przypadku
  20.     int size();                      //zwraca wielkość listy (liczbę elementów w liście)
  21.     int capacity();             //zwraca pojemnośc listy
  22.     int first();                //zwraca pozycję pierwszego elementu
  23.     int last();                 //zwraca pozycję ostatniego elementu
  24.     int next(int p);            //zwraca pozycję elementu następnego za p
  25.     int prev(int p);            //zwraca pozycję elementu poprzedzającego p
  26.     int retrieve(int p);            //zwraca element z pozycji p, retrieve - pobierac, odnalezc, odszukac
  27.     int locate(int x);              //zwraca pozycję pierwszego wystąpienia elementu x, -1 jeśli x nie występuje, wyszukiwanie liniowe, linear search
  28.     void insert(int p, int x);      //wstawia na pozycję p element x
  29.     void append(int x);         //wstawia x za ostatnim elementem listy
  30.     void del(int p);                //usuwa element z pozycji p
  31.     void clear();               //usuwa całą listę
  32.     void resize();             //zwiększa pojemnosc listy o 1/2 aktualnej pojemnosci
  33.     void delX(int x);         //usuwa pierwsze wystąpienie elementu x
  34.     void delAllX(int x);      //usuwa wszystkie wystapienia elementu x
  35.     bool concat(ArrayList& ll); // dodaje listę l2 na końcu listy, zwraca prawdę jeśli złączenie powiodło się, fałsz jeśli nie jest możliwe(czyli w przypadku braku miejsca w liście)
  36.     friend ostream& operator<<(ostream& out, ArrayList& l); //wypisuje elementy listy
  37. };
  38.  
  39. ArrayList::ArrayList(int c)
  40. {
  41.     elems = new int[c];
  42.     first_elem = 0;
  43.     last_elem = -1; //-1 mowi, ze lista jest pusta
  44.     list_capacity = c;
  45.     list_size = 0;
  46. }
  47.  
  48. bool ArrayList::empty()
  49. {
  50.     /**if (last_elem == -1)
  51.     {
  52.         return true;
  53.     }
  54.     else
  55.     {
  56.         return false;
  57.     }**/
  58.  
  59.     //drugi sposob zapisu
  60.  
  61.     if (last_elem == -1)
  62.         return true;
  63.     return false;
  64. }
  65.  
  66. bool ArrayList::full()
  67. {
  68.     /**if (list_size >= list_capacity)
  69.     {
  70.         return true;
  71.     }
  72.     else
  73.     {
  74.         return false;
  75.     }**/
  76.  
  77.     if (list_size == list_capacity)
  78.         return true;
  79.     return false;
  80. }
  81.  
  82. int ArrayList::size()
  83. {
  84.     return list_size;
  85. }
  86.  
  87. int ArrayList::capacity()
  88. {
  89.     return list_capacity;
  90. }
  91.  
  92. int ArrayList::first()
  93. {
  94.     /**if (empty())
  95.     {
  96.         cout << "Brak elementow w liscie!" << endl;
  97.     }
  98.     else
  99.     {
  100.         return first_elem;
  101.     }**/
  102.  
  103.     if (!empty())
  104.         return first_elem;
  105.     return NULL;
  106. }
  107.  
  108. int ArrayList::last()
  109. {
  110.     /**if (empty())
  111.     {
  112.         cout << "Brak elemetow w liscie!" << endl;
  113.     }
  114.     else
  115.     {
  116.         return last_elem;
  117.     }**/
  118.  
  119.     if (!empty())
  120.         return last_elem;
  121.     return NULL;
  122. }
  123.  
  124. int ArrayList::next(int p)
  125. {
  126.     /**if (p < list_size - 1)
  127.     {
  128.         return p + 1;
  129.     }
  130.     else
  131.     {
  132.         cout << "Nie ma nastepnego elementu w liscie!" << endl;
  133.     }**/
  134.  
  135.     if (p < last_elem)
  136.         return p + 1;
  137.     return NULL;
  138. }
  139.  
  140. int ArrayList::prev(int p)
  141. {
  142.     /**if (p > first_elem)
  143.     {
  144.         return p - 1;
  145.     }
  146.     else
  147.     {
  148.         cout << "Nie ma poprzedniego elementu w liscie!" << endl;
  149.     }**/
  150.  
  151.     if (p > first_elem)
  152.         return p - 1;
  153.     return NULL;
  154. }
  155.  
  156. int ArrayList::retrieve(int p)
  157. {
  158.     /**if (p >= first_elem && p <= last_elem)
  159.     {
  160.         return elems[p];
  161.     }
  162.     else
  163.     {
  164.         cout << "Elementu nie ma w liscie!" << endl;
  165.     }**/
  166.  
  167.     if ((p >= first_elem) && (p <= last_elem))
  168.         return elems[p];
  169.     return NULL;
  170. }
  171.  
  172. int ArrayList::locate(int x)
  173. {
  174.     for (int i = first_elem; i <= last_elem; i++) //wyszukiwanie liniowe
  175.     {
  176.         if (elems[i] == x)
  177.         {
  178.             return i;
  179.         }
  180.     }
  181.     return -1;
  182. }
  183.  
  184. void ArrayList::insert(int p, int x)
  185. {
  186.     /**if (!full()) //jesli jest niepelna
  187.     {
  188.         for (int i = last_elem; i >= p; i--) //tak jest szybciej niz jakbysmy zaczeli od first_elem
  189.         {
  190.             elems[i + 1] = elems[i];
  191.         }
  192.         elems[p] = x;
  193.         list_size++;
  194.         last_elem++;
  195.     }
  196.     else
  197.     {
  198.         //modyfikacja metody insert, jesli lista jest pelna to zwiekszamy pojemnosc listy za pomoca metody resize()
  199.         //cout << "Lista jest pelna!" << endl;
  200.         resize();
  201.         for (int i = last_elem; i >= p; i--) //tak jest szybciej niz jakbysmy zaczeli od first_elem
  202.         {
  203.             elems[i + 1] = elems[i];
  204.         }
  205.         elems[p] = x;
  206.         list_size++;
  207.         last_elem++;
  208.     }**/
  209.  
  210.     if (full())
  211.         resize();
  212.  
  213.     for (int i = last_elem; i >= p; i--) //tak jest szybciej niz jakbysmy zaczeli od first_elem
  214.     {
  215.         elems[i + 1] = elems[i];
  216.     }
  217.  
  218.     //sposob
  219.     /**for (int i = p; i <= last_elem; i++)
  220.     {
  221.         elems[i + 1] = elems[i];
  222.     }**/
  223.  
  224.     elems[p] = x;
  225.     list_size++;
  226.     last_elem++;
  227. }
  228.  
  229. void ArrayList::append(int x)
  230. {
  231.     /**if (!full())
  232.     {
  233.         elems[last_elem + 1] = x;
  234.         list_size++;
  235.         last_elem++;
  236.     }
  237.     else
  238.     {
  239.         cout << "Lista jest pelna!" << endl;
  240.     }**/
  241.  
  242.     if (!full())
  243.     {
  244.         elems[last_elem + 1] = x;
  245.         list_size++;
  246.         last_elem++;
  247.     }
  248.     else
  249.     {
  250.         return;
  251.     }
  252. }
  253.  
  254. void ArrayList::del(int p)
  255. {
  256.     if (!empty()) //jesli lista jest niepusta
  257.     {
  258.         if (p >= first_elem && p <= last_elem)
  259.         {
  260.             for (int i = p; i <= last_elem; i++)
  261.             {
  262.                 elems[i] = elems[i + 1];
  263.             }
  264.             list_size--;
  265.             last_elem--;
  266.         }
  267.         else
  268.         {
  269.             //cout<<"Pozycja nie istnieje!" << endl;
  270.             return;
  271.         }
  272.     }
  273.     else
  274.     {
  275.         //cout << "Lista jest pusta!" << endl;
  276.         return;
  277.     }
  278. }
  279.  
  280. void ArrayList::clear()
  281. {
  282.     if (!empty()) //jesli lista jest niepusta
  283.     {
  284.         last_elem = -1;
  285.         list_size = 0;
  286.     }
  287.     else
  288.     {
  289.         //cout << "Lista jest pusta!" << endl;
  290.         return;
  291.     }
  292. }
  293.  
  294. void ArrayList::resize()
  295. {
  296.     list_capacity = int(1.5 * list_capacity); //swiadomie ucinam czesc ulamkowa
  297. }
  298.  
  299. void ArrayList::delX(int x)
  300. {
  301.     int a;
  302.     a = locate(x); //a-pozycja x w liscie
  303.     if (a != -1)
  304.     {
  305.         del(a);
  306.     }
  307.     else
  308.     {
  309.         //cout << "Elementu nie ma w liście!" << endl;
  310.         return;
  311.     }
  312. }
  313.  
  314. void ArrayList::delAllX(int x) //usuwa wszystkie wystapienia elementu x
  315. {
  316.     int b;
  317.     b = locate(x);
  318.     while (b != -1)
  319.     {
  320.         del(b);
  321.         b = locate(x);
  322.     }
  323. }
  324.  
  325. bool ArrayList::concat(ArrayList& ll)
  326. {
  327.     if (full()) //jesli lista jest pelna
  328.     {
  329.         return false;
  330.     }
  331.     else if ((list_capacity - list_size) < ll.list_size) //jesli w liscie jest za malo miejsca
  332.     {
  333.         return false;
  334.     }
  335.     else
  336.     {
  337.         int temp = ll.first_elem;
  338.         //for (int i = (last_elem + 1); i < list_size + ll.list_size; i++)
  339.         //{
  340.         //  elems[i] = ll.elems[temp];
  341.         //  temp++;
  342.         //}
  343.         while (temp != ll.last_elem+1)
  344.         {
  345.             append(ll.elems[temp]);
  346.             temp++;
  347.         }
  348.         return true;
  349.     }
  350. }
  351.  
  352. ostream& operator<<(ostream& out, ArrayList& l)
  353. {
  354.     int temp = l.first_elem;
  355.     while (temp != l.last_elem+1)
  356.     {
  357.         cout << l.elems[temp] << " ";
  358.         temp++;
  359.     }
  360.     return out;
  361. }
  362.  
  363. int main()
  364. {
  365.     //ArrayList lista=ArrayList(20); //dwie metody sa
  366.     ArrayList lista(20);
  367.  
  368.     lista.insert(0, 5);
  369.     lista.insert(1, 6);
  370.     lista.insert(2, 7);
  371.     lista.insert(3, 8);
  372.     lista.insert(4, 9);
  373.     lista.insert(5, 10);
  374.     cout << lista << endl;
  375.  
  376.     lista.append(12);
  377.     cout << lista << endl;
  378.  
  379. //  lista.delX(6);
  380. //  cout << lista << endl;
  381.  
  382. //  lista.clear();
  383. //  cout << lista<<endl;
  384.  
  385.     //tworze druga liste
  386.     ArrayList lista2(15);
  387.  
  388.     lista2.append(1);
  389.     lista2.append(2);
  390.     lista2.append(3);
  391.     lista2.append(4);
  392.     lista2.append(5);
  393.     cout << lista2 << endl;
  394.    
  395.     lista.concat(lista2);
  396.     cout << lista << endl;
  397.  
  398.  
  399.     return 0;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement